案例 : 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:

  1. using AFramework;
  2. [AFUI((int)testUIEnum.ABLoadTestPre, "Assets/ResForAB/ABMain/Prefabs/ABLoadTestPre")]
  3. public class UIABLoadTestPre : UIPanelParent
  4. {
  5. //第一次初始化调用
  6. protected override void InitUI(UIDataParent UIDataParent = null)
  7. {
  8. base.InitUI(UIDataParent);
  9. AF_Logger.Debug("UIABLoadTestPre InitUI");
  10. UIChildData uIChildData = new UIChildData();
  11. //初始化子物体
  12. for (int i = 0; i < 3; i++)
  13. {
  14. //TODO : transformPath完成替换
  15. uIChildData.index = i;
  16. CreateSubPanel<UIChild>((int)testChildUIEnum.UIChild,
  17. transform.GetChild(1).GetChild(0).GetChild(0), UIDataParent: uIChildData, this);
  18. }
  19. }
  20. protected override void InitUIAll(UIDataParent UIDataParent = null)
  21. {
  22. base.InitUIAll(UIDataParent);
  23. AF_Logger.Debug("UIABLoadTestPre InitUIAll");
  24. }
  25. protected override void RegisterUIEvent()
  26. {
  27. base.RegisterUIEvent();
  28. AF_Logger.Debug("UIABLoadTestPre RegisterUIEvent");
  29. }
  30. public override void RefreshUIByData(UIDataParent UIDataParent = null)
  31. {
  32. base.RefreshUIByData(UIDataParent);
  33. AF_Logger.Debug("UIABLoadTestPre RefreshUIByData");
  34. }
  35. }
  36. public enum testUIEnum
  37. {
  38. ABLoadTestPre
  39. }
  40. public enum testChildUIEnum
  41. {
  42. UIChild = 3000
  43. }

子脚本UIChild :

  1. using AFramework;
  2. using UnityEngine.UI;
  3. public class UIChildData : UIDataParent
  4. {
  5. public int index;
  6. }
  7. [AFUI((int)testChildUIEnum.UIChild, "Assets/ResForAB/ABMain/Prefabs/ChildUI")]
  8. public class UIChild : UIPanelParent
  9. {
  10. [AFTransformPath("Text")]
  11. Text indexText;
  12. protected override void InitUI(UIDataParent UIDataParent = null)
  13. {
  14. base.InitUI(UIDataParent);
  15. AF_Logger.Debug("UIChild InitUI");
  16. if (UIDataParent == null)
  17. return;
  18. UIChildData uIChildData = UIDataParent as UIChildData;
  19. AF_Logger.Debug(transform);
  20. indexText.text = "子物体:" + uIChildData.index.ToString();
  21. }
  22. protected override void InitUIAll(UIDataParent UIDataParent = null)
  23. {
  24. base.InitUIAll(UIDataParent);
  25. AF_Logger.Debug("UIChild InitUIAll");
  26. }
  27. protected override void RegisterUIEvent()
  28. {
  29. base.RegisterUIEvent();
  30. AF_Logger.Debug("UIChild RegisterUIEvent");
  31. }
  32. public override void RefreshUIByData(UIDataParent UIDataParent = null)
  33. {
  34. base.RefreshUIByData(UIDataParent);
  35. AF_Logger.Debug("UIChild RefreshUIByData");
  36. }
  37. }

2).打开UI面板

  1. UIManager.Instance.OpenUI<UIABLoadTestPre>((int)testUIEnum.ABLoadTestPre,
  2. null,panelLayer:PanelLayer.Load,isClear:true)