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

242 lines
6.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.Video;
public class TipPointCollider : MonoBehaviour
{
static List<string> oldKeys = new List<string>();
static List<string> newKeys = new List<string>();
public static TipPointCollider Instance;
private void Awake()
{
Instance = this;
}
private void Start()
{
BaseController.StatusChanged += ControllerStatusChanged;
ShowPointEx.ShowPointChanged += ControllerStatusChanged;
}
Data info;
Dictionary<string, List<BaseShow>> dicShows = new Dictionary<string, List<BaseShow>>();
ShowBoxCollider tmpSBC;
GameObject tmpObj;
public void ControllerStatusChanged()
{
// Debug.Log(newKeys.Count);
#region Spot¿ØÖÆ
foreach (var key in newKeys)
{
if (oldKeys.Contains(key))
{
continue;
}
else
{
GameObject obj = GameObject.Find(key);
if (obj != null)
{
oldKeys.Add(key);
if (DataLoader<Data>.DataDics.ContainsKey(key))
{
info = DataLoader<Data>.DataDics[key];
AddShow(obj, info);
}
if (obj.transform.childCount != 0)
{
for (int i = 0; i < obj.transform.childCount; i++)
{
tmpObj = obj.transform.GetChild(i).gameObject;
if (DataLoader<Data>.DataDics.ContainsKey(tmpObj.name))
{
info = DataLoader<Data>.DataDics[tmpObj.name];
AddShow(tmpObj, info, key);
}
}
}
}
}
if (DataLoader<Data>.DataList != null)
{
var result = DataLoader<Data>.DataList.Where(data => data.ParentKey == key);
if (result != null && result.Count() > 0)
{
foreach (var item in result)
{
GameObject obj = GameObject.Find(item.ObjectID);
if (obj != null)
{
item.ShowHotTip = false;
AddShow(obj, item, key);
}
}
}
}
}
foreach (var key in oldKeys)
{
if (newKeys.Contains(key))
{
continue;
}
else
{
if (dicShows.ContainsKey(key))
{
foreach (BaseShow bs in dicShows[key])
{
if (bs.info.Clickable)
{
Destroy(bs.GetComponent<ShowBoxCollider>());
}
if (bs is VideoShow && bs.info.PlayVideoOnObject)
{
Destroy(bs.gameObject.GetComponent<VideoPlayer>());
}
Destroy(bs);
}
dicShows.Remove(key);
}
}
}
oldKeys.RemoveAll(tmp => !newKeys.Contains(tmp));
#endregion
}
public string GetImageShow(Data info, int index)
{
var result =
DataLoader<Data>.DataDics.Values.FirstOrDefault(data =>
(data.ObjectID == info.ObjectID || data.ParentID == info.ParentID)
&& data.Type == DataType.MultiImage
&& data.Index == index
);
if (result == null)
return null;
else
return result.Title;
}
void AddShow(GameObject obj, Data info, string key = null)
{
if (key == null)
{
key = info.ObjectID;
}
info.ParentKey = key;
if (info.IsType(DataType.Image) || info.IsType(DataType.MultiImage))
{
AddShow<ImageShow>(obj, info, key);
if (info.IsType(DataType.MultiImage))
{
Data dt = DataLoader<Data>.DataDics.Values.FirstOrDefault(data => data.ObjectID == info.ParentID);
if (dt != null)
{
info.DataDetails = dt.DataDetails;
}
}
}
if (info.IsType(DataType.Video))
{
AddShow<VideoShow>(obj, info, key);
}
if (info.IsType(DataType.Game))
{
AddShow<GameShow>(obj, info, key);
}
if (info.IsType(DataType.Url))
{
AddShow<UrlShow>(obj, info, key);
}
if (info.IsType(DataType.TipPoint))
{
AddShow<TipShow>(obj, info, key);
}
if (info.IsType(DataType.Material))
{
AddShow<MaterialShow>(obj, info, key);
}
if (info.IsType(DataType.Model))
{
AddShow<ModelShow>(obj, info, key);
}
if (info.IsType(DataType.Scene))
{
AddShow<SceneShow>(obj, info, key);
}
//else
// obj.AddComponent<ShowBoxCollider>().Test = true;
// Debug.Log(info.ObjectID);
if (info.Clickable)
{
tmpSBC = obj.AddComponent<ShowBoxCollider>();
tmpSBC.showHotTip = info.ShowHotTip;
tmpSBC.showBox = info.ShowBox;
}
}
void AddShow<T>(GameObject obj, Data info, string key)
where T : BaseShow
{
BaseShow bs = obj.AddComponent<T>();
bs.info = info;
if (dicShows.ContainsKey(key))
{
dicShows[key].Add(bs);
}
else
{
dicShows.Add(key, new List<BaseShow>());
dicShows[key].Add(bs);
}
}
void OnTriggerEnter(Collider other)
{
if (IsTipPoint(other))
{
newKeys.Add(other.name.Substring(3));
// Debug.Log(other.name.Substring(3) + "Enter");
}
}
void OnTriggerStay(Collider other)
{
if (IsTipPoint(other))
{
if (!newKeys.Contains(other.name.Substring(3)))
{
newKeys.Add(other.name.Substring(3));
// Debug.Log(other.name.Substring(3) + "Enter");
}
}
}
void OnTriggerExit(Collider other)
{
if (IsTipPoint(other))
{
newKeys.Remove(other.name.Substring(3));
// Debug.Log(other.name.Substring(3) + "Exit");
}
}
bool IsTipPoint(Collider other)
{
// return other.gameObject.layer == LayerMask.NameToLayer("TipPoint") && other.name.StartsWith("TP_");
return other.name.StartsWith("TP_");
}
}