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

174 lines
4.8 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;
2024-02-20 13:45:32 +08:00
using Object = UnityEngine.Object;
2023-08-22 19:08:45 +08:00
namespace Framework.Wrapper
{
public class Unit : IDisposable
{
public enum UnitType
{
Charactor,
Vehicle,
Building,
2023-08-22 19:08:45 +08:00
}
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 bool isDisposed { get; protected set; } = false;
2023-08-22 19:08:45 +08:00
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;
}
2024-02-20 13:45:32 +08:00
public GameObject Load(string path)
2023-08-22 19:08:45 +08:00
{
_loadStatus = LoadStatus.Loading;
_path = path;
UpdateGameObjectName();
2024-02-20 14:33:52 +08:00
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(path);
2024-02-20 13:45:32 +08:00
var instObj = Object.Instantiate(sampleObj);
_OnLoaded(instObj);
2023-08-22 19:08:45 +08:00
return _node.GameObject;
}
public bool IsLoaded()
{
return _loadStatus == LoadStatus.Loaded;
}
public void Dispose()
{
isDisposed = true;
2023-08-22 19:08:45 +08:00
OnDispose();
if (_goWrapper != null)
_goWrapper.IsActive = false;
2024-02-20 13:45:32 +08:00
// if (_goWrapper != null) GameplayInfoManager.Instance.UnRegister(_goWrapper.GetInstanceID());
2024-02-04 16:49:26 +08:00
//Utils.Destroy(_goWrapper?.GameObject);
2024-02-20 13:45:32 +08:00
if (_goWrapper != null && _goWrapper.GameObject != null)
{
Object.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;
}
2024-02-20 13:45:32 +08:00
private void _OnLoaded(GameObject instObj)
2023-08-22 19:08:45 +08:00
{
if (IsDispose())
{
return;
}
2024-02-20 13:45:32 +08:00
if (instObj == null)
2023-08-22 19:08:45 +08:00
{
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;
}
2024-02-20 13:45:32 +08:00
var go = instObj;
2023-08-22 19:08:45 +08:00
if (go != null)
{
_goWrapper = new GameObjectWrapper(go);
var collector = new ComponentCollector(go);
2024-07-03 17:00:17 +08:00
Skeleton = new Skeleton(collector.Transforms, go.transform);
2023-08-22 19:08:45 +08:00
RendererWrapper = new RendererWrapper(collector.Renderers);
BoxColliders = collector.BoxColliders;
ParticleSystems = collector.ParticleSystems;
MeshFilter = collector.FirstMeshFilter;
RawAnimator = collector.FirstAnimator;
MagicaCloth = collector.MagicaClothFirst;
2024-02-20 13:45:32 +08:00
// 原用与收集unit信息然后进行选中判断,现在已弃用
// var info = new GameplayInfoManager.GameplayInfo();
// info.id = _id;
// info.type = 0;
// GameplayInfoManager.Instance.Register(_goWrapper.GetInstanceID(), info);
2023-08-22 19:08:45 +08:00
_node.AttachChild(_goWrapper);
2024-06-06 15:39:43 +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()
{
2024-02-20 15:17:22 +08:00
RendererWrapper = null;
Skeleton = null;
2023-08-22 19:08:45 +08:00
}
}
}