using LitJson; using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using ZXKFramework; public class HighLightManager : MonoBehaviour { Outline outLine; private Dictionary allOutLineDict = new Dictionary(); Transform root; public void Init() { outLine = GetComponent(); root = GameObject.Find("Root").transform; Game.Instance.res.Load(MVC.GetModel().mainData.folder + "/ExcelData/ExcelToJson/BaseData.json", args => { string interactionObjs = ""; List data = JsonMapper.ToObject>(args.text); for (int i = 0; i < data.Count; i++) { interactionObjs += data[i]["obj"].ToString() + "|"; } string[] strs = interactionObjs.Split('|'); for (int i = 0; i < strs.Length; i++) { if (!allOutLineDict.ContainsKey(strs[i]) && !string.IsNullOrEmpty(strs[i])) { GameObject go = root.FindFirst(strs[i]); if (go != null && go.TryGetComponent(out Collider col)) { if (go.TryGetComponent(out Outline light)) { allOutLineDict.Add(strs[i], light); } else { Outline outli = go.AddComponent(); outli.OutlineColor = outLine.OutlineColor; outli.enabled = false; } } } } }); Game.Instance.eventManager.AddListener(VisiableLight); } private void VisiableLight(HighLightEvent e) { if (e.visiable) { ShowHighLight(e.name); } else { HideHighLight(e.name); } } /// /// 打开高亮 /// public void ShowHighLight(string name, Action callBack = null) { if (allOutLineDict.ContainsKey(name)) { allOutLineDict[name].enabled = true; callBack?.Invoke(); } else { GameObject go = GameObject.Find("Root").FindFirst(name); if (go != null) { allOutLineDict.Add(go.name, go.GetOrAddComponent()); } allOutLineDict[name].enabled = true; allOutLineDict[name].OutlineColor = outLine.OutlineColor; allOutLineDict[name].OutlineWidth = outLine.OutlineWidth; callBack?.Invoke(); } } public bool IsHighLight(string name) { if (allOutLineDict.ContainsKey(name)) { if (allOutLineDict[name].enabled && allOutLineDict[name].enabled) { return true; } else { return false; } } return false; } /// /// 关闭高亮 /// /// public void HideHighLight(string name, Action callBack = null) { if (allOutLineDict.TryGetValue(name, out Outline outli)) { if (outli != null) { outli.enabled = false; } } callBack?.Invoke(); } /// /// 关闭全部高亮 /// public void HideAllHighLight() { foreach (var item in allOutLineDict) { if (item.Value.enabled) item.Value.enabled = false; } } public void RemoveHighLight(string name) { if (allOutLineDict.ContainsKey(name)) { Destroy(allOutLineDict[name]); allOutLineDict.Remove(name); } } public void RemoveAllHighLight() { foreach (var item in allOutLineDict) { Destroy(item.Value); } allOutLineDict.Clear(); } private void OnDestroy() { Game.Instance.eventManager.RemoveListener(VisiableLight); } }