示例#1
0
        /// <summary>
        /// Will retrieve asset either from LoadedAssets or asset bundle and return it. Will load
        /// asset bundle if necessary.
        /// </summary>
        /// <typeparam name="T">Type of asset</typeparam>
        /// <param name="assetName">name of asset</param>
        /// <param name="loadedBundle">had to load asset bundle</param>
        /// <returns>A reference to the loaded asset or null if not found.</returns>
        private T LoadAssetFromBundle <T>(string assetName, out bool loadedBundle) where T : UnityEngine.Object
        {
            LoadedAsset la = new LoadedAsset();

            loadedBundle = false;

            if (string.IsNullOrEmpty(assetName))
            {
                return(null);
            }
            try
            {
                assetName = ModManager.GetAssetName(assetName);

                if (IsAssetLoaded(assetName))
                {
                    la = loadedAssets[assetName];
                    return(la.Obj as T);
                }

#if UNITY_EDITOR
                if (IsVirtual)
                {
                    la.Obj = LoadAssetFromResources <T>(assetName);
                    if (la.Obj != null)
                    {
                        la.T = la.Obj.GetType();
                        loadedAssets.Add(assetName, la);
                    }
                    return(la.Obj as T);
                }
#endif

                if (AssetBundle == null)
                {
                    loadedBundle = LoadAssetBundle();
                }

                if (AssetBundle.Contains(assetName))
                {
                    la.Obj = AssetBundle.LoadAsset <T>(assetName);

                    if (la.Obj != null)
                    {
                        la.T = la.Obj.GetType();
                        AddAsset(assetName, la);
                    }
                    return(la.Obj as T);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message);
                return(null);
            }
        }
示例#2
0
        private bool AddAsset(string assetName, LoadedAsset la)
        {
            if (IsAssetLoaded(assetName))
            {
                return(false);
            }
            else if (la.Obj == null || la.T == null)
            {
                return(false);
            }
            else
            {
                if (la.T == typeof(GameObject) && AssetBundle.Contains(ImportedComponentAttribute.MakeFileName(assetName)))
                {
                    ImportedComponentAttribute.Restore(this, la.Obj as GameObject);
                }

                loadedAssets.Add(assetName, la);

                if (this.ModInfo != null && string.IsNullOrEmpty(this.Title) == false)
                {
                    ModManager.OnLoadAsset(this.Title, assetName, la.T);
                }
#if DEBUG
                Debug.Log(string.Format("added asset: {0}", assetName));
#endif
                return(true);
            }
        }
示例#3
0
        private bool AddAsset(string assetName, UnityEngine.Object asset)
        {
            if (asset == null)
            {
                return(false);
            }

            LoadedAsset la = new LoadedAsset(asset.GetType(), asset);

            return(AddAsset(assetName, la));
        }
示例#4
0
        /// <summary>
        /// Loads all assets from asset bundle immediately.
        /// Invidual assets can then be retrieved with <see cref="GetAsset{T}(string, bool)"/>.
        /// Assets are permanently cached until requested once; then they benefit of memory management.
        /// </summary>
        /// <param name="unloadBundle">Unload asset bundle if true</param>
        /// <returns>True if assetbundle has been loaded succesfully.</returns>
        public bool LoadAllAssetsFromBundle(bool unloadBundle = true)
        {
            if (AssetNames == null)
            {
                return(false);
            }

            try
            {
                for (int i = 0; i < AssetNames.Length; i++)
                {
                    string assetname = ModManager.GetAssetName(AssetNames[i]);

                    if (IsAssetLoaded(assetname))
                    {
                        continue;
                    }

                    if (AssetBundle == null)
                    {
                        if (!LoadAssetBundle())
                        {
                            return(false);
                        }
                    }

                    LoadedAsset la = new LoadedAsset();
                    la.Obj = AssetBundle.LoadAsset(assetname);

                    if (la.Obj == null)
                    {
                        Debug.LogWarning(string.Format("failed to load asset: {0} for mod: {1}", AssetNames[i], Title));
                        continue;
                    }

                    la.T         = la.Obj.GetType();
                    la.TimeStamp = -1;
                    AddAsset(assetname, la);
                }

                if (unloadBundle)
                {
                    UnloadAssetBundle(false);
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message);
                return(false);
            }

            return(true);
        }
示例#5
0
        private bool AddAsset(string assetName, LoadedAsset la)
        {
            if (IsAssetLoaded(assetName))
            {
                return(false);
            }
            else if (la.Obj == null || la.T == null)
            {
                return(false);
            }
            else
            {
                LoadedAssets.Add(assetName, la);

                if (this.modInfo != null && this.Name != null)
                {
                    ModManager.OnLoadAsset(this.Name, assetName, la.T);
                }
#if DEBUG
                Debug.Log(string.Format("added asset: {0}", assetName));
#endif
                return(true);
            }
        }
示例#6
0
        /// <summary>
        /// Will retrieve asset either from LoadedAssets or asset bundle and return it. Will load
        /// asset bundle if necessary.
        /// </summary>
        /// <typeparam name="T">Type of asset</typeparam>
        /// <param name="assetName">name of asset</param>
        /// <param name="loadedBundle">had to load asset bundle</param>
        /// <returns>A reference to the loaded asset or null if not found.</returns>
        private T LoadAssetFromBundle <T>(string assetName, out bool loadedBundle) where T : UnityEngine.Object
        {
            LoadedAsset la = new LoadedAsset();

            loadedBundle = false;

            if (string.IsNullOrEmpty(assetName))
            {
                return(null);
            }
            try
            {
                assetName = ModManager.GetAssetName(assetName);
                float time = Time.realtimeSinceStartup;

                if (IsAssetLoaded(assetName))
                {
                    la = loadedAssets[assetName];
                    if (la.Obj)
                    {
                        // Update timestamp of last access, but only if difference is
                        // significant to limit the number of reassignment to dictionary.
                        if (la.TimeStamp < 0 || time - la.TimeStamp > 59)
                        {
                            la.TimeStamp            = time;
                            loadedAssets[assetName] = la;
                        }

                        return(la.Obj as T);
                    }

                    loadedAssets.Remove(assetName);
                    Debug.LogWarningFormat("Removed asset {0} from cache of mod {1} because object is unloaded.", assetName, Title);
                }

#if UNITY_EDITOR
                if (IsVirtual)
                {
                    la.Obj = LoadAssetFromResources <T>(assetName);
                    if (la.Obj != null)
                    {
                        la.T         = la.Obj.GetType();
                        la.TimeStamp = time;
                        loadedAssets.Add(assetName, la);
                    }
                    return(la.Obj as T);
                }
#endif

                if (AssetBundle == null)
                {
                    loadedBundle = LoadAssetBundle();
                }

                if (AssetBundle.Contains(assetName))
                {
                    la.Obj = AssetBundle.LoadAsset <T>(assetName);

                    if (la.Obj != null)
                    {
                        la.T         = la.Obj.GetType();
                        la.TimeStamp = time;
                        AddAsset(assetName, la);
                    }
                    return(la.Obj as T);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message);
                return(null);
            }
        }