public AsyncAsset[] LoadAllAssets(string assetBundleName) { IAssetBundle assetBundle = LoadAssetBundle(assetBundleName); string[] assetNames = m_Catalog.AssetBundles[assetBundleName].Assets; AsyncAsset[] assets = new AsyncAsset[assetNames.Length]; for (int i = 0; i < assetNames.Length; i++) { string assetName = assetNames[i]; if (m_CreatedAssets.TryGetValue(assetName, out AsyncAsset asset)) { LogMessage("Load Asset: '", assetName, "' from cache."); } else { asset = CreateAssetFromAssetBundle(assetName, typeof(Object), assetBundle); } assets[i] = asset; } return(assets); }
public void Update() { for (int i = m_LoadingAssetBundles.Count - 1; i >= 0; --i) { IAssetBundle assetBundle = m_LoadingAssetBundles[i]; if (!m_CreatedAssetBundles.ContainsKey(assetBundle.Name)) // unloaded { m_LoadingAssetBundles.RemoveAt(i); } else if (assetBundle.UpdateLoadingState()) { LogMessage("AssetBundle: ", assetBundle.Name, " is loaded."); m_LoadingAssetBundles.RemoveAt(i); } } for (int i = m_LoadingAssets.Count - 1; i >= 0; --i) { AsyncAsset asset = m_LoadingAssets[i]; if (!m_CreatedAssets.ContainsKey(asset.AssetName)) // unloaded { m_LoadingAssets.RemoveAt(i); } else if (asset.UpdateLoadingState()) { LogMessage("Asset: ", asset.AssetName, " is loaded."); m_LoadingAssets.RemoveAt(i); } } }
/// <summary> /// /// </summary> /// <param name="assetParameter"></param> public virtual void LoadAsset(AssetParameter assetParameter, string loadType) { if (beginLoad) { throw new ArgumentException("Loading entity, can't add new resources!"); } if (needs.Count == 0) { loaded = 0; toLoad = 0; } if (!assets.ContainsKey(assetParameter.Path) && !needs.ContainsKey(assetParameter.Path)) { if (assetLoaders.ContainsKey(loadType)) { assetLoaders[loadType].LoadAsset(assetParameter, loadType); } needs.Add(assetParameter.Path, assetParameter); } else { if (assets.ContainsKey(assetParameter.Path)) { IAssetBundle assetBundle = assets[assetParameter.Path]; if (assetBundle.Parameter.StorageType <= assetParameter.StorageType) { assetBundle.Parameter = assetParameter; } } } }
/// <summary> /// Load a set of <see cref="IAsset"/>s from a bilingual file into a new TableController. /// </summary> /// <param name="bundle">Asset bundle that contains entries to show on this table.</param> /// <returns> /// A newly created and loaded <see cref="ITableController"/> instance. /// </returns> public static ITableController LoadBilingualAssets(IAssetBundle bundle) { var renderer = new PairRenderer(); var instance = new TableController(renderer, bundle); instance.ReloadBilingualAssets(); return(instance); }
public void Initialize(string name, Type type, IAssetBundle assetBundle) { m_AssetName = name; m_AssetType = type; m_AssetBundle = assetBundle; m_Request = null; m_Asset = null; m_IsDone = false; }
public void Initialize(string name, Object asset, IAssetBundle assetBundle) { m_AssetName = name; m_AssetType = null; m_AssetBundle = assetBundle; m_Request = null; m_Asset = asset; m_IsDone = true; }
public void Unload(bool unloadAllLoadedObjects = true) { if (_ab == null) { return; } _ab?.Unload(unloadAllLoadedObjects); _ab = null; }
/// <summary> /// The get asset method /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName">fileName the asset file name</param> /// <returns></returns> public virtual IAssetBundle GetAsset(string path) { IAssetBundle assetBundle = assets[path]; if (assetBundle == null) { throw new Exception("Asset not loaded: " + path); } return(assetBundle); }
public void RegisterBundle(IAssetBundle bundle, string name) { if (!this.bundles.TryAdd(name, bundle)) { throw new ArgumentException(); } else if (bundle is IDisposable disposableBundle) { this.disposableBundles.Add(disposableBundle); } }
/// <summary> /// /// </summary> /// <param name="swfUri"></param> /// <returns></returns> public bool unloadSWF(string swfUri) { if (!bundles.ContainsKey(swfUri)) { return(false); } IAssetBundle bundle = bundles[swfUri]; bundle.Unload(true); return(true); }
private IAssetBundle[] LoadAssetBundleDependencies(string[] dependencyNames) { IAssetBundle[] dependencies = new IAssetBundle[dependencyNames.Length]; for (int i = 0; i < dependencies.Length; i++) { dependencies[i] = LoadAssetBundle(dependencyNames[i]); dependencies[i].IncreaseRef(); } return(dependencies); }
/// <summary> /// /// </summary> /// <param name="bundle"></param> public void AddAsset(IAssetBundle bundle) { string name = fixAssetBundleUri(bundle.Name); if (bundles.ContainsKey(name)) { unloadSWF(name); } bundles.Add(name, bundle); loadSWF(name, bundle); }
private AsyncAsset CreateAssetFromAssetBundle(string name, Type type, IAssetBundle assetBundle) { AsyncAsset asset = new AsyncAsset(); asset.Initialize(name, type, assetBundle); assetBundle.IncreaseRef(); m_CreatedAssets.Add(name, asset); m_LoadingAssets.Add(asset); LogMessage("Create Asset: '", name, "'."); return(asset); }
/// <summary> /// /// </summary> /// <param name="swfUri"></param> /// <param name="useAssetBundle"></param> /// <returns></returns> internal SwfAssetContext loadSWF(string swfUri, IAssetBundle useAssetBundle) { if (swfUri.EndsWith(".swf")) { swfUri = swfUri.Substring(0, swfUri.IndexOf(".swf")); } if (contextCache.ContainsKey(swfUri)) { return(contextCache[swfUri]); } string uri = swfUri; if (useAssetBundle == null) { return(null); } TextAsset swfInfoAsset = useAssetBundle.Load <TextAsset>(uri); if (swfInfoAsset == null) { Debug.LogError("MovieClip() Invalid asset url '" + swfUri + "' actual url loaded '" + uri + "'"); return(null); } string texuri = swfUri + "_tex"; Texture texture = useAssetBundle.Load <Texture>(texuri); if (texture == null) { Debug.Log(string.Concat("Failed to load texture: ", texuri, " from bundle: ", useAssetBundle)); return(null); } Material material = new Material(baseBitmapShader) { color = Color.white, name = texuri }; texture.name = texuri; material.mainTexture = texture; SwfAssetContext context = _loadFromTextAsset(swfInfoAsset, uri, useAssetBundle, new Vector2(texture.width, texture.height)); if (context != null) { context.texture = texture; context.material = material; } useAssetBundle.Unload(uri); return(context); }
public static object Deserialize(Stream strm, IAssetBundle assetBundle) { if (strm == null) { throw new System.ArgumentNullException("strm"); } using (var serializer = SPSerializer.Create()) { var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); serializer.AssetBundle = assetBundle; return(serializer.Deserialize(formatter, strm)); } }
/// <summary> /// Creates an unloaded instance of TableController. /// </summary> /// <param name="renderer">A pair renderer to render the rows in this table.</param> /// <remarks> /// Use <see cref="LoadBilingualAssets"/> to create a usable instance. /// </remarks> protected TableController(PairRenderer renderer, IAssetBundle bundle) { DelegateCommandHelper.GetHelp(this); Renderer = renderer; Bundle = bundle; // The following are the default settings whose values are different from default(T). // BTW, the default settings should be user configurable. FIXME. TagShowing = TagShowing.Disp; ShowSpecials = true; UpdateFilter(); }
public void OnDeserialize(SerializationInfo info, StreamingContext context, IAssetBundle assetBundle) { _persistenceUid = new PersistentUid(info.GetString("uid")); _uidSet = true; this.transform.position = (Vector3)info.GetValue("pos", typeof(Vector3)); this.transform.rotation = (Quaternion)info.GetValue("rot", typeof(Quaternion)); this.transform.localScale = (Vector3)info.GetValue("scale", typeof(Vector3)); SerializationInfoEnumerator e = info.GetEnumerator(); while (e.MoveNext()) { Type componentType = TypeUtil.FindType(e.Name, true); if (componentType == null) { continue; } Component component = this.GetComponent(componentType); if (component == null) { continue; } SerializableComponent serializedComponent = (SerializableComponent)e.Value; ComponentSerializationUtility.DeserializeComponent(ref component, serializedComponent.DeserializeInfo); } int cnt = info.GetInt32("count"); if (cnt > 0) { var lst = new List <IPersistentUnityObject>(); this.GetComponentsInChildren <IPersistentUnityObject>(true, lst); for (int i = 0; i < cnt; i++) { ChildObjectData data = (ChildObjectData)info.GetValue(i.ToString(), typeof(ChildObjectData)); if (data != null && data.ComponentType != null) { IPersistentUnityObject pobj = (from o in lst where o.Uid == data.Uid select o).FirstOrDefault(); if (pobj != null) { pobj.OnDeserialize(data.DeserializeInfo, data.DeserializeContext, assetBundle); } } } } }
/// <summary> /// The clear temp asset /// </summary> public virtual void ClearTempAsset() { ICollection <IAssetBundle> list = assets.Values; IAssetBundle[] array = new IAssetBundle[list.Count]; list.CopyTo(array, 0); for (int i = 0; i < array.Length; ++i) { IAssetBundle bundle = array[i]; if (bundle.Parameter.StorageType == AssetStorageType.Temporary) { UnLoadAsset(bundle.Name); } } GC.Collect(); }
protected async UniTask <T> LoadAssetAsync <T>(string assetName, IProgress <float> progress = null, CancellationToken token = default) where T : UnityEngine.Object { ProgressDispatcher.Handler handler = null; if (_ab == null) { handler = ProgressDispatcher.instance.Create(progress); progress = handler.CreateProgress(); _ab = await AssetBundleLoader.instance.LoadByGuidAsync(_guid, handler.CreateProgress(), token); } var ret = await _ab.LoadAssetAsync <T>(assetName, progress, token); handler?.Dispose(); return(ret); }
/// <summary> /// /// </summary> /// <param name="path"></param> public virtual void UnLoadAsset(string path) { if (beginLoad) { throw new ArgumentException("Loading entity, can't add new resources!"); } IAssetBundle asset = assets[path]; if (asset != null) { if (assetLoaders.ContainsKey(asset.Parameter.LoadType)) { assetLoaders[asset.Parameter.LoadType].RemoveAsset(asset); } asset.Unload(true); } }
public static void Serialize(Stream strm, object obj, IAssetBundle assetBundle) { if (strm == null) { throw new System.ArgumentNullException("strm"); } if (obj != null) { using (var serializer = SPSerializer.Create()) { var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); serializer.AssetBundle = assetBundle; serializer.Serialize(formatter, strm, obj); } } }
public async UniTask <Scene> LoadAsync(LoadSceneMode loadSceneMode = LoadSceneMode.Additive, IProgress <float> progress = null, CancellationToken token = default) { // todo 次数处理Progress的代码跟AssetReference中的有些重复,考虑重构 ProgressDispatcher.Handler handler = null; if (_assetBundle == null) { handler = ProgressDispatcher.instance.Create(progress); progress = handler.CreateProgress(); _assetBundle = await AssetBundleLoader.instance.LoadByGuidAsync(_guid, handler.CreateProgress(), token); } Scene scene = await _assetBundle.LoadSceneAsync(_assetName, loadSceneMode, progress); handler?.Dispose(); return(scene); }
/// <summary> /// /// </summary> /// <param name="asset"></param> internal void CompleteLoad(IAssetBundle asset) { if (!beginLoad) { throw new ArgumentException("Not loading entity, can't complete be load!"); } if (asset != null && asset.Parameter != null && !assets.ContainsKey(asset.Parameter.Path)) { IAssetLoader assetLoader = GetAssetLoader(asset.Parameter.LoadType); if (assetLoader != null) { assetLoader.AddAsset(asset); } assets.Add(asset.Parameter.Path, asset); needs.Remove(asset.Parameter.Path); loaded++; } }
/// <summary> /// Load a set of <see cref="IAsset"/>s from a bilingual file into a new TableController. /// </summary> /// <param name="bundle">Asset bundle that contains entries to show on this table.</param> /// <returns> /// A newly created and loaded <see cref="ITableController"/> instance. /// </returns> public static ITableController LoadBilingualAssets(IAssetBundle bundle) { var name = bundle.Name; var assets = bundle.Assets; var renderer = new PairRenderer(); var asset_array = assets.ToArray(); // Build an instance. var new_instance = new TableController(renderer); new_instance.LoadBilingualRowData(asset_array, a => a.TransPairs, renderer); new_instance.Name = name; // Take care of Alt. if (asset_array.Any(a => a.AltPairs.Any())) { new_instance.AltLoader = delegate(string[] origins) { var alt_instance = new TableController(renderer); alt_instance.LoadBilingualRowData(asset_array, FilteredAltPairs(origins), renderer); alt_instance.Name = string.Format("Alt TM {0}", name); return(alt_instance); }; new_instance.AltOriginsLoader = delegate() { var origins = new HashSet <string>(); foreach (var asset in asset_array) { var origin = asset.Properties.ToList().FindIndex(prop => prop.Key == "origin"); if (origin >= 0) { origins.UnionWith(asset.AltPairs.Select(pair => pair[origin])); } } origins.Remove(null); // XXX return(origins.AsEnumerable()); }; } return(new_instance); }
public IEnumerator InitMaterials() { IAssetBundle ab = m_Loader.LoadAssetBundle("minecraft/materials"); yield return(ab.AsyncHandler); AsyncAsset <Material> asset; asset = ab.LoadAsset <Material>("chunkmaterial.mat"); while (!asset.IsDone) { yield return(null); } ChunkMaterial = asset.Asset; this.Log("加载Chunk材质球"); asset = ab.LoadAsset <Material>("liquidmaterial.mat"); while (!asset.IsDone) { yield return(null); } LiquidMaterial = asset.Asset; this.Log("加载Liquid材质球"); asset = ab.LoadAsset <Material>("blockentitymaterial.mat"); while (!asset.IsDone) { yield return(null); } BlockEntityMaterial = asset.Asset; this.Log("加载BlockEntity材质球"); }
public AsyncAsset LoadAsset(string name, Type type) { if (!m_Catalog.Assets.TryGetValue(name, out AssetInfo assetInfo)) { throw new FileNotFoundException("Can not find Asset: " + name); } name = assetInfo.AssetName; if (m_CreatedAssets.TryGetValue(name, out AsyncAsset asset)) { LogMessage("Load Asset: '", name, "' from cache."); } else { IAssetBundle assetBundle = LoadAssetBundle(assetInfo.AssetBundleName); asset = CreateAssetFromAssetBundle(name, type, assetBundle); } return(asset); }
public IEnumerator DoLua() { IAssetBundle ab = m_Loader.LoadAssetBundle("minecraft/lua"); yield return(ab.AsyncHandler); AsyncAssets assets = ab.LoadAllAssets <TextAsset>(); while (!assets.IsDone) { yield return(null); } Object[] luas = assets.Assets; byte[] mainLuaBytes = null; for (int i = 0; i < luas.Length; i++) { TextAsset text = luas[i] as TextAsset; byte[] bytes = text.bytes; m_LuaMap.Add(text.name, bytes); this.Log("加载lua:", text.name); if (text.name == "main.lua") { mainLuaBytes = bytes; } } if (mainLuaBytes != null) { m_LuaEnv.DoString(mainLuaBytes); this.Log("调用main.lua"); } m_Loader.UnloadAssetBundle(ab, true); }
private void UnloadAssetBundlePrivate(IAssetBundle assetBundle, bool unloadAllLoadedObjects) { if (!m_CreatedAssetBundles.Remove(assetBundle.Name)) { Debug.LogWarning($"AssetBundle '{assetBundle.Name}' is not loaded, you can not unload it."); return; } assetBundle.Unload(unloadAllLoadedObjects); LogMessage("Unload AssetBundle: '", assetBundle.Name, "', unloadAllLoadedObjects: ", unloadAllLoadedObjects); for (int i = 0; i < assetBundle.Dependencies.Length; i++) { IAssetBundle dep = assetBundle.Dependencies[i]; dep.DecreaseRef(); if (dep.RefCount <= 0) { UnloadAssetBundlePrivate(dep, unloadAllLoadedObjects); } } }
public void OnDeserialize(SerializationInfo info, StreamingContext context, IAssetBundle assetBundle) { //if (assetBundle == null) return null; //var resourceId = info.GetString("sp*id"); //var obj = assetBundle.LoadAsset(resourceId); //if (obj == null) return null; //obj = UnityEngine.Object.Instantiate(obj); //foreach (var pobj in ComponentUtil.GetComponentsFromSource<IPersistantUnityObjectToken>(obj)) //{ // pobj.OnDeserialize(info, context, assetBundle); //} //return obj; _info = info; _context = context; _bundle = assetBundle; }
public IEnumerator InitBlocks() { IAssetBundle ab = m_Loader.LoadAssetBundle("minecraft/blocks"); yield return(ab.AsyncHandler); AsyncAssets assets = ab.LoadAllAssets <Block>(); while (!assets.IsDone) { yield return(null); } Object[] blocks = assets.Assets; for (int i = 0; i < blocks.Length; i++) { Block block = blocks[i] as Block; m_BlocksMap.Add(block.Type, block); this.Log("加载方块:", block.BlockName); } }