上海虹口龙之梦项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
3.3 KiB

using GLTFast;
using HighlightPlus;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
/// <summary>
/// 设备对象池
/// <summary>
public class EquipmentPool : Singleton<EquipmentPool>
{
//创建池
private Dictionary<string, GameObject> pool = new Dictionary<string, GameObject>();
private List<EquipmentAttributeConfig> Data = new List<EquipmentAttributeConfig>();
public override void Awake()
{
base.Awake();
var url = $"{Application.streamingAssetsPath}/Config/EquipmentAttributeConfig.json";
HttpManager.Instance.Get<List<EquipmentAttributeConfig>>(url, data => Data = data);
}
public async Task<GameObject> 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<GameObject> 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<BoxCollider>();
collider.center = data.BoxColliderCenter;
collider.size = data.BoxColliderSize;
}
if (data.IsMeshCollider)
{
var mesh = go.transform.Find("Scene/Plane").GetComponent<MeshFilter>().mesh;
go.AddComponent<ModelMeshEditor>();
go.AddComponent<MeshCollider>().sharedMesh=mesh;
}
var controller = go.AddComponent<EquipmentController>();
controller.equipmentType = data.Type;
var uf = go.AddComponent<UIFollowTarget>();
uf.IconName = data.ufIconName;
uf.DistanceY = data.ufDistanceY;
uf.ChangeFloor = data.ufChangeFloor;
uf.FloorNumber = data.ufFloorNumber;
var effacr = go.AddComponent<HighlightEffect>();
effacr.profile = AssetManager.Instance.highlightProfile;
effacr.profile.Load(effacr);
go.AddComponent<DragObject>();
if (data.IsEquipmentDataListening)
go.AddComponent<EquipmentDataListening>();
go.AddComponent<Selectable>();
}
}