コード例 #1
0
        public void CacheUI(string uiPath)
        {
            if (cacheUIs.ContainsKey(uiPath))
            {
                return;
            }

            cacheUIs.Add(uiPath, new CacheUI(uiPath, null, false));
            ResourceManager.Instance.LoadAssetAsync(
                uiPath,
                (Object obj) =>
            {
                if (obj == null)
                {
                    Debug.LogError(string.Format("cache UI failed: {0}", uiPath));
                }

                GameObject instance = GameObject.Instantiate(obj) as GameObject;
                instance.transform.SetParent(cacheRoot, false);

                CacheUI cacheUI = cacheUIs[uiPath];
                if (cacheUI == null)
                {
                    cacheUIs[uiPath] = new CacheUI(uiPath, instance, true);
                }
                else
                {
                    cacheUI.gameObject = instance;
                    cacheUI.isLoaded   = true;
                }
            });
        }
コード例 #2
0
        private IEnumerator LoadUI(Window window, bool isAsync = false)
        {
            GameObject uiGameObject = null;

            if (cacheUIs.ContainsKey(window.uiPath))//如果UI已缓存,等待UI加载完毕
            {
                CacheUI cacheUI   = cacheUIs[window.uiPath];
                float   startTime = Time.realtimeSinceStartup;
                while (!cacheUI.isLoaded)
                {
                    loadingTipUI.SetActive(true);
                    yield return(null);

                    if (Time.realtimeSinceStartup - startTime > loadUIExpireTime)
                    {
                        window.error = string.Format("wait for cache ui load time expire : {0}", cacheUI.path);
                        loadingTipUI.SetActive(false);
                        yield break;
                    }
                }
                loadingTipUI.SetActive(false);
                uiGameObject = cacheUI.gameObject;
                cacheUIs.Remove(window.uiPath);
            }
            else if (isAsync)
            {
                yield return(StartCoroutine(ResourceManager.Instance._LoadAssetAsync(window.uiPath, (Object obj) => { uiGameObject = GameObject.Instantiate(obj) as GameObject; })));
            }
            else
            {
                uiGameObject = GameObject.Instantiate(ResourceManager.Instance.LoadAsset(window.uiPath)) as GameObject;
            }

            if (uiGameObject == null)
            {
                window.error = string.Format("load ui failed : {0}", window.uiPath);
                yield break;
            }

            AnchorUIGameObject(uiGameObject, window.type);
            window.uiGameObject = uiGameObject;
            window.uiTransform  = uiGameObject.transform;
        }