Ejemplo n.º 1
0
        IEnumerator SetImageSpriteCoroutine(UnityEngine.UI.Image image, string spriteName, bool isNativeSize)
        {
            LoadAssetAsyncOperation operation = AssetBundleManager.Instance.LoadAssetAsync <GameObject>(spriteName, false);

            if (operation != null && !operation.IsDone)
            {
                yield return(operation);
            }

            if (operation != null)
            {
                if (image != null)
                {
                    GameObject spriteGo = operation.GetAsset <GameObject>();
                    if (spriteGo != null)
                    {
                        SpriteRenderer spriteRenderer = spriteGo.GetComponent <SpriteRenderer>();
                        if (spriteRenderer != null)
                        {
                            if (spriteRenderer.sprite != null)
                            {
                                image.sprite = spriteRenderer.sprite;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 异步加载一个资源
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assetName"></param>
        /// <param name="unloadAssetBundle"></param>
        /// <param name="unloadAllLoadedObjects"></param>
        /// <returns></returns>
        public LoadAssetAsyncOperation LoadAssetAsync <T>(string assetName, bool unloadAssetBundle = true, bool unloadAllLoadedObjects = false) where T : UnityEngine.Object
        {
            try
            {
                if (!IsReady)
                {
                    return(null);
                }

                LoadAssetAsyncOperation operation   = null;
                AssetRecordInfo         assetRecord = GetAssetRecordInfoByAsset(assetName);
                if (assetRecord == null)
                {
                    return(null);
                }

#if UNITY_EDITOR
                if (SimulateAssetBundleInEditor)
                {
                    T asset = UnityEditor.AssetDatabase.LoadAssetAtPath <T>(assetRecord.RelativePath);
                    operation = new LoadAssetAsyncOperation(asset);
                }
                else
#endif
                {
                    AssetBundle        assetBundle = LoadAssetBundleAndDependenciesByAsset(assetRecord);
                    AssetBundleRequest request     = null;
                    if (assetBundle != null)
                    {
                        request = assetBundle.LoadAssetAsync <T>(assetRecord.AssetName);
                        DisposeTemporaryAssetBundles(unloadAssetBundle, unloadAllLoadedObjects);
                    }
                    operation = new LoadAssetAsyncOperation(request);
                }
                return(operation);
            }
            catch (System.Exception ex)
            {
                Debug.LogErrorFormat("AssetBundleManager.LoadAsset is falid!\n{0}", ex.Message);
            }
            return(null);
        }