Tool道具类,挂在物体上,手动填入名称,实现鼠标移入移出物体标签显示隐藏

 

using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace FrameWork
{
    /// <summary>
    /// Tool道具类,挂在物体上,手动填入名称,实现鼠标移入移出物体标签显示隐藏
    /// </summary>
    public class ToolLabel : MonoBehaviour
    {
        public Vector2 hudOffset;//hud显示的偏移量


        [SerializeField]
        private string toolName; //道具名称
        /// <summary>
        /// 标签克隆物的父物体
        /// </summary>
        public Transform labelParent;
        /// <summary>
        /// 标签的预设物体
        /// </summary>
        public GameObject labelTemp;

        private RectTransform myLabel;
        private bool isShowStay;
        private bool isShow;
        private ToolLabelType type;

        public string ToolName
        {
            get
            {
                return toolName;
            }
            set
            {
                if (toolName != value)
                {
                    toolName = value;

                }
            }
        }
        private void Update()
        {
            if (myLabel != null)
            {
                if (isShow)
                {
                    float angle = Vector3.Angle(Camera.main.transform.forward, transform.position - Camera.main.transform.position);

                    bool isInView = angle < 55;

                    if (isInView)
                    {
                        Vector2 screenPos = Camera.main.WorldToScreenPoint(transform.position);

                        myLabel.anchoredPosition = new Vector2(screenPos.x - Screen.width / 2, screenPos.y - Screen.height / 2) + hudOffset;

                        myLabel.GetComponent<RectTransform>().localScale = Vector3.one;
                    }
                    else
                    {
                        myLabel.GetComponent<RectTransform>().localScale = Vector3.zero;
                    }
                }
                else
                {
                    myLabel.GetComponent<RectTransform>().localScale = Vector3.zero;
                }
            }
        }
        private void OnMouseEnter()
        {
            if (type != ToolLabelType.Mouse)
            {
                return;
            }
            if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())//屏蔽UI操作事件
            {
                return;
            }

            isShow = true;
        }
        private void OnMouseExit()
        {
            if (type != ToolLabelType.Mouse)
            {
                return;
            }
            if (isShowStay)
                return;
          
            isShow = false;
        }
        private void Awake()
        {
            isShowStay = false;
            isShow = false;
            type = ToolLabelType.Mouse;

            RegisterUI();
        }


        /// <summary>
        /// 事件响应,toollabel是否持续展示
        /// </summary>
        /// <param name="coreEvent"></param>
        private void ShowStay(CoreEvent coreEvent)
        {
           

            isShow = (bool)coreEvent.EventParam;
        }

        /// <summary>
        /// 事件响应,toollabel响应模式改变
        /// </summary>
        /// <param name="coreEvent"></param>
        private void ChangType(CoreEvent coreEvent)
        {
            type = (ToolLabelType)coreEvent.EventParam;
            isShow = false;
        }

        /// <summary>
        /// 事件响应,toollabel响应模式改变
        /// </summary>
        /// <param name="coreEvent"></param>
        private void PlaneControlToTarget(CoreEvent coreEvent)
        {
            if (type != ToolLabelType.PlaneControl)
            {
                return;
            }
            if (isShowStay)
                return;
            Transform target = (Transform)coreEvent.EventParam;
            isShow = transform == target;
        }
        private void OnDisable()
        {
            if (myLabel != null)
                myLabel.gameObject.SetActive(false);
            if (FrameEntrance.Instance != null)
            {
                FrameEntrance.Instance.Event.RemoveEventListener((int)CoreEventId.ShowAllToolLabel, ShowStay);
                FrameEntrance.Instance.Event.RemoveEventListener((int)CoreEventId.ToolLabelTypeChange, ChangType);
                FrameEntrance.Instance.Event.RemoveEventListener((int)CoreEventId.PlaneControlTarget, PlaneControlToTarget);
            }
        }
        private void OnEnable()
        {
            if (myLabel != null)
                myLabel.gameObject.SetActive(true);
            //添加事件监听
            FrameEntrance.Instance.Event.AddEventListener((int)CoreEventId.ShowAllToolLabel, ShowStay);
            FrameEntrance.Instance.Event.AddEventListener((int)CoreEventId.ToolLabelTypeChange, ChangType);
            FrameEntrance.Instance.Event.AddEventListener((int)CoreEventId.PlaneControlTarget, PlaneControlToTarget);
        }
        /// <summary>
        /// 生成Label
        /// </summary>
        public void RegisterUI()
        {
            if (labelTemp != null && myLabel == null)
            {
                myLabel = Instantiate(labelTemp.gameObject, labelParent).GetComponent<RectTransform>();
                myLabel.localScale = Vector3.zero;
                myLabel.Find("Text").GetComponent<Text>().text = ToolName;
                myLabel.name = ToolName;
            }
        }
        /// <summary>
        /// 设置标签的值
        /// </summary>
        /// <param name="name"></param>
        public void SetLabelValue(string name)
        {
            myLabel.Find("Text").GetComponent<Text>().text = name;
            myLabel.name = name;
            toolName = name;
        }
    }
    public enum ToolLabelType
    {
        Mouse = 1,
        PlaneControl = 2,
    }
}
 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值