/// ///******************************************************** /// 脚本功能:交互Manager /// 创建人: GD /// 创建时间: 2023/09/20 14:12 ///******************************************************** /// using LitJson; using System.Collections.Generic; using UnityEngine; using ZXKFramework; public class InteractionManager : MonoBehaviour { Transform root; /// /// 所有交互的物体 /// public Dictionary _allInteraction; OperatingType type; public void Init(OperatingType type) { 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() + "|"; } this.type = type; _allInteraction = new Dictionary(); InitGetOrAddComponent(interactionObjs); }); } /// /// 开启物体的可交互 /// public void EnableInteraction(string name) { if (_allInteraction[name].TryGetComponent(out Collider collider)) { collider.enabled = true; } } /// /// 关闭物体的可交互 /// public void DisableInteraction(string name) { if (_allInteraction[name].TryGetComponent(out Collider collider)) { collider.enabled = false; } } /// /// 根据不同平台初始化 /// public void InitGetOrAddComponent(string value) { if (string.IsNullOrEmpty(value)) return; string[] allInteraction = value.Split('|'); switch (type) { case OperatingType.FirstPerson: case OperatingType.Hardware: case OperatingType.Touch: for (int j = 0; j < allInteraction.Length; j++) { if (!string.IsNullOrEmpty(allInteraction[j])) { if (!_allInteraction.ContainsKey(allInteraction[j])) { GameObject go = root.FindFirst($"{allInteraction[j]}"); if (go == null) continue; if (go.TryGetComponent(out Collider collider)) { go.GetOrAddComponent(); go.GetOrAddComponent(); go.GetOrAddComponent(); go.GetOrAddComponent(); go.GetOrAddComponent(); } _allInteraction.Add(allInteraction[j], go); } } } break; } } }