Unity UGUI 鼠标悬停一段时间显示Text文字

Unity UGUI 鼠标悬停一段时间显示Text文字

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

public class ShowBuildProperty : MonoBehaviour ,IPointerEnterHandler, IPointerExitHandler
{
    public Text BlocksProperty;    // 详细信息;
    [SerializeField]
    private float timer;           // 计时器;

    private bool isCanTimer;
    public float DelayTime;        // 悬停时间;

 
    

    private void Start()
    {
        timer = 0f;
        DelayTime = 0.5f;
        BlocksProperty = GameObject.Find("BlocksProperties").GetComponent();
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        timer = 0f;
        isCanTimer = true;
        BlocksProperty.text = this.gameObject.name + "\n\r" + "Kawano";
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        
        isCanTimer = false;
        // BlocksProperty.enabled=false;
        HideUIProperty();
    }
    /// 
    ///显示详细信息
    /// 
    private void ShowUIProperty()
    {
        if (!BlocksProperty.IsActive())
        {
            BlocksProperty.enabled = true;
        }
        
        
       
    }
    /// 
    /// 内容归为空
    /// 
    private void HideUIProperty()
    {
        BlocksProperty.text = string.Empty;
        BlocksProperty.enabled = false;
        
    }
    private void Update()
    {

       DelayTimeShow();
       

    }
    void DelayTimeShow()
    {
        if (isCanTimer)
        {
            BlocksProperty.rectTransform.position = Input.mousePosition;  // 进入图片后, 提示文字跟随鼠标;
            timer += Time.deltaTime;
            if ( timer > DelayTime)
            {
                ShowUIProperty();
                timer = 0f;


            }
        }

        
    }

}


OnPointerEnter ,OnPointerExit 是 一帧 触发的 , 是时间点触发。

你可能感兴趣的:(Unity,UI)