215 lines
5.1 KiB
C#
215 lines
5.1 KiB
C#
|
|
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;
|
|||
|
|
|
|||
|
|
// <20><>ʾ
|
|||
|
|
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;
|
|||
|
|
// <20>ر<EFBFBD>
|
|||
|
|
if (anim != null)
|
|||
|
|
{
|
|||
|
|
//anim.SetBool("IsShow", false);
|
|||
|
|
anim.SetTrigger("HideTip");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ж<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ұ<EFBFBD><D2B0>
|
|||
|
|
/// </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);//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
//ת<><D7AA>Ϊ<EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
Vector3 viewPos = mCamera.WorldToViewportPoint(pos);
|
|||
|
|
// z<0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (viewPos.z < 0) return false;
|
|||
|
|
//̫Զ<CCAB>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>
|
|||
|
|
if (viewPos.z > maxDistance)
|
|||
|
|
return false;
|
|||
|
|
// x,yȡֵ<C8A1><D6B5> 0~1֮<31><D6AE>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽǷ<D3BD>Χ<EFBFBD>⣻
|
|||
|
|
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()
|
|||
|
|
//{
|
|||
|
|
// // <20><>ȡ<EFBFBD><C8A1>Ļ<EFBFBD><C4BB><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
// Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0);
|
|||
|
|
|
|||
|
|
// // <20><><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
// Ray ray = CommonData.MainCamera.ScreenPointToRay(screenCenter);
|
|||
|
|
// Debug.DrawRay(ray.origin, ray.direction, Color.red);
|
|||
|
|
// RaycastHit hit;
|
|||
|
|
|
|||
|
|
// // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ཻ<EFBFBD><E0BDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߷<EFBFBD>Χ<EFBFBD><CEA7>
|
|||
|
|
// 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);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
}
|