Unity进阶之ET网络游戏开发框架 03-Hotfix层启动

时间:2019-08-19 12:45:01   收藏:0   阅读:163

版权申明:

  1. 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123
  2. 优梦创客的官方博客:https://91make.top
  3. 优梦创客的游戏讲堂:https://91make.ke.qq.com
  4. 『优梦创客』的微信公众号:umaketop

概要

先上Hotfix入口代码:

public void LoadHotfixAssembly()
{
    Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
    GameObject code = (GameObject)Game.Scene.GetComponent<ResourcesComponent>().GetAsset("code.unity3d", "Code");
    
    byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
    byte[] pdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes;
    
#if ILRuntime
    Log.Debug($"当前使用的是ILRuntime模式");
    // ...
#else
    Log.Debug($"当前使用的是Mono模式");

    this.assembly = Assembly.Load(assBytes, pdbBytes);

    Type hotfixInit = this.assembly.GetType("ETHotfix.Init");
    this.start = new MonoStaticMethod(hotfixInit, "Start");
    
    this.hotfixTypes = this.assembly.GetTypes().ToList();
#endif
    
    Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
}

public void GotoHotfix()
{
#if ILRuntime
    ILHelper.InitILRuntime(this.appDomain);
#endif
    this.start.Run();
}

ETHotfix.Init.Start():

namespace ETHotfix
{
    public static class Init
    {
        public static void Start()
        {
#if ILRuntime
            if (!Define.IsILRuntime)
            {
                Log.Error("Model层是mono模式, 但是Hotfix层是ILRuntime模式");
            }
#else
            if (Define.IsILRuntime)
            {
                Log.Error("Model层是ILRuntime模式, Hotfix层是mono模式");
            }
#endif
            
            try
            {
                // 注册热更层回调
                ETModel.Game.Hotfix.Update = () => { Update(); };
                ETModel.Game.Hotfix.LateUpdate = () => { LateUpdate(); };
                ETModel.Game.Hotfix.OnApplicationQuit = () => { OnApplicationQuit(); };
                
                Game.Scene.AddComponent<UIComponent>();
                Game.Scene.AddComponent<OpcodeTypeComponent>();
                Game.Scene.AddComponent<MessageDispatcherComponent>();

                // 加载热更配置
                ETModel.Game.Scene.GetComponent<ResourcesComponent>().LoadBundle("config.unity3d");
                Game.Scene.AddComponent<ConfigComponent>();
                ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle("config.unity3d");

                UnitConfig unitConfig = (UnitConfig)Game.Scene.GetComponent<ConfigComponent>().Get(typeof(UnitConfig), 1001);
                Log.Debug($"config {JsonHelper.ToJson(unitConfig)}");

                Game.EventSystem.Run(EventIdType.InitSceneStart);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
    }
}

InitSceneStart_CreateLoginUI.Run():

namespace ETHotfix
{
    [Event(EventIdType.InitSceneStart)]
    public class InitSceneStart_CreateLoginUI: AEvent
    {
        public override void Run()
        {
            UI ui = UILoginFactory.Create(); // 注意:这次是调用Hotfix层的UILoginFactory的Create工厂方法来创建UI实体,下面会重点分析
            Game.Scene.GetComponent<UIComponent>().Add(ui); // 注意:这次是添加Hotfix层的UIComponent组件,其代码与模型层类似,不再赘述
        }
    }
}

UILoginFactory.Create():

```csharp
namespace ETHotfix
{
public static class UILoginFactory
{
public static UI Create()
{
try
{
ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent();
resourcesComponent.LoadBundle(UIType.UILogin.StringToAB());
GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset(UIType.UILogin.StringToAB(), UIType.UILogin);
GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject);

            UI ui = ComponentFactory.Create<UI, string, GameObject>(UIType.UILogin, gameObject, false);

            ui.AddComponent<UILoginComponent>();
            return ui;
        }
        catch (Exception e)
        {
            Log.Error(e);
            return null;
        }
    }
}

}```

UILoginComponent事件处理:

namespace ETHotfix
{
    [ObjectSystem]
    public class UiLoginComponentSystem : AwakeSystem<UILoginComponent>
    {
        public override void Awake(UILoginComponent self)
        {
            self.Awake();
        }
    }
    
    public class UILoginComponent: Component
    {
        private GameObject account;
        private GameObject loginBtn;

        public void Awake()
        {
            ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
            loginBtn = rc.Get<GameObject>("LoginBtn");
            loginBtn.GetComponent<Button>().onClick.Add(OnLogin);
            this.account = rc.Get<GameObject>("Account");
        }

        public void OnLogin()
        {
            LoginHelper.OnLoginAsync(this.account.GetComponent<InputField>().text).Coroutine();
        }
    }
}

原文:https://www.cnblogs.com/raymondking123/p/11369644.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!