初始化事件
1 using ETModel; 2 3 namespace ETHotfix 4 { 5 [Event(EventIdType.InitSceneStart)] 6 public class InitSceneStartEvent:AEvent 7 { 8 public override void Run() 9 { 10 Game.Scene.GetComponent<TKComponent>().Create(TKType.Boot, "TK"); 11 Game.Scene.GetComponent<UIComponent>().Create(UIType.UILoading); 12 } 13 } 14 }
UILoading组件
1 using System; 2 using ETModel; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 namespace ETHotfix 7 { 8 [ObjectSystem] 9 public class UILoadingAwakeSystem: AwakeSystem<UILoadingComponent> 10 { 11 public override void Awake(UILoadingComponent self) 12 { 13 self.Awake(); 14 } 15 } 16 17 [ObjectSystem] 18 public class UILoadingUpdateSystem: UpdateSystem<UILoadingComponent> 19 { 20 public override void Update(UILoadingComponent self) 21 { 22 self.Update(); 23 } 24 } 25 26 [UIFactory(UIType.UILoading)] 27 public class UILoadingFactory: IUIFactory 28 { 29 public UI Create(Scene scene, string type, GameObject parent) 30 { 31 try 32 { 33 ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>(); 34 resourcesComponent.LoadBundle($"{type}.unity3d"); 35 GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset($"{type}.unity3d", $"{type}"); 36 GameObject inst = UnityEngine.Object.Instantiate(bundleGameObject); 37 UI ui = ComponentFactory.Create<UI, GameObject>(inst); 38 39 ui.AddComponent<UILoadingComponent>(); 40 return ui; 41 } 42 catch (Exception e) 43 { 44 Log.Error(e); 45 return null; 46 } 47 } 48 49 public void Remove(string type) 50 { 51 ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"{type}.unity3d"); 52 } 53 } 54 55 public class UILoadingComponent: Component 56 { 57 private Text text; 58 private SceneChangeComponent scc; 59 60 public void Awake() 61 { 62 ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>(); 63 var obj = rc.Get<GameObject>("Text"); 64 this.text = obj.GetComponent<Text>(); 65 this.GetParent<UI>().GameObject.SetActive(false); 66 } 67 68 public void Update() 69 { 70 if (scc != null) this.text.text = scc.Process.ToString(); 71 } 72 73 public void Loading(SceneChangeComponent pscc) 74 { 75 this.scc = pscc; 76 this.GetParent<UI>().GameObject.SetActive(true); 77 } 78 79 public void LoadCompleted() 80 { 81 this.GetParent<UI>().GameObject.SetActive(false); 82 } 83 } 84 }
tkBootComponent中的场景切换代码
1 using (SceneChangeComponent scc = ETModel.ComponentFactory.Create<SceneChangeComponent>()) 2 { 3 Game.Scene.GetComponent<UIComponent>().Get(UIType.UILoading).GetComponent<UILoadingComponent>().Loading(scc); 4 await scc.ChangeSceneAsync(ETModel.SceneType.main); 5 scc.tcs.Task.GetAwaiter().OnCompleted(() => Game.Scene.GetComponent<UIComponent>().Get(UIType.UILoading).GetComponent<UILoadingComponent>().LoadCompleted()); 6 } 7 Game.EventSystem.Run(EventIdType.MainSceneStart);
tkBootComponent
1 using System; 2 using System.Threading.Tasks; 3 using DG.Tweening; 4 using ETModel; 5 using UnityEngine; 6 using UnityEngine.UI; 7 8 namespace ETHotfix 9 { 10 [ObjectSystem] 11 public class tkBootComponentAwakeSystem: AwakeSystem<tkBootComponent> 12 { 13 public override void Awake(tkBootComponent self) 14 { 15 self.Awake(); 16 } 17 } 18 19 [TKFactory(TKType.Boot)] 20 public class tkBootComponentFactory: ITKFactory 21 { 22 public TK Create(Scene scene, string type, GameObject parent) 23 { 24 try 25 { 26 ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>(); 27 resourcesComponent.LoadBundle($"{type}.unity3d"); 28 GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset($"{type}.unity3d", $"{type}"); 29 GameObject inst = UnityEngine.Object.Instantiate(bundleGameObject); 30 TK tk = ComponentFactory.Create<TK, GameObject>(inst); 31 32 tk.AddComponent<tkBootComponent>(); 33 return tk; 34 } 35 catch (Exception e) 36 { 37 Log.Error(e); 38 return null; 39 } 40 } 41 42 public void Remove(string type) 43 { 44 ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"{type}.unity3d"); 45 } 46 } 47 48 public class tkBootComponent: Component 49 { 50 private GameObject bright; 51 private GameObject info; 52 private GameObject vsn; 53 private GameObject vmn; 54 private GameObject vver; 55 private GameObject date; 56 57 public async void Awake() 58 { 59 ReferenceCollector rc = this.GetParent<TK>().gameObject.GetComponent<ReferenceCollector>(); 60 // 测试动态添加Mono 61 //this.GetParent<TK>().gameObject.AddComponent<ScrollBackground>(); 62 63 this.bright = rc.Get<GameObject>("bright"); 64 this.info = rc.Get<GameObject>("Info"); 65 this.vsn = rc.Get<GameObject>("vsn"); 66 this.vmn = rc.Get<GameObject>("vmn"); 67 this.vver = rc.Get<GameObject>("vver"); 68 this.date = rc.Get<GameObject>("date"); 69 70 Image brightImage = this.bright.GetComponent<Image>(); 71 // Logo图片亮起来 72 brightImage.DOFade(1f, 3f).SetEase(Ease.Linear); 73 await Task.Delay(3000); 74 this.bright.transform.parent.gameObject.SetActive(false); 75 76 this.info.SetActive(true); 77 await Task.Delay(6000); 78 79 // 测试访问 80 Log.Info(Game.Scene.GetComponent<TKComponent>().Get(TKType.Boot).gameObject.name); 81 Log.Info(Game.Scene.GetComponent<TKComponent>().Get(TKType.Boot).GetComponent<tkBootComponent>().InstanceId.ToString()); 82 83 // 销毁GameObject 84 //this.GetParent<TK>().Dispose(); 85 // 切换场景到main 86 using (SceneChangeComponent scc = ETModel.ComponentFactory.Create<SceneChangeComponent>()) 87 { 88 Game.Scene.GetComponent<UIComponent>().Get(UIType.UILoading).GetComponent<UILoadingComponent>().Loading(scc); 89 await scc.ChangeSceneAsync(ETModel.SceneType.main); 90 scc.tcs.Task.GetAwaiter().OnCompleted(() => Game.Scene.GetComponent<UIComponent>().Get(UIType.UILoading).GetComponent<UILoadingComponent>().LoadCompleted()); 91 } 92 Game.EventSystem.Run(EventIdType.MainSceneStart); 93 } 94 } 95 }
MainSceneStartEvent
1 using ETModel; 2 3 namespace ETHotfix 4 { 5 [Event(EventIdType.MainSceneStart)] 6 public class MainSceneStartEvent: AEvent 7 { 8 public override void Run() 9 { 10 Game.Scene.GetComponent<TKComponent>().Create(TKType.ScrollBackground, "TK"); 11 } 12 } 13 }
tkScrollBackgroundComponent
1 using System; 2 using ETModel; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 namespace ETHotfix 7 { 8 [ObjectSystem] 9 public class tkScrollBackgroundComponentAwakeSystem: AwakeSystem<tkScrollBackgroundComponent> 10 { 11 public override void Awake(tkScrollBackgroundComponent self) 12 { 13 self.Awake(); 14 } 15 } 16 17 [TKFactory(TKType.ScrollBackground)] 18 public class tkScrollBackgroundComponentFactory: ITKFactory 19 { 20 public TK Create(Scene scene, string type, GameObject parent) 21 { 22 try 23 { 24 ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>(); 25 resourcesComponent.LoadBundle($"{type}.unity3d"); 26 GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset($"{type}.unity3d", $"{type}"); 27 GameObject inst = UnityEngine.Object.Instantiate(bundleGameObject); 28 TK tk = ComponentFactory.Create<TK, GameObject>(inst); 29 30 tk.AddComponent<tkScrollBackgroundComponent>(); 31 return tk; 32 } 33 catch (Exception e) 34 { 35 Log.Error(e); 36 return null; 37 } 38 } 39 40 public void Remove(string type) 41 { 42 ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"{type}.unity3d"); 43 } 44 } 45 46 public class tkScrollBackgroundComponent: Component 47 { 48 public void Awake() 49 { 50 51 } 52 } 53 }
通过熊猫的指导,代码有两处地方更改,同时对ComponentFactory理明了了:
// 切换场景到main using (SceneChangeComponent scc = ETModel.ComponentFactory.Create<SceneChangeComponent>()) { ETModel.Game.Scene.AddComponent(scc); Game.Scene.GetComponent<UIComponent>().Get(UIType.UILoading).GetComponent<UILoadingComponent>().Loading(); await scc.ChangeSceneAsync(ETModel.SceneType.main); scc.tcs.Task.GetAwaiter().OnCompleted(() => { Game.Scene.GetComponent<UIComponent>().Get(UIType.UILoading) .GetComponent<UILoadingComponent>().LoadCompleted(); // Mian场景启动事件 Game.EventSystem.Run(EventIdType.MainSceneStart); }); }
1 //public void Loading(SceneChangeComponent pscc) 2 public void Loading() 3 { 4 //this.scc = pscc; 5 this.scc = ETModel.Game.Scene.GetComponent<SceneChangeComponent>(); 6 this.GetParent<UI>().GameObject.SetActive(true); 7 } 8 9 public void LoadCompleted() 10 { 11 this.GetParent<UI>().GameObject.SetActive(false); 12 ETModel.Game.Scene.RemoveComponent<SceneChangeComponent>(); 13 }
ps:可能存在理解不当的地方,欢迎留言指正!