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(); txt.text = str; var uilistener = UIEventListener.GetListener(transform.Find("Label").GetComponent().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(); 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); } } }