Пример #1
0
    // 获取指定Layer当前最大的sortingOrder
    public int GetMaxSortOrder(SortOrderLayer layerType, string name)
    {
        int maxOrder = (int)layerType;

//        Debug.Log("----> Start order: " + maxOrder);
        foreach (var uiPair in _uiDic)
        {
            if (maxOrder <= uiPair.Value.SortOrder && name != uiPair.Key)
            {
//                Debug.LogFormat("----> Compare Order: {0}/{1} ", maxOrder, uiPair.Value.SortOrder);
                // 每新加一个界面,sortingOrder会根据当前层级增加
                maxOrder = uiPair.Value.SortOrder + (int)layerType;
            }
        }
//        Debug.Log("----> Result order: " + maxOrder);
        return(maxOrder);
    }
Пример #2
0
    /// <summary>
    /// 打开一个界面
    /// </summary>
    public T Show <T>(OpenType openType = OpenType.Add, SortOrderLayer layer = SortOrderLayer.HomePanel) where T : UIBase
    {
        string uiName = typeof(T).ToString();
        UIBase uiBase;

        if (_uiDic.ContainsKey(uiName))
        {
            // 界面已经存在,从字典中取出,Active设置为true
            // 且会重新计算层级,且会刷新界面
            uiBase = _uiDic[uiName];
            uiBase.gameObject.SetActive(true);
            uiBase.Refresh();
        }
        else
        {
            // 新开界面
            GameObject uiGO = SimpleLoader.InstantiateGameObject(_uiRootPath + uiName + ".prefab");
            uiGO.transform.SetParent(_canvasGameObject.transform, false);
            uiBase = uiGO.GetComponent <UIBase>();
        }
        uiBase.UIName     = uiName;
        uiBase.OrderLayer = layer;

        // 替换形式会关闭上个界面
        if (openType == OpenType.Replace)
        {
            CloseTopUI();
        }

        // 检测必要组件是否存在并处理层级
        CheckComponentAndSortOrder(layer, uiBase);

        // 添加到字典
        if (!_uiDic.ContainsKey(uiName))
        {
            _uiDic.Add(uiName, uiBase);
        }
//        CheckCurrentUIDIC();
        return(uiBase as T);
    }
Пример #3
0
    // 检测必要组件是否存在并处理层级
    private void CheckComponentAndSortOrder(SortOrderLayer layer, UIBase uiBase)
    {
        Canvas canvas = uiBase.gameObject.GetComponent <Canvas>();

        if (canvas == null)
        {
            canvas = uiBase.gameObject.AddComponent <Canvas>();
        }

        canvas.overrideSorting = true;
        canvas.sortingOrder    = GetMaxSortOrder(layer, uiBase.UIName);

        uiBase.UICanvas  = canvas;
        uiBase.SortOrder = canvas.sortingOrder;

        GraphicRaycaster raycast = uiBase.gameObject.GetComponent <GraphicRaycaster>();

        if (raycast == null)
        {
            raycast = uiBase.gameObject.AddComponent <GraphicRaycaster>();
        }

        uiBase.Raycast = raycast;
    }