using UnityEngine; using UnityEngine.UI; using QFramework; using System.Text.RegularExpressions; using DG.Tweening; using System; using System.Collections.Generic; using System.Linq; using static UnityEngine.GraphicsBuffer; using UnityEngine.Assertions; namespace QFramework.Example { public class UIGuideTipData : UIPanelData { public string targets; public string showName; public string offestPos; public string tiptextAudio; } public partial class UIGuideTip : UIPanel { [SerializeField] public Camera gameCamera; [SerializeField] public List TipOffectList; [SerializeField] public List TipItemList; //UIGuideTipData uIGuideTipData; public Camera Camera { get { return this.gameCamera; } set { this.gameCamera = value; } } ResLoader loader; protected override void OnInit(IUIData uiData = null) { mData = uiData as UIGuideTipData ?? new UIGuideTipData(); //uIGuideTipData = mData; // please add init code here loader = ResLoader.Allocate(); TypeEventSystem.Global.Register((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject); } protected override void OnOpen(IUIData uiData = null) { Connet.RemoveAllChildren(); TipOffectList.Clear(); TipItemList.Clear(); mData = uiData as UIGuideTipData ?? new UIGuideTipData(); //uIGuideTipData = mData; if (uiData != null) { Connet.RemoveAllChildren(); gameCamera = GameObject.Find("ZhanShiCamera").GetComponent(); mData = uiData as UIGuideTipData ?? new UIGuideTipData(); Debug.Log(mData); List Objs = mData.targets.Split(',')?.ToList(); List ShowTexts = mData.showName.Split(',')?.ToList(); if (String.IsNullOrEmpty(mData.offestPos) == false) { List TipOffects = mData.offestPos.Split('|')?.ToList(); for (int i = 0; i < TipOffects.Count; i++) { Vector3 TipOffect = Utility.GetVector3FromStrArray(TipOffects[i]); Debug.Log(TipOffect + "??????????"); TipOffectList.Add(TipOffect); } } else { Debug.Log("没有设置Tip偏移"); } //生成预制体 for (int i = 0; i < Objs.Count; i++) { GameObject tipItemObj = GameObject.Instantiate(TipItem.gameObject, Connet); TipItemList.Add(tipItemObj); tipItemObj.name = Objs[i]; GameObject target = GameObject.Find(Objs[i].ToString()); if (target==null) { Debug.Log("出现异常,没有找到该物体"); } tipItemObj.GetComponentInChildren().text = ShowTexts[i]; UpdatePos( target, GetComponentInParent(), tipItemObj.GetComponent(), tipItemObj.transform.Find("tip").GetComponent(), TipOffectList[i], tipItemObj.transform.Find("Circle").GetComponent(), tipItemObj.transform.Find("Line").GetComponent() ); } } isUpdate = true; } //private void OnCloseSelf(string[] obj) //{ // CloseSelf(); //} bool isUpdate = false; private void LateUpdate() { if (isUpdate) { for (int i = 0; i < TipItemList.Count; i++) { GameObject tipItemObj = TipItemList[i]; GameObject target = GameObject.Find(TipItemList[i].name); Debug.Log("LateUpdate"); UpdatePos( target, GetComponentInParent(), tipItemObj.GetComponent(), tipItemObj.transform.Find("tip").GetComponent(), TipOffectList[i], tipItemObj.transform.Find("Circle").GetComponent(), tipItemObj.transform.Find("Line").GetComponent() ); } } } /// /// 计算屏幕的位置 /// public static Vector3 CalculateScreenPosition(Vector3 position/* 目标点*/, Camera camera, Canvas canvas, RectTransform transform) { Assert.IsNotNull(camera); Assert.IsNotNull(canvas); Assert.IsNotNull(transform); //世界坐标转屏幕坐标 Vector3 vector = camera.WorldToScreenPoint(position); Vector3 zero = Vector3.zero; //判断canvas 渲染模式 switch (canvas.renderMode) { //屏幕空间 case RenderMode.ScreenSpaceOverlay: RectTransformUtility.ScreenPointToWorldPointInRectangle(transform, vector, null, out zero); break; case RenderMode.ScreenSpaceCamera: //世界空间 case RenderMode.WorldSpace: RectTransformUtility.ScreenPointToWorldPointInRectangle(transform, vector, canvas.worldCamera, out zero); break; } return zero; } /// /// 赋值TipItem方法 /// private void UpdatePos(GameObject target, Canvas canvas, RectTransform TipItem, RectTransform tip, Vector3 tipOffset, RectTransform YuanDian, RectTransform zhiyinxian) { //if (this.target == null || this.canvas == null || this.gameCamera == null || this.rectTransform == null) //{ // return; //} //计算屏幕坐标 Vector3 position = CalculateScreenPosition(target.transform.position, gameCamera, canvas, tip.GetComponent()); tip.transform.position = position + tipOffset; YuanDian.transform.position = Camera.WorldToScreenPoint(target.transform.position) /*+ YuanDianOffset*/; zhiyinxian.transform.position = (YuanDian.transform.position + tip.transform.position) / 2; zhiyinxian.transform.localEulerAngles = new Vector3(0, 0, Vector2.SignedAngle(Vector2.right, YuanDian.transform.position - tip.transform.position)); float length = (YuanDian.transform.position - tip.transform.position).magnitude; //设置线的宽度 长度 zhiyinxian.GetComponent().sizeDelta = new Vector2(length, 3f); Debug.Log("UpdatePos"); } protected override void OnShow() { } protected override void OnHide() { isUpdate = false; } protected override void OnClose() { } } }