NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Wrapper/Unit.cs

169 lines
4.7 KiB
C#

using System;
using Cysharp.Threading.Tasks;
using MagicaCloth2;
using PhxhSDK;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Framework.Wrapper
{
public class Unit : IDisposable
{
public enum UnitType
{
Charactor,
Vehicle,
}
public Skeleton Skeleton;
public RendererWrapper RendererWrapper;
public BoxCollider[] BoxColliders;
public ParticleSystem[] ParticleSystems;
public MeshFilter MeshFilter;
public Animator RawAnimator;
public SkinnedMeshRenderer SkinnedMeshRenderer;
public MagicaCloth MagicaCloth;
public Node Node
{
get { return _node; }
}
enum LoadStatus
{
NotLoad,
Loading,
Loaded,
}
private uint _id;
protected Node _node = new Node();
private GameObjectWrapper _goWrapper;
private LoadStatus _loadStatus = LoadStatus.NotLoad;
// private bool isDisposed = false;
private string _path;
public string Path
{
get { return _path; }
}
public Unit(uint id)
{
_id = id;
}
public uint GetID()
{
return _id;
}
public GameObject Load(string path)
{
_loadStatus = LoadStatus.Loading;
_path = path;
UpdateGameObjectName();
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(path);
var instObj = Object.Instantiate(sampleObj);
_OnLoaded(instObj);
return _node.GameObject;
}
public bool IsLoaded()
{
return _loadStatus == LoadStatus.Loaded;
}
public void Dispose()
{
OnDispose();
_goWrapper?.SetActive(false);
// if (_goWrapper != null) GameplayInfoManager.Instance.UnRegister(_goWrapper.GetInstanceID());
//Utils.Destroy(_goWrapper?.GameObject);
if (_goWrapper != null && _goWrapper.GameObject != null)
{
Object.Destroy(_goWrapper.GameObject);
}
_node?.Dispose();
_node = null;
_loadStatus = LoadStatus.NotLoad;
EventManager.Instance.Send(EventManager.EventName.UnitDispose, _id);
}
public bool IsDispose()
{
return _node == null;
}
protected virtual void UpdateGameObjectName()
{
SetName(string.Format("Unit_{0}_{1}", _id, _path));
}
public void Log(string str)
{
if (DebugUtil.LogEnable)
DebugUtil.Log(string.Format("Unit_{0} : {1}", _id, str));
}
public void LogError(string str)
{
if (DebugUtil.LogEnable)
DebugUtil.LogError(string.Format("Unit_{0} : {1}", _id, str));
}
protected void SetName(string name)
{
_node.Name = name;
}
private void _OnLoaded(GameObject instObj)
{
if (IsDispose())
{
return;
}
if (instObj == null)
{
DebugUtil.LogError("Unit Load Error: path = {0}", _path);
return;
}
var go = instObj;
if (go != null)
{
_goWrapper = new GameObjectWrapper(go);
var collector = new ComponentCollector(go);
Skeleton = new Skeleton(collector.Transforms);
RendererWrapper = new RendererWrapper(collector.Renderers);
BoxColliders = collector.BoxColliders;
ParticleSystems = collector.ParticleSystems;
MeshFilter = collector.FirstMeshFilter;
RawAnimator = collector.FirstAnimator;
MagicaCloth = collector.MagicaClothFirst;
// 原用与收集unit信息然后进行选中判断,现在已弃用
// var info = new GameplayInfoManager.GameplayInfo();
// info.id = _id;
// info.type = 0;
// GameplayInfoManager.Instance.Register(_goWrapper.GetInstanceID(), info);
_node.AttachChild(_goWrapper);
DebugUtil.Log("------------unit({0}) loaded, path = {1}", _id, _path);
// 自动设置层级
_node.GameObject.SetLayerEx(_node.GameObject.layer);
}
_loadStatus = LoadStatus.Loaded;
}
protected virtual void OnDispose()
{
RendererWrapper = null;
Skeleton = null;
}
}
}