在编写unity时,需要实现鼠标在某一个按钮上时,就显示其子物体中对应的下拉菜单,为此编写了一个公共类,对于需要悬浮显示的控件均可挂载此类。代码如下:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;public class SuspendShow:MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{public GameObject showContent;//悬浮显示的内容public void OnPointerEnter(PointerEventData eventData){showContent.SetActive(true);}public void onPointerExit(PointerEventData eventData){showContent.SetActive(false);}
}
该类的应用场景还可以放在图标类型的功能按钮上,悬浮在功能按钮上时功能按钮的名称,方便区分按钮的功能。