| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using DG.Tweening;
- namespace UnityCommon
- {
- public class SmartLabelItem : MonoBehaviour
- {
- private Transform tfLine;
- private void Awake()
- {
- tfLine = transform.Find("Line");
- (tfLine as RectTransform).sizeDelta = new Vector2(offset, (tfLine as RectTransform).sizeDelta.y);
- Hide();
- }
- [HideInInspector]
- public Transform tfTargetItem;
- //private Vector3 centerP;
- private Canvas canvas;
- private float offset = 120f;
- private float angle = 0;
- public void SetLabel(string str, Canvas canvas, Transform tfTargetItem, float angle /*Vector3 centerP*/)
- {
- var txt = GetComponentInChildren<TextMeshProUGUI>();
- txt.text = str;
- var uilistener = UIEventListener.GetListener(transform.Find("Label").GetComponent<Image>().gameObject);
- uilistener.OnEnter = OnEnter;
- uilistener.OnExit = OnExit;
- uilistener.OnClick = OnClick;
- this.tfTargetItem = tfTargetItem;
- //this.centerP = centerP;
- this.angle = angle;
- this.canvas = canvas;
- }
- public void SetOffset(float offset)
- {
- this.offset = offset;
- }
- private void Update()
- {
- if (canvas != null && isShow)
- SetPosition();
- }
- private void SetPosition()
- {
- if (Camera.main.IsInFrontOfMe(tfTargetItem))
- {
- gameObject.SetActive(true);
- gameObject.transform.localScale = Vector3.one;
- var t = Utility.WordPosition2UILocalposition(Camera.main, canvas, tfTargetItem.position);
- //var c = Utility.WordPosition2UILocalposition(Camera.main, canvas, centerP);
- //var dir = (t - c).normalized;
- var dir = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.up;
- //dir = Quaternion.Inverse(dir);
- var labelP = t + dir.normalized * offset;
- (transform as RectTransform).anchoredPosition = labelP;
- if (tfLine != null)
- {
- //(tfLine as RectTransform).rotation = Quaternion.FromToRotation(Vector3.right, -dir);
- (tfLine as RectTransform).rotation = Quaternion.FromToRotation(Vector3.right, -dir);
- }
- }
- else
- {
- gameObject.transform.localScale = Vector3.zero;
- }
- }
- public void SetAngle(float angle)
- {
- this.angle = angle;
- }
- public void SetLabel(string label)
- {
- var txt = GetComponentInChildren<TextMeshProUGUI>();
- txt.text = label;
- }
- private SmartLabelHelperBase helper;
- public void SetHelper(SmartLabelHelperBase helper)
- {
- this.helper = helper;
- }
- private void OnClick(GameObject go, PointerEventData pEventData)
- {
- helper?.OnClick(this);
- }
- private void OnExit(GameObject go, PointerEventData pEventData)
- {
- helper?.OnExit(this);
- }
- private void OnEnter(GameObject go, PointerEventData pEventData)
- {
- transform.SetAsLastSibling();
- helper?.OnEnter(this);
- }
- private bool isShow = false;
- public void Show()
- {
- var t = Utility.WordPosition2UILocalposition(Camera.main, canvas, tfTargetItem.position);
- //var c = Utility.WordPosition2UILocalposition(Camera.main, canvas, centerP);
- //var dir = (t - c).normalized;
- var dir = Quaternion.AngleAxis(angle, Vector3.up).eulerAngles.normalized;
- var labelP = t + dir * offset;
- var rectTF = transform as RectTransform;
- rectTF.anchoredPosition = t;
- if (tfLine != null)
- {
- tfLine.localScale = new Vector3(0, 1, 1);
- (tfLine as RectTransform).rotation = Quaternion.FromToRotation(Vector3.right, -dir);
- }
- gameObject.SetActive(true);
- rectTF.DOLocalMove(labelP, 1).onComplete = () =>
- {
- isShow = true;
- };
- tfLine.DOScale(Vector3.one, 1);
- }
- public void Hide()
- {
- isShow = false;
- gameObject.SetActive(false);
- }
- }
- }
|