From a2cc78cf52cea71e63f5fa99bbe579c57eee0434 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Mon, 19 Sep 2022 19:37:31 +0800 Subject: [PATCH] Update ActorManager.cs --- .../Scripts/Runtime/Actor/ActorManager.cs | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Assets/TEngine/Scripts/Runtime/Actor/ActorManager.cs b/Assets/TEngine/Scripts/Runtime/Actor/ActorManager.cs index 2c6afe90..5dd4b9c4 100644 --- a/Assets/TEngine/Scripts/Runtime/Actor/ActorManager.cs +++ b/Assets/TEngine/Scripts/Runtime/Actor/ActorManager.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using UnityEngine; + namespace TEngine.Runtime.Actor { public partial class ActorType @@ -25,12 +26,7 @@ namespace TEngine.Runtime.Actor private Dictionary _actorTypes = new Dictionary(); public Transform ActorRootTrans { get; set; } - /// - /// 注册Actor类型便于创建 - /// - /// - /// - public void RegisterActorTypes(int actorType,System.Type type) + public void RegisterActorType(int actorType,System.Type type) { if (!_actorTypes.ContainsKey(actorType)) { @@ -38,10 +34,32 @@ namespace TEngine.Runtime.Actor } } + private void RegisterAllTypes() + { + System.Type baseType = typeof(GameActor); + System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); + System.Type[] types = assembly.GetTypes(); + for (int i = 0; i < types.Length; i++) + { + if (!types[i].IsClass || types[i].IsAbstract) + { + continue; + } + if (types[i].BaseType == baseType) + { + GameActor actor = (GameActor)System.Activator.CreateInstance(types[i]); + RegisterActorType(actor.GetActorType(),actor.GetType()); + actor = null; + } + } + } + public override void Awake() { InitActorRoot(); + RegisterAllTypes(); + _tickRefreshVisible = TimerMgr.Instance.AddTimer(o => { RefreshActorVisible(); }, 1f, true, true); } @@ -156,8 +174,7 @@ namespace TEngine.Runtime.Actor { GameActor ret = null; - GameActor actorSave; - if (_actorPool.TryGetValue(actorID, out actorSave)) + if (_actorPool.ContainsKey(actorID)) { var oldActor = _actorPool[actorID]; var oldActorType = oldActor.GetActorType(); @@ -187,6 +204,8 @@ namespace TEngine.Runtime.Actor { SetMainPlayer(ret); } + + ret.Init(); return ret; }