TinaX.Core
# 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();
});
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- 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;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 事件消息 XEvent
用于管理模块间消息通知传递
- 事件注册接口
public static IEventTicket Register(string EventName, Action<object> handler, string EventGroup = DefaultGroup)
1
- 事件广播接口
public static void Call(string eventName, object param = null, string eventGroup = DefaultGroup)
1
- 事件移除接口
public static void Remove(Action<object> handler)
public static void Remove(Action<object> handler, string eventName, string eventGroup = DefaultGroup)
1
2
2
- 代码
//事件注册
XEvent.Register("Framework_Init_Completed", _=>{
var canvas = transform.GetChild(0);
canvas.gameObject.Destroy();
});
//事件广播
XEvent.Call("Framework_Init_Completed");
1
2
3
4
5
6
7
2
3
4
5
6
7
# 时间机器 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)
1
2
3
2
3
- LateUpdate
public static ITimeTicket RegisterLateUpdate(Action lateupdateAction, int order = 0)
public static void RemoveLateUpdate(Action lateupdateAction)
public static void RemoveLateUpdate(int action_id)
1
2
3
2
3
- FixedUpdate
public static ITimeTicket RegisterFixedUpdate(Action fixedupdateAction, int order = 0)
public static void RemoveFixedUpdate(Action fixedupdateAction)
public static void RemoveFixedUpdate(int action_id)
1
2
3
2
3
# 资产释放组 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();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- 扩展
//实现
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));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 依赖注入
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));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 2023/10/17, 14:09:52 访问次数: 0