Beispiel #1
0
        /// <summary>
        /// 网络资源加载
        /// 异步运行,内部异步加载,获得资源对象,如果资源对象为空,尝试加载,并且通过回调返回加载的资源对象,允许添加加载进度回调
        /// </summary>
        /// <param name="assetBundleName"></param>
        /// <param name="assetName"></param>
        /// <param name="loadedListener"></param>
        /// <param name="progressListener"></param>
        /// <returns></returns>
        public IEnumerator LoadAssetObjectCoroutineAsync(string assetName, string assetBundleName, Action <UnityEngine.Object> loadedListener = null, Action <float> progressListener = null)
        {
            yield return(null);

            AssetInfo assetInfo = GetOrAddAssetInfo(assetName, assetBundleName);

            // 资源已加载
            if (assetInfo.AssetObjectCache != null)
            {
                if (progressListener != null)
                {
                    progressListener(1.0f);

                    yield return(null);
                }

                if (loadedListener != null)
                {
                    loadedListener(assetInfo.AssetObjectCache);
                }
            }
            // 资源未加载
            else
            {
                AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync(assetInfo.AssetBundleName, assetInfo.AssetName, typeof(UnityEngine.Object));
                if (request == null)
                {
                    yield break;
                }

                // 最多尝试100次(10秒)。防止资源不存在不停下载的情况 TODO 可能有BUG
                int maxTry = 0;
                while (request.Process() < 0.9f)
                {
                    if (request.Process() < 0.001f && maxTry++ > 100)
                    {
                        Debug.LogError("网络无法获得资源:" + assetInfo.AssetBundleName + " " + assetInfo.AssetName);
                    }
                    // TODO 进度条处理
                    Debug.Log("加载:" + request.Process() * 100 + "/100");
                    progressListener(request.Process());
                    yield return(new WaitForSeconds(0.1f));
                }

                while (!request.IsDone())
                {
                    maxTry++;
                    Debug.Log("加载:" + request.Process() * 100 + "/100");
                    progressListener(request.Process());
                    yield return(new WaitForSeconds(0.1f));
                }

                Debug.Log("加载:100/100");
                assetInfo.AssetObjectCache = request.GetAsset <GameObject>();
            }
        }
Beispiel #2
0
    protected IEnumerator InstantiateGameObjectAsync(string assetBundleName, string assetName)
    {
        // This is simply to get the elapsed time for this phase of AssetLoading.
        float startTime = Time.realtimeSinceStartup;

        // Load asset from assetBundle.
        AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync(assetBundleName, assetName, typeof(GameObject));

        if (request == null)
        {
            Debug.LogError("Failed AssetBundleLoadAssetOperation on " + assetName + " from the AssetBundle " + assetBundleName + ".");
            yield break;
        }
        // 方案一
        // yield return StartCoroutine(request);

        // 方案二
        {
            while (request.Process() < 0.9f)
            {
                // TODO 进度条处理
                Debug.Log("加载:" + request.Process() * 100 + "/100");
                yield return(new WaitForEndOfFrame());
            }

            while (!request.IsDone())
            {
                Debug.Log("加载:" + request.Process() * 100 + "/100");
                yield return(new WaitForEndOfFrame());
            }

            Debug.Log("加载:100/100");
        }

        // Get the Asset.
        GameObject prefab = request.GetAsset <GameObject> ();

        // Instantiate the Asset, or log an error.
        if (prefab != null)
        {
            GameObject.Instantiate(prefab);
        }
        else
        {
            Debug.LogError("Failed to GetAsset from request");
        }

        // Calculate and display the elapsed time.
        float elapsedTime = Time.realtimeSinceStartup - startTime;

        Debug.Log(assetName + (prefab == null ? " was not" : " was") + " loaded successfully in " + elapsedTime + " seconds");
    }
    IEnumerator WaitLoadObject <T>(AssetBundleLoadAssetOperation operation, UnityAction <T> onLoad) where T : UnityEngine.Object
    {
        yield return(operation);

        if (onLoad != null)
        {
            T asset = operation.GetAsset <T>();
            if (asset == null)
            {
                Debug.Log(operation.IsDone());
            }
            onLoad.Invoke(asset);
        }
    }
Beispiel #4
0
 void CheckLoading()
 {
     if (assetLoadOperation != null)
     {
         if (assetLoadOperation.IsDone())
         {
             GameObject prefab = assetLoadOperation.GetAsset <GameObject>();
             if (prefab == null)
             {
                 assetLoadOperation.Unload();
                 assetLoadOperation = null;
                 StopImmediate();
             }
             else
             {
                 InstantiateModel(prefab);
             }
         }
     }
 }
Beispiel #5
0
 void CheckLoading()
 {
     if (assetLoadOperation != null)
     {
         if (assetLoadOperation.IsDone())
         {
             audioClip        = assetLoadOperation.GetAsset <AudioClip>();
             audioSource.clip = audioClip;
             if (audioClip == null)
             {
                 assetLoadOperation.Unload();
                 assetLoadOperation = null;
                 Stop();
             }
             else
             {
                 audioSource.Play();
             }
         }
     }
 }
    LuaFileInfo CreatePromtionObject(string bundleName, string className)
    {
        if (className == "none")
        {
            return(null);
        }

        AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetFromCachedAssetBundle(bundleName, className, typeof(TextAsset));

        if (request != null && request.IsDone())
        {
            TextAsset luaSrc = request.GetAsset <TextAsset>();
            script.DoString(luaSrc.text);
        }
        else
        {
            Debug.LogError(className + ".lua load Failed");
            return(null);
        }

        LuaFileInfo info = new LuaFileInfo();

        info.className = script.Globals.Get(className);

        DynValue newFunction = info.className.Table.Get("new");

        if (newFunction.IsNotNil())
        {
            info.classInst = script.Call(newFunction, info.className);
        }
        else
        {
            Debug.LogError(className + "new not found");
            return(null);
        }

        return(info);
    }
    static public IEnumerator LoadTextAssets()
    {
        s_dicTextAsset.Clear();


        if (AssetBundleManager.SimulateAssetBundleInEditor == true)
        {
            string[] arrSettingData = AssetDatabase.GetAssetPathsFromAssetBundle("setting_data");
            if (arrSettingData == null)
            {
                yield break;
            }

            string stringData = string.Empty;
            for (int i = 0; i < arrSettingData.Length; ++i)
            {
                stringData = Path.GetFileName(arrSettingData[i]);
                stringData = stringData.Replace(".json", "");
                TextAsset assetData = AssetDatabase.LoadAssetAtPath <TextAsset>(arrSettingData[i]);
                s_dicTextAsset.Add(stringData, assetData.text);
            }

            yield break;
        }



        {
            while (AssetBundleManager.AssetBundleManifestObject == null)
            {
                yield return(new WaitForSeconds(1f));
            }


            AssetBundleManager.LoadAssetBundle("setting_data");


            string strError = string.Empty;


            LoadedAssetBundle loadBundle = AssetBundleManager.GetLoadedAssetBundle("setting_data", out strError);
            while (loadBundle == null)
            {
                yield return(new WaitForSeconds(0.1f));

                loadBundle = AssetBundleManager.GetLoadedAssetBundle("setting_data", out strError);
            }


            string[] strAssetData = loadBundle.m_AssetBundle.GetAllAssetNames();

            if (strAssetData != null)
            {
                string stringData = string.Empty;
                for (int i = 0; i < strAssetData.Length; ++i)
                {
                    stringData = Path.GetFileName(strAssetData[i]);
                    stringData = stringData.Replace(".json", "");
                    stringData = stringData.ToUpper();

                    AssetBundleLoadAssetOperation operation = AssetBundleManager.LoadAssetAsync("setting_data", stringData, typeof(TextAsset));
                    if (operation != null)
                    {
                        while (operation.IsDone() == false)
                        {
                            yield return(new WaitForEndOfFrame());
                        }

                        TextAsset assetData = operation.GetAsset <TextAsset>();
                        s_dicTextAsset.Add(stringData, assetData.text);
                    }

                    AssetBundleManager.UnloadAssetBundle("setting_data");
                }
            }
        }
    }
Beispiel #8
0
        private void UpdateLoadAssets()
        {
            AssetBundleLoadAssetOperation operation = null;

            for (int i = 0; i < m_InProgressOperations.Count;)
            {
                operation = m_InProgressOperations[i];
                if (operation.IsDone())
                {
                    // 如果完成直接移除,避免后面回调方法中上层逻辑错误导致不停Update
                    m_InProgressOperations.RemoveAt(i);
                    string key = AssetKey(operation.assetBundleName, operation.assetName, operation.type);

                    if (string.IsNullOrEmpty(operation.Error))
                    {
                        AssetCache asset = CacheAsset(key, operation.GetAsset());
                        if (asset != null)
                        {
                            asset.assetbundleVariant = operation.assetBundleVariant;

                            m_CallbackTemp.Add(key);
                        }
                        else
                        {
                            AssetBundleManager.UnloadAssetBundle(operation.assetBundleVariant);
                            Callback(key, null, string.Format("[{0} no asset error!]", key));
                        }
                    }
                    else
                    {
                        if (m_AssetCaches.TryGetValue(key, out AssetCache cache))
                        {
                            cache.assetbundleVariant = operation.assetBundleVariant;
                        }

                        RemoveCachedAsset(key);
                        Callback(key, null, operation.Error);
                    }
                }
                else
                {
                    i++;
                }
            }

            //todo 同一帧可能返回多个,这里还是需要保护一下,
            while (m_CallbackTemp.Count > 0)
            {
                string key = m_CallbackTemp[0];

                if (m_AssetCaches.TryGetValue(key, out AssetCache cache))
                {
                    if (cache != null && cache.obj != null)
                    {
                        Callback(key, cache.obj, string.Empty);
                    }
                    else
                    {
                        Debug.LogErrorFormat("{0} is Null", key);
                    }
                }

                m_CallbackTemp.RemoveAt(0);
            }
        }