using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; public class TipPlayerExx : MonoBehaviour { public static TipPlayerExx Instance; private void Awake() { Instance = this; } public Text txtTitle; public Button btnTitle; Animator anim; Data currentInfo; TipShow[] lstTipShow; public float maxDistance = 10f; void Start() { anim = GetComponent(); if (btnTitle != null) btnTitle.onClick.AddListener(OnBtnTitleClick); // lstTipShow = FindObjectsOfType(); } void OnBtnTitleClick() { GameObject obj = GameObject.Find(currentInfo.ParentID); if (obj != null) { BaseShow[] bss = obj.GetComponents(); foreach (var bs in bss) { bs.OnClick(); } HideTipOnClick(); } } bool isShow; public void ShowTip(Data info) { if (isShow) { if (currentInfo == info) return; else { currentInfo = info; txtTitle.text = info.Title; } } else { if (isClicked && clickedInfo == info) return; isShow = true; currentInfo = info; txtTitle.text = info.Title; isClicked = false; // 显示 if (anim != null) { // anim.SetBool("IsShow", true); anim.SetTrigger("ShowTip"); } } } bool isClicked; Data clickedInfo; public void HideTipOnClick() { clickedInfo = currentInfo; isClicked = true; HideTip(); } public void HideTip() { isShow = false; txtTitle.text = ""; currentInfo = null; // 关闭 if (anim != null) { //anim.SetBool("IsShow", false); anim.SetTrigger("HideTip"); } } /// /// 判断位置是否在相机视野内 /// /// /// private bool IsInViewX(Vector3 worldPos) { Transform camTransform = CommonData.MainCamera.transform; Vector2 viewPos = CommonData.MainCamera.WorldToViewportPoint(worldPos); Vector3 dir = (worldPos - camTransform.position).normalized; float dot = Vector3.Dot(camTransform.forward, dir);//判断物体是否在相机前面 if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1) return true; else return false; } public float xMin = 0; public float yMin = 0; public float xMax = 1; public float yMax = 1; public bool IsInView(Vector3 pos) { Camera mCamera = CommonData.MainCamera; //转化为视角坐标 Vector3 viewPos = mCamera.WorldToViewportPoint(pos); // z<0代表在相机背后 if (viewPos.z < 0) return false; //太远了!看不到了! if (viewPos.z > maxDistance) return false; // x,y取值在 0~1之外时代表在视角范围外; if (viewPos.x < xMin || viewPos.y < yMin || viewPos.x > xMax || viewPos.y > yMax) return false; return true; } private void Update() { if (lstTipShow == null || lstTipShow.Length == 0) { lstTipShow = FindObjectsOfType(); } if (lstTipShow.Any(ts => IsInView(ts.transform.position))) { var result = lstTipShow.Where(ts => IsInView(ts.transform.position)); // Debug.Log(result.Count()); //string stmp = null; //foreach (var item in result) //{ // stmp += "|" + item.name; //} //Debug.Log(stmp); TipShow tmp = result.Max(); if (Vector3.Distance(CommonData.MainCamera.transform.position, tmp.transform.position) < maxDistance) { ShowTip(tmp.info); } else { if (isShow) { HideTip(); } } } else { if (isShow) { HideTip(); } } } //public float maxDistance = 500; //void Update() //{ // // 获取屏幕中心点的坐标 // Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0); // // 将屏幕中心点的坐标转换为世界坐标系中的坐标 // Ray ray = CommonData.MainCamera.ScreenPointToRay(screenCenter); // Debug.DrawRay(ray.origin, ray.direction, Color.red); // RaycastHit hit; // // 如果射线与物体相交,则表示物体在摄像机的视线范围内 // if (Physics.Raycast(ray, out hit, maxDistance)) // { // Debug.Log(hit.collider.name); // TipShow ts = hit.collider.GetComponent(); // if(ts != null) // { // Debug.Log(ts.gameObject.name); // } // } //} }