160 lines
4.3 KiB
C#
160 lines
4.3 KiB
C#
using System;
|
|
using Cysharp.Threading.Tasks;
|
|
using MagicaCloth2;
|
|
using PhxhSDK;
|
|
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();
|
|
var sampleObj = await AssetManager.Instance.LoadAssetAsync<GameObject>(path);
|
|
_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());
|
|
Utils.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 o)
|
|
{
|
|
if (IsDispose())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (o == null)
|
|
{
|
|
DebugUtil.LogError("Unit Load Error: path = {0}", _path);
|
|
return;
|
|
}
|
|
|
|
var go = GameObject.Instantiate(o);
|
|
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);
|
|
DebugUtil.Log("------------unit({0}) loaded, path = {1}", _id, _path);
|
|
|
|
// 自动设置层级
|
|
_node.GameObject.SetLayerEx(_node.GameObject.layer);
|
|
}
|
|
_loadStatus = LoadStatus.Loaded;
|
|
}
|
|
|
|
protected virtual void OnDispose()
|
|
{
|
|
}
|
|
}
|
|
}
|