简体   繁体   中英

How to solve this OnPointerEnter() problem in unity

I'm making some game menu in unity using UI event systems.

My intention is when I move mouse pointer over the text in game menu area, font-size get bigger and when I move mouse pointer out, font size get back to it's original size.

It works as I intended but there's a problem that if I move mouse pointer over the text and set my game menu deactive then active again using key press,font-size doesn't get back to it's original size,just getting bigger.

Here's my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class HandleIngameMenu : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler,IPointerClickHandler
{

    private Text texts=null;
    private void Start()
    {
        texts = GetComponentInChildren<Text>();
    }


    public void OnPointerEnter(PointerEventData data)
    {
        texts.fontSize += 3;
    }


    public void OnPointerExit(PointerEventData data)
    {
        if (this.gameObject.name.Equals("ReStartBtn"))
        {
            texts.fontSize = 30;
        }
        else
        {
            texts.fontSize = 37;
        }
}

I guess setting object deactive is not same as moving mouse pointer out.

Is there any way to solve this problem??

When you activate your menu set the defaults, this way it won't matter what happens when it is deactivated. Or alternatively when you deactivate your menu run the defaults method first then deactivate. This way when it's enabled it will be back to normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM