UIFramework学习记录

UI自适应

game窗口选择FreeAspect 模式,然后调整CanvasScaler模式为 ScaleWitchScreenSize,根据分辨率大小来自动调整UI,Mach属性选择Height

面板的管理

  • 首先需要知道有哪些面板
  • 通过json信息来保存面板的路径
  • 面板的类型 (UIType)
  • UI面板的核心管理类(UIManager)
    • 解析保存所有面板信息(PanelPathDict)
    • 创建保存所有面板的示例(PanelDict)
    • 管理保存所有显示的面板
  • 公共基类BasePanel,各个子面板继承于BasePanel
  • GameRoot,负责UI框架的启动

单例模式

  • 定义一个静态对象,在外界访问,在内部构造
  • 构造方法私有化
    private static UIManager _instance;  //存储实例的静态变量

    public static UIManager Instance  //对外界提供的get方法
    {
        get
        {
            if (_instance == null)
            {
                _instance = new UIManager();
            }
            return _instance;
        }
    }

解析带有枚举的类型的json


public class UIPanelInfo  //存储面板信息的model
{
    public UIPanelType panelTypeEnum;  //枚举
    public string panelType
    {
        get { return panelTypeEnum.ToString(); }
        set
        {
            UIPanelType type = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), value);  //将传入的string转换为枚举的类型
            panelTypeEnum = type;
        }
    }
    public string path;
}

扩展方法

  • 拓展方法和类型都必须是static类型
  • this必须有
  • this后面是需要扩展的类型
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 对Dictionary的扩展
/// </summary>
public static class DictionaryExtension  {

    /// <summary>
    /// 尝试根据key得到value,得到直接返回value,没有得到返回null
    /// </summary>

    public static Tvalue TryGet<Tkey,Tvalue>(this Dictionary<Tkey,Tvalue> dict,Tkey key)
    {
        Tvalue value;
        dict.TryGetValue(key, out value);
        return value;
    }
}

调用:

        //BasePanel panel;
        //panelDict.TryGetValue(paneltype, out panel);

        BasePanel panel = panelDict.TryGet(paneltype);  //如果找到面板直接存储到panel里面

go.transform.SetParent(canvas,false); //false参数控制实例化的面板按照原来的大小显示

UI面板的生命周期

  • 界面显示(OnEnter)
  • 界面暂停(OnPause),弹出其他界面
  • 界面继续(OnResume),其他界面的移除,恢复本界面的交互
  • 界面移除(OnExit),页面移除不在显示的

CanvasGroup属性,可以设置页面的交互性

猜你喜欢

转载自blog.csdn.net/gsm958708323/article/details/79109716