Esempio n. 1
0
    public static GameObject CreateUIFromResource(string UIName, bool isTopCanvas = false, bool isBGCanvas = false)
    {
        GameObject go = null;

        if (IsHaveUI(UIName))
        {
            go = GetExistUI(UIName);
            if (!go.activeInHierarchy)
            {
                go.SetActive(true);
            }
        }
        else
        {
            var       prefab = Resources.Load <GameObject>(UIName);
            Transform parent = null;
            if (isTopCanvas)
            {
                parent = UIRoot.instance.GetUIRootCanvasTop().transform;
            }
            else if (isBGCanvas)
            {
                parent = UIRoot.instance.GetUIRootCanvasBG().transform;
            }
            else
            {
                parent = UIRoot.instance.GetUIRootCanvas().transform;
            }
            go = ResourcesHelper.InstantiatePrefabFromABSetDefault(prefab, parent);
            AddListUI(UIName, go);
        }
        //层级设置到最高
        go.transform.SetAsLastSibling();
        return(go);
    }
Esempio n. 2
0
    public static async Task <GameObject> CreateUIAsync(string UIName, bool isTopCanvas = false, bool isBGCanvas = false, bool isGuideCanvas = false)
    {
        GameObject go = null;

        if (IsHaveUI(UIName))
        {
            go = GetExistUI(UIName);
            if (!go.activeInHierarchy)
            {
                go.SetActive(true);
            }
            //层级设置到最高
            go.transform.SetAsLastSibling();
            return(go);
        }
        else
        {
            bool isRelease = false;
            try
            {
                //打开遮罩,阻挡点击事件
                GameObject uiEventMask = CreateUI(UIType.UIEventMask, true);
                await semaphoreSlim.WaitAsync();

                //信号量释放后要重新刷新堵塞前的状态
                var prefab = await ABManager.GetAssetAsync <GameObject>(UIName);

                Transform parent = null;
                if (isTopCanvas)
                {
                    parent = UIRoot.instance.GetUIRootCanvasTop().transform;
                }
                else if (isBGCanvas)
                {
                    parent = UIRoot.instance.GetUIRootCanvasBG().transform;
                }
                else if (isGuideCanvas)
                {
                    parent = UIRoot.instance.GetUIRootCanvasGuide().transform;
                }
                else
                {
                    parent = UIRoot.instance.GetUIRootCanvas().transform;
                }
                go = ResourcesHelper.InstantiatePrefabFromABSetDefault(prefab, parent);
                //层级设置到最高
                go.transform.SetAsLastSibling();
                AddListUI(UIName, go);
                ReleaseRelated(ref isRelease);
                return(go);
            }
            catch (Exception e)
            {
                throw new Exception($"{UIName} UI 错误: {e}");
            }
            finally
            {
                ReleaseRelated(ref isRelease);
            }
        }
    }