using GLTFast; using HighlightPlus; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; /// /// 设备对象池 /// public class EquipmentPool : Singleton { //创建池 private Dictionary pool = new Dictionary(); private List Data = new List(); public override void Awake() { base.Awake(); var url = $"{Application.streamingAssetsPath}/Config/EquipmentAttributeConfig.json"; HttpManager.Instance.Get>(url, data => Data = data); } public async Task GetEquipment(string key, Vector3 position, Quaternion rotation, Transform parent) { var temp = FindEquipment(key); GameObject go; if (temp == null) { temp = await LoadEquipment(key); } go = Instantiate(temp, position, rotation); go.transform.parent = parent; go.SetActive(true); return go; } private async Task LoadEquipment(string key) { AssetManager.Instance.SetLoadingPanel(true); //var path = $"{Application.streamingAssetsPath}/ModelBase/Equipments/{key}/{key}.gltf"; var path = string.Format(HttpManager.Instance.GetEquipmentModel, key, key); Debug.Log(path); var gltf = new GltfImport(); var success = await gltf.Load(path, new ImportSettings()); if (success) { var root = new GameObject(key); gltf.InstantiateMainScene(root.transform); pool.Add(key, root); SetAttribute(key, root); root.SetActive(false); AssetManager.Instance.SetLoadingPanel(false); return root; } return null; } private GameObject FindEquipment(string key) { //从列中中找出未激活的游戏对象并返回 return pool.ContainsKey(key) ? pool[key] : null; } private void SetAttribute(string key, GameObject go) { var data = Data.Find(temp => temp.Id == key); if (data.IsBoxCollider) { var collider = go.AddComponent(); collider.center = data.BoxColliderCenter; collider.size = data.BoxColliderSize; } if (data.IsMeshCollider) { var mesh = go.transform.Find("Scene/Plane").GetComponent().mesh; go.AddComponent(); go.AddComponent().sharedMesh=mesh; } var controller = go.AddComponent(); controller.equipmentType = data.Type; var uf = go.AddComponent(); uf.IconName = data.ufIconName; uf.DistanceY = data.ufDistanceY; uf.ChangeFloor = data.ufChangeFloor; uf.FloorNumber = data.ufFloorNumber; var effacr = go.AddComponent(); effacr.profile = AssetManager.Instance.highlightProfile; effacr.profile.Load(effacr); go.AddComponent(); if (data.IsEquipmentDataListening) go.AddComponent(); go.AddComponent(); } }