/// <summary> /// /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static XBufferAsset LoadBytecodeAsset(string fileName) { XBufferAsset bufferAsset = null; #if UNITY_ANDROID && !UNITY_EDITOR string name = fileName.ToLower(); XSheet.XAssetInfo info = XSheet.Instance.Find(name); if (info != null && info.locationType == XSheet.XLocationType.Resource) { if (asset != null) { Debug.LogError(fileName); byte[] bytes = GetBytecodeByFileName(fileName); if (bytes != null) { bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>(); bufferAsset.init(bytes); } } else { bufferAsset = XRes.Load <XBufferAsset>(fileName); } } else { bufferAsset = XRes.Load <XBufferAsset>(fileName); } #else bufferAsset = XRes.Load <XBufferAsset>(fileName); #endif return(bufferAsset); }
/// <summary> /// /// </summary> /// <param name="info"></param> /// <param name="type"></param> /// <param name="callback"></param> /// <returns></returns> public IEnumerator DoLoadAssetAsync(XSheet.XAssetInfo info, System.Type type, System.Action <Object> callback) { XAssetBundleInfo bundleInfo = GetAssetBundleInfo(info.bundleName); if (!bundleInfo.isDone) { yield return(XCoroutine.Run(LoadAssetBundleAsync(bundleInfo))); } if (bundleInfo.isDone) { bundleInfo.IncRef(Time.time); AssetBundleRequest request = null; Object obj = null; if (type == typeof(XBufferAsset)) { request = bundleInfo.bundle.LoadAssetAsync <TextAsset>(System.IO.Path.Combine(assetBasePath, info.fullName)); yield return(request); XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>(); asset.init((TextAsset)request.asset); obj = asset; } else { request = bundleInfo.bundle.LoadAssetAsync(System.IO.Path.Combine(assetBasePath, info.fullName), type); yield return(request); obj = request.asset; } bundleInfo.DecRef(); callback(obj); } }
public static Object Load(string path, System.Type type) { if (string.IsNullOrEmpty(path)) { GLog.LogError("[XRes.Load] sent empty/null path!"); return(null); } string name = path.ToLower(); Object asset = Find(name); if (!asset) { XSheet.XAssetInfo info = XSheet.Instance.Find(name); if (info != null) { switch (info.locationType) { case XSheet.XLocationType.Resource: if (type == typeof(XBufferAsset)) { XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>(); if (bufferAsset) { bufferAsset.init( Resources.Load <TextAsset>(name) ); asset = bufferAsset; } } else { asset = Resources.Load(name, type); } break; case XSheet.XLocationType.Bundle: GLog.LogError("[XRes.Load] can't load bundle in sync load {0}", name); break; case XSheet.XLocationType.Data: string filePath = System.IO.Path.Combine(Application.persistentDataPath, info.fullName); if (System.IO.File.Exists(filePath)) { System.IO.FileStream fs = System.IO.File.OpenRead(filePath); if (fs.CanRead) { XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>(); if (bufferAsset) { bufferAsset.init((int)fs.Length); fs.Read( bufferAsset.bytes, 0, (int)fs.Length ); fs.Close(); if (type == typeof(Texture2D)) { Texture2D buffTexture = new Texture2D(1, 1); buffTexture.LoadImage(bufferAsset.bytes); asset = buffTexture; } else { asset = bufferAsset; } } } } break; default: GLog.LogError("[XRes] no imp resource type {0}", info.locationType.ToString()); break; } } } else { if (type == typeof(XBufferAsset)) { XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>(); bufferAsset.init( Resources.Load <TextAsset>(name) ); asset = bufferAsset; } else { asset = Resources.Load(name, type); } } return(asset); }
/// <summary> /// /// </summary> /// <param name="info"></param> /// <param name="type"></param> /// <param name="callback"></param> /// <returns></returns> public Coroutine LoadAssetAsync(XSheet.XAssetInfo info, System.Type type, System.Action <Object> callback) { return(XCoroutine.Run(DoLoadAssetAsync(info, type, callback))); }
/// <summary> /// Dos the load async. /// </summary> /// <returns>The load async.</returns> /// <param name="path">Path.</param> /// <param name="type">Type.</param> /// <param name="callback">Callback.</param> private static IEnumerator DoLoadAsync(string path, System.Type type, System.Action <Object> callback) { if (string.IsNullOrEmpty(path)) { GLog.LogError("[XRes::Load] sent empty/null path!"); yield break; } string name = path.ToLower(); UnityEngine.Object obj = Find(name); if (!obj) { XSheet.XAssetInfo info = XSheet.Instance.Find(name); if (info != null) { switch (info.locationType) { case XSheet.XLocationType.Resource: ResourceRequest request = null; if (type == typeof(XBufferAsset)) { request = Resources.LoadAsync <TextAsset>(name); yield return(request); XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>(); asset.init((TextAsset)request.asset); obj = asset; } else { request = Resources.LoadAsync(name, type); yield return(request); obj = request.asset; } break; case XSheet.XLocationType.Bundle: yield return(XBundleManager.Instance.LoadAssetAsync(info, type, delegate(Object result) { obj = result; })); break; case XSheet.XLocationType.Data: string fileUrl = XSheet.GetLocalFileUrl(System.IO.Path.Combine(Application.persistentDataPath, info.fullName)); WWW fileLoader = new WWW(fileUrl); yield return(fileLoader); if (!string.IsNullOrEmpty(fileLoader.error)) { XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>(); asset.init( fileLoader.bytes ); if (type == typeof(Texture2D)) { Texture2D texture = new Texture2D(1, 1); texture.LoadImage(asset.bytes); obj = texture; } else { obj = asset; } } break; case XSheet.XLocationType.Streaming: string streamUrl = XSheet.GetLocalFileUrl(System.IO.Path.Combine(Application.streamingAssetsPath, info.fullName)); if (Application.platform != RuntimePlatform.Android) { streamUrl = XSheet.GetLocalFileUrl(streamUrl); } WWW streamLoader = new WWW(streamUrl); yield return(streamLoader); if (!string.IsNullOrEmpty(streamLoader.error)) { XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>(); asset.init( streamLoader.bytes ); if (type == typeof(Texture2D)) { Texture2D texture = new Texture2D(1, 1); texture.LoadImage(asset.bytes); obj = texture; } else { obj = asset; } } break; } } else { ResourceRequest request = Resources.LoadAsync(name, type); yield return(request); obj = request.asset; } if (!obj) { if (info != null) { GLog.LogError("[XRes.Load] Can't find {0} in Location({1})", name, info.locationType); } else { GLog.LogError("[XRes.Load] Can't find {0} in Resources", name); } } } callback(obj); }