TEngine--UI模块

UI模块

UI模块一定要先看TE作者A大写的Books/UI,A大写的已经很详细了,这里只做补充说明。

1.搭建UI

按图创建GameObject,必须要在GameObject里添加Canvas,不然会报错。

需要交互的UI还要把GraphicRaycaster加上
添加UItest
UITest组件

接下来按照命名规则搭建UI,并且生成预制件放在Assets/AssetRaw/UI/UITset.prefab

UI预制件

2.生成脚本

在UItest上右键生成脚本粘贴到UITest.cs脚本里(预制件和脚本必须要同名!),在里面边写逻辑代码,最后在添加一些测试脚本。

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
using UnityEngine.UI;
using TEngine;
using TMPro;

namespace GameLogic
{
[Window(UILayer.UI)]
class UITset : UIWindow
{
public static readonly int UITestEventID = 8;
#region 脚本工具生成的代码
private Text m_textTitle;
private TMP_Text m_tmp_textTitle;
private Button m_btnTestButton;
protected override void ScriptGenerator()
{
m_textTitle = FindChildComponent<Text>("m_textTitle");
m_tmp_textTitle = FindChildComponent<TMP_Text>("m_tmp_textTitle");
m_btnTestButton = FindChildComponent<Button>("m_btnTestButton");
m_btnTestButton.onClick.AddListener(OnClickTestButtonBtn);

}
#endregion

#region 事件
protected override void RegisterEvent() {
AddUIEvent(UITestEventID, Tlske);
}

private void OnClickTestButtonBtn()
{
GameEvent.Send(UITestEventID);
}

private void Tlske()
{
Log.Debug("UI测试");
}
#endregion

}
}

然后再想创建UI的脚本里添加GameModule.UI.Show()方法就可以了。

PS:生成脚本近期添加了一个生成脚本的工具窗口,可以用这个来生成脚本。节省创建脚本并粘贴的时间。
在层级窗口右键选择UI,然后选择生成脚本,选择好脚本模板,然后输入脚本名字,点击生成。


3.测试截图

截图

事件截图

UI特性

UI特性目前可以控制UI窗口层级、资源自定义定位、全屏UI、隐藏UI关闭时间。

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
70
71
72
73
74
75
using System;

namespace TEngine
{
/// <summary>
/// UI层级枚举。
/// </summary>
public enum UILayer : int
{
Bottom = 0,
UI = 1,
Top = 2,
Tips = 3,
System = 4,
}

[AttributeUsage(AttributeTargets.Class)]
public class WindowAttribute : Attribute
{
/// <summary>
/// 窗口层级
/// </summary>
public readonly int WindowLayer;

/// <summary>
/// 资源定位地址。
/// </summary>
public readonly string Location;

/// <summary>
/// 全屏窗口标记。
/// </summary>
public readonly bool FullScreen;

/// <summary>
/// 是内部资源无需AB加载。
/// </summary>
public readonly bool FromResources;

public readonly int HideTimeToClose;

public WindowAttribute(int windowLayer, string location = "", bool fullScreen = false, int hideTimeToClose = 10)
{
WindowLayer = windowLayer;
Location = location;
FullScreen = fullScreen;
HideTimeToClose = hideTimeToClose;
}

public WindowAttribute(UILayer windowLayer, string location = "", bool fullScreen = false, int hideTimeToClose = 10)
{
WindowLayer = (int)windowLayer;
Location = location;
FullScreen = fullScreen;
HideTimeToClose = hideTimeToClose;
}

public WindowAttribute(UILayer windowLayer, bool fromResources, bool fullScreen = false, int hideTimeToClose = 10)
{
WindowLayer = (int)windowLayer;
FromResources = fromResources;
FullScreen = fullScreen;
HideTimeToClose = hideTimeToClose;
}

public WindowAttribute(UILayer windowLayer, bool fromResources, string location, bool fullScreen = false, int hideTimeToClose = 10)
{
WindowLayer = (int)windowLayer;
FromResources = fromResources;
Location = location;
FullScreen = fullScreen;
HideTimeToClose = hideTimeToClose;
}
}
}

通过特性的方式来快速定义UIWindow的属性设置。

1
2
3
4
5
[Window(UILayer.UI)]
class UITset : UIWindow
{
//todo
}

其中UI窗口层级这里是以TE内构建栈的方式,控制UI的深度,来起到层级的效果。同一层级同一时间最好不要超过20个,如需更多,自行调试更改WINDOW_DEEP常量和相关源码。

UI传参

在UIModule模块中,ShowUI方法还可以传递参数,我们可以在Assets/GameScripts/Main/Launcher/Scripts/UI/UILoadTip.cs脚本里看到传参的使用方式。就是在ShowUI方法里传递参数,然后通过在UIBase的UserData或者UserDatas字段里获取。

UI类图

UI类图


TEngine--UI模块
https://www.liu2dream.fun/post/TEngine--UI模块/
作者
刘老师 MrLiu
发布于
2024年6月2日
许可协议