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

160 lines
4.3 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
using Cysharp.Threading.Tasks;
using MagicaCloth2;
2023-10-19 15:00:19 +08:00
using PhxhSDK;
2023-08-22 19:08:45 +08:00
using UnityEngine;
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 async UniTask<GameObject> Load(string path)
{
_loadStatus = LoadStatus.Loading;
_path = path;
UpdateGameObjectName();
2023-10-19 15:00:19 +08:00
var sampleObj = await AssetManager.Instance.LoadAssetAsync<GameObject>(path);
2023-08-22 19:08:45 +08:00
_onLoaded(sampleObj);
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());
2023-12-14 19:00:39 +08:00
Utils.Destroy(_goWrapper?.GameObject);
2023-08-22 19:08:45 +08:00
_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)
{
2023-10-19 15:00:19 +08:00
if (DebugUtil.LogEnable)
DebugUtil.Log(string.Format("Unit_{0} : {1}", _id, str));
2023-08-22 19:08:45 +08:00
}
public void LogError(string str)
{
2023-10-19 15:00:19 +08:00
if (DebugUtil.LogEnable)
DebugUtil.LogError(string.Format("Unit_{0} : {1}", _id, str));
2023-08-22 19:08:45 +08:00
}
protected void SetName(string name)
{
_node.Name = name;
}
private void _onLoaded(GameObject o)
{
if (IsDispose())
{
return;
}
if (o == null)
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("Unit Load Error: path = {0}", _path);
2023-08-22 19:08:45 +08:00
return;
}
var go = GameObject.Instantiate(o);
2023-08-22 19:08:45 +08:00
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;
var info = new GameplayInfoManager.GameplayInfo();
info.id = _id;
info.type = 0;
GameplayInfoManager.Instance.Register(_goWrapper.GetInstanceID(), info);
_node.AttachChild(_goWrapper);
2023-10-19 15:00:19 +08:00
DebugUtil.Log("------------unit({0}) loaded, path = {1}", _id, _path);
2023-08-22 19:08:45 +08:00
// 自动设置层级
_node.GameObject.SetLayerEx(_node.GameObject.layer);
}
_loadStatus = LoadStatus.Loaded;
}
protected virtual void OnDispose()
{
}
}
}