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,
}
}