EventSystem.Run(EventIdType.LoadingBegin)
public static class BundleHelper{ public static async ETTask DownloadBundle() { if (Define.IsAsync) { try { using (BundleDownloaderComponent bundleDownloaderComponent = Game.Scene.AddComponent<BundleDownloaderComponent>()) { await bundleDownloaderComponent.StartAsync(); Debug.Log("EventIdType.LoadingBegin"); Game.EventSystem.Run(EventIdType.LoadingBegin); await bundleDownloaderComponent.DownloadAsync(); } Game.EventSystem.Run(EventIdType.LoadingFinish); Game.Scene.GetComponent<ResourcesComponent>().LoadOneBundle("StreamingAssets"); ResourcesComponent.AssetBundleManifestObject = (AssetBundleManifest)Game.Scene.GetComponent<ResourcesComponent>().GetAsset("StreamingAssets", "AssetBundleManifest"); } catch (Exception e) { Log.Error(e); } } }}
public static class BundleHelper
{
public static async ETTask DownloadBundle()
if (Define.IsAsync)
try
using (BundleDownloaderComponent bundleDownloaderComponent = Game.Scene.AddComponent<BundleDownloaderComponent>())
await bundleDownloaderComponent.StartAsync();
Debug.Log("EventIdType.LoadingBegin");
Game.EventSystem.Run(EventIdType.LoadingBegin);
await bundleDownloaderComponent.DownloadAsync();
}
Game.EventSystem.Run(EventIdType.LoadingFinish);
Game.Scene.GetComponent<ResourcesComponent>().LoadOneBundle("StreamingAssets");
ResourcesComponent.AssetBundleManifestObject = (AssetBundleManifest)Game.Scene.GetComponent<ResourcesComponent>().GetAsset("StreamingAssets", "AssetBundleManifest");
catch (Exception e)
Log.Error(e);
实现LoadingBegin事件处理程序:
[Event(EventIdType.LoadingBegin)] public class LoadingBeginEvent_CreateLoadingUI : AEvent { public override void Run() { UI ui = UILoadingFactory.Create(); Game.Scene.GetComponent<UIComponent>().Add(ui); } }
[Event(EventIdType.LoadingBegin)]
public class LoadingBeginEvent_CreateLoadingUI : AEvent
public override void Run()
UI ui = UILoadingFactory.Create();
Game.Scene.GetComponent<UIComponent>().Add(ui);
在这里有需要注意学习定义事件类的方法: 1. 为一个类添加Event标志(参数填具体事件类型) 2. 从AEvent继承 3. 此时,ET就会自动将该类识别为一个事件处理类(通过反射机制),并在EventSystem.Run被调用时执行LoadingBeginEvent_CreateLoadingUI事件类的Run方法!
EventSystem.Run
第六行代码UILoadingFactory.Create()负责创建UILoading界面,下面代码加了注释:
UILoadingFactory.Create()
public static class UILoadingFactory { public static UI Create() { try { // KV是Resources文件夹下存储的本地预制体资源,主要存储一些键值对数据 // 从KV加载UIType.UILoading预制体,并实例化UI对象: GameObject bundleGameObject = ((GameObject)ResourcesHelper.Load("KV")).Get<GameObject>(UIType.UILoading); GameObject go = UnityEngine.Object.Instantiate(bundleGameObject); go.layer = LayerMask.NameToLayer(LayerNames.UI); // 创建UI这个Entity,并将上面创建的UI对象作为该Entity的图形表示 UI ui = ComponentFactory.Create<UI, string, GameObject>(UIType.UILoading, go, false); // 添加UILoadingComponent,该组件负责更新loading进度并刷新显示 ui.AddComponent<UILoadingComponent>(); return ui; } catch (Exception e) { Log.Error(e); return null; } } }
public static class UILoadingFactory
public static UI Create()
// KV是Resources文件夹下存储的本地预制体资源,主要存储一些键值对数据
// 从KV加载UIType.UILoading预制体,并实例化UI对象:
GameObject bundleGameObject = ((GameObject)ResourcesHelper.Load("KV")).Get<GameObject>(UIType.UILoading);
GameObject go = UnityEngine.Object.Instantiate(bundleGameObject);
go.layer = LayerMask.NameToLayer(LayerNames.UI);
// 创建UI这个Entity,并将上面创建的UI对象作为该Entity的图形表示
UI ui = ComponentFactory.Create<UI, string, GameObject>(UIType.UILoading, go, false);
// 添加UILoadingComponent,该组件负责更新loading进度并刷新显示
ui.AddComponent<UILoadingComponent>();
return ui;
return null;
说明: - UI类是一个Entity类,Entity间接从Component类继承,但只有Entity类可以附加组件,Component类不行 - Entity和Component的关系实际就是设计模式中的Composite模式 - UI类可以复用,当你要创建一个UI时,在ET框架下只要: - 添加一个static的UI工厂类,并在其中定义一个static的Create方法,具体实现参照UILoadingFactory - 为该工厂添加一个新的UI组件(从Component类继承),并实现该组件的事件系统(见下文)
实现UILoadingComponent并实现该组件的事件系统:
public class UILoadingComponent : Component { public Text text; }
public class UILoadingComponent : Component
public Text text;
[ObjectSystem] public class UiLoadingComponentAwakeSystem : AwakeSystem<UILoadingComponent> { public override void Awake(UILoadingComponent self) { self.text = self.GetParent<UI>().GameObject.Get<GameObject>("Text").GetComponent<Text>(); } } [ObjectSystem] public class UiLoadingComponentStartSystem : StartSystem<UILoadingComponent> { public override void Start(UILoadingComponent self) { StartAsync(self).Coroutine(); } public async ETVoid StartAsync(UILoadingComponent self) { TimerComponent timerComponent = Game.Scene.GetComponent<TimerComponent>(); long instanceId = self.InstanceId; while (true) { await timerComponent.WaitAsync(1000); if (self.InstanceId != instanceId) { return; } BundleDownloaderComponent bundleDownloaderComponent = Game.Scene.GetComponent<BundleDownloaderComponent>(); if (bundleDownloaderComponent == null) { continue; } self.text.text = $"{bundleDownloaderComponent.Progress}%"; } } }
[ObjectSystem]
public class UiLoadingComponentAwakeSystem : AwakeSystem<UILoadingComponent>
public override void Awake(UILoadingComponent self)
self.text = self.GetParent<UI>().GameObject.Get<GameObject>("Text").GetComponent<Text>();
public class UiLoadingComponentStartSystem : StartSystem<UILoadingComponent>
public override void Start(UILoadingComponent self)
StartAsync(self).Coroutine();
public async ETVoid StartAsync(UILoadingComponent self)
TimerComponent timerComponent = Game.Scene.GetComponent<TimerComponent>();
long instanceId = self.InstanceId;
while (true)
await timerComponent.WaitAsync(1000);
if (self.InstanceId != instanceId)
return;
BundleDownloaderComponent bundleDownloaderComponent = Game.Scene.GetComponent<BundleDownloaderComponent>();
if (bundleDownloaderComponent == null)
continue;
self.text.text = $"{bundleDownloaderComponent.Progress}%";
事件类的定义: 1. 添加[ObjectSystem]标志 2. 继承自对应的XxxSystem类,并实现基类的虚方法 - 事件类与Unity中含义类似,请自行参阅源码学习
原文链接:http://www.cnblogs.com/raymondking123/p/11369623.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728