TinaX.Core

简介

TinaX.Core 是TinaX的核心内容包.

框架核心 控制反转容器 (IoC) 事件广播系统 时间驱动系统 常用方法扩展

package name: io.nekonya.tinax.core

启动

  • GameStartup.cs 创建框架并启动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TinaX;
using TinaX.Services;
using TinaX.UIKit;
namespace RO
{
    public class GameStartup : MonoBehaviour
    {
        private async void Start()
        {
            var core = XCore.New()
                .UseVFS().UseI18N().UseUIKit().UseLuaRuntime()
                .UseSDK(new SDKDelegate())
                .UseDownloader()
                .UseNet()
                .OnServicesStartException((service, error) =>
                {

                });
            await core.RunAsync();
            XEvent.Register("Framework_Init_Completed", _=>{
                var canvas = transform.GetChild(0);
                canvas.gameObject.Destroy();
            });
        }
    }

}
  • GameBootstrap.cs : 框架入口类
using System;
using System.IO;
using System.Text;
using Cysharp.Threading.Tasks;
using Google.Protobuf;
using RO.Net;
using TinaX;
using TinaX.Lua;
using TinaX.Net;
using TinaX.UIKit;
using TinaX.Utils;
using UnityEngine;
namespace RO
{
    /// <summary>
    /// 推荐作为Game的程序入口。该class会被TinaX反射寻找,可以同时存在多个
    /// </summary>
    public class GameBootstrap : IXBootstrap
    {
        public void OnAppRestart()
        {

        }

        public void OnInit(IXCore core)
        {
            //Debug.Log("GameBootstrap:OnInit()");
            var service = core.Services.Get<ILua>();
            service.LuaVM.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);
            service.LuaVM.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf);
            //service.LuaVM.AddBuildin("protobuf.c", XLua.LuaDLL.Lua.LoadProtobufC);
            // 用于LuaPerfect调试, 详见https://luaperfect.net/
            // For LuaPerfect Debugging
            // If you don't use luaperfect, you can delete the code
            /*
            service.ConfigureCustomLoadHandler(options =>
            {
                options.Add("LuaDebuggee", () => null);
            });
            */
        }

        public void OnQuit()
        {

        }

        public void OnStart(IXCore core)
        {
            Debug.Log("GameBootstrap:OnStart()");
            var net = core.Services.Get<INet>();
            net.AddResponseCallback<TheMsg>(0x1001, (resp) =>
            {
                Debug.Log(resp);
            });
        }
        private async void DoSomething()
        {
            var rets = await DoTask();
            Debug.Log($"DoTask=>{rets}");

        }
        private async UniTask<int> DoTask()
        {
            await UniTask.WaitForEndOfFrame();
            return 100;
        }
    }
}

事件消息 XEvent

用于管理模块间消息通知传递

  • 事件注册接口
public static IEventTicket Register(string EventName, Action<object> handler, string EventGroup = DefaultGroup)
  • 事件广播接口
public static void Call(string eventName, object param = null, string eventGroup = DefaultGroup)
  • 事件移除接口
public static void Remove(Action<object> handler)
public static void Remove(Action<object> handler, string eventName, string eventGroup = DefaultGroup)
  • 代码
//事件注册
XEvent.Register("Framework_Init_Completed", _=>{
                var canvas = transform.GetChild(0);
                canvas.gameObject.Destroy();
            });
//事件广播
XEvent.Call("Framework_Init_Completed");

时间机器 TimeMachine

用于管理Update LateUpdate FixedUpdate更新

  • Update
public static ITimeTicket RegisterUpdate(Action updateAction, int order = 0)
public static void RemoveUpdate(Action updateAction)
public static void RemoveUpdate(int action_id)
  • LateUpdate
public static ITimeTicket RegisterLateUpdate(Action lateupdateAction, int order = 0)
public static void RemoveLateUpdate(Action lateupdateAction)
public static void RemoveLateUpdate(int action_id)
  • FixedUpdate
public static ITimeTicket RegisterFixedUpdate(Action fixedupdateAction, int order = 0)
public static void RemoveFixedUpdate(Action fixedupdateAction)
public static void RemoveFixedUpdate(int action_id)

资产释放组 DisposableGroup

以组的形式统一释放资源

  • 代码

//正常模式
var event1 = XEvent.Register("EventName1",action1);
var event2 = XEvent.Register("EventName2",action2);

event2.Unregister()或XEvent.Remove(action2,"EventName2");
event1.Unregister()或XEvent.Remove(action1,"EventName1");

///DisposableGroup模式
var disposableGroup = new DisposableGroup();
disposableGroup.RegisterEvent("EventName1",aciton1);
disposableGroup.RegisterEvent("EventName2",aciton2);

disposableGroup.Dispose();
  • 扩展
//实现
namespace TinaX.Systems
{
    public interface ITimeTicket : IDisposable
    {
        int? id { get; }
        TimeTicket.TicketType type { get; }

        void Unregister();
    }
}
//扩展
namespace TinaX
{
    public static class TimeMachineExtend
    {
        public static DisposableGroup RegisterUpdate(this DisposableGroup dg, Action updateAction, int order = 0)
            => dg.Register(
                TimeMachine.RegisterUpdate(updateAction, order));

        public static DisposableGroup RegisterLateUpdate(this DisposableGroup dg, Action lateupdateAction, int order = 0)
            => dg.Register(
                TimeMachine.RegisterLateUpdate(lateupdateAction, order));

        public static DisposableGroup RegisterFixedUpdate(this DisposableGroup dg, Action fixedupdateAction, int order = 0)
            => dg.Register(
                TimeMachine.RegisterFixedUpdate(fixedupdateAction, order));
    }
}

依赖注入

using UnityEngine;
namespace TinaX.SDK.Internal
{
    public class SDKBehaviour : MonoBehaviour
    {
        [Inject]
        public ISDKInternal SDK { get; set; }
        public void OnNativeCallback(string args)
        {
            SDK?.OnNativeCallback(args);
        }
    }
}

var behaviour = gameObject.AddComponent<SDKBehaviour>();
Core.InjectObject(behaviour);

Core.CreateInstance(typeof(SDKBehaviour));

Core.CreateInstanceAndInject(typeof(SDKBehaviour));
© 2017~ 随风逐叶 all right reserved,powered by Gitbook文章修订时间: 2021-07-14 18:19:25

results matching ""

    No results matching ""