案例 : Assets/Scripts/Example/3.UIFrame/UIPanel/UIExample.unity
1).UI面板脚本
- 继承自UIPanelParent
- 打开UI面板需要传参,可新建一个类继承UIDataParent,比如UIChildData(在UIChild脚本里)
- 使用AFUI特性标记UI脚本,定义脚本对应的UI面板Tag以及加载路径/是否添加AFStart中的LanuageSuffix,请注意加载路径不需要加.prefab后缀
如果你的UI面板需要适配多语言,即一种语言有一个后缀(一般是因为适配和字体导致一种语言一套UI),AFUI的第三个参数isNeedLanuageSuffix请设置为true,调用AFStart.Instance.ChangeLanuageSuffix修改语言后缀时会自动同步修改完整加载路径
最终的加载路径相对于Assets并且带有后缀是从AB包里加载,相对于Resources且不带有后缀则是从Resources路径加载
- 使用AFTransformPath特性自动获取UI组件
- CreateSubPanel可以用来打开子面板
案例中父脚本UIABLoadTestPre:
using AFramework;
[AFUI((int)testUIEnum.ABLoadTestPre, "Assets/ResForAB/ABMain/Prefabs/ABLoadTestPre")]
public class UIABLoadTestPre : UIPanelParent
{
//第一次初始化调用
protected override void InitUI(UIDataParent UIDataParent = null)
{
base.InitUI(UIDataParent);
AF_Logger.Debug("UIABLoadTestPre InitUI");
UIChildData uIChildData = new UIChildData();
//初始化子物体
for (int i = 0; i < 3; i++)
{
//TODO : transformPath完成替换
uIChildData.index = i;
CreateSubPanel<UIChild>((int)testChildUIEnum.UIChild,
transform.GetChild(1).GetChild(0).GetChild(0), UIDataParent: uIChildData, this);
}
}
protected override void InitUIAll(UIDataParent UIDataParent = null)
{
base.InitUIAll(UIDataParent);
AF_Logger.Debug("UIABLoadTestPre InitUIAll");
}
protected override void RegisterUIEvent()
{
base.RegisterUIEvent();
AF_Logger.Debug("UIABLoadTestPre RegisterUIEvent");
}
public override void RefreshUIByData(UIDataParent UIDataParent = null)
{
base.RefreshUIByData(UIDataParent);
AF_Logger.Debug("UIABLoadTestPre RefreshUIByData");
}
}
public enum testUIEnum
{
ABLoadTestPre
}
public enum testChildUIEnum
{
UIChild = 3000
}
子脚本UIChild :
using AFramework;
using UnityEngine.UI;
public class UIChildData : UIDataParent
{
public int index;
}
[AFUI((int)testChildUIEnum.UIChild, "Assets/ResForAB/ABMain/Prefabs/ChildUI")]
public class UIChild : UIPanelParent
{
[AFTransformPath("Text")]
Text indexText;
protected override void InitUI(UIDataParent UIDataParent = null)
{
base.InitUI(UIDataParent);
AF_Logger.Debug("UIChild InitUI");
if (UIDataParent == null)
return;
UIChildData uIChildData = UIDataParent as UIChildData;
AF_Logger.Debug(transform);
indexText.text = "子物体:" + uIChildData.index.ToString();
}
protected override void InitUIAll(UIDataParent UIDataParent = null)
{
base.InitUIAll(UIDataParent);
AF_Logger.Debug("UIChild InitUIAll");
}
protected override void RegisterUIEvent()
{
base.RegisterUIEvent();
AF_Logger.Debug("UIChild RegisterUIEvent");
}
public override void RefreshUIByData(UIDataParent UIDataParent = null)
{
base.RefreshUIByData(UIDataParent);
AF_Logger.Debug("UIChild RefreshUIByData");
}
}
2).打开UI面板
UIManager.Instance.OpenUI<UIABLoadTestPre>((int)testUIEnum.ABLoadTestPre,
null,panelLayer:PanelLayer.Load,isClear:true)