Files
TaiWan/Assets/Roaming/Extenders/TipPlayerEx/Scripts/TipPlayerExx.cs
2025-10-31 15:20:38 +08:00

215 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Animator>();
if (btnTitle != null)
btnTitle.onClick.AddListener(OnBtnTitleClick);
// lstTipShow = FindObjectsOfType<TipShow>();
}
void OnBtnTitleClick()
{
GameObject obj = GameObject.Find(currentInfo.ParentID);
if (obj != null)
{
BaseShow[] bss = obj.GetComponents<BaseShow>();
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");
}
}
/// <summary>
/// ÅжÏλÖÃÊÇ·ñÔÚÏà»úÊÓÒ°ÄÚ
/// </summary>
/// <param name="worldPos"></param>
/// <returns></returns>
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<TipShow>();
}
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<TipShow>();
// if(ts != null)
// {
// Debug.Log(ts.gameObject.name);
// }
// }
//}
}