/// <summary> /// 开始异步加载 /// </summary> /// <param name="bundleRequest">异步AssetBundle </param> /// <param name="entity">加载资源后实体</param> /// <param name="callback">资源加载完成回调</param> /// <returns></returns> private IEnumerator StartLoad(AssetBundleCreateRequest bundleRequest, LoadSourceEntity entity, Action <LoadSourceEntity> callback) { if (entity == null) { yield return(0); } if (bundleRequest == null) { LCSConsole.WriteError("异步加载 AssetBundleCreateRequest 不存在!, bundleRequest = null"); yield return(0); } yield return(bundleRequest); AssetBundle assetBundle = bundleRequest.assetBundle; if (assetBundle == null) { LCSConsole.WriteError("创建资源 AssetBundle 失败!"); yield return(0); } UnityEngine.Object retobj = assetBundle.Load(entity.ResName); if (retobj == null) { LCSConsole.WriteError("资源 AssetBundle 中不存在 resName = " + entity.ResName); yield return(0); } if (callback == null) { yield return(0); } entity.LoadObj = retobj; entity.Bundle = assetBundle; callback(entity); }
/// <summary> /// 创建界面 /// </summary> /// <param name="winPath">加载资源路径</param> /// <param name="winName">打开界面的名字</param> private static LAUIBehaviour CreatePage(string winName, string winPath) { if (string.IsNullOrEmpty(winName)) { LCSConsole.WriteError("打开的界面名字为空! pageName = " + winName); return(null); } if (string.IsNullOrEmpty(winPath)) { LCSConsole.WriteError("加载资源 AssetBundle 文件路径为空! bundlePath = " + winPath); return(null); } GameObject ui = LCSManageSource.LoadSource(winName, winPath); if (ui == null) { LCSConsole.WriteError("加载的资源不存在!"); return(null); } GameObject go = GameObject.Instantiate(ui) as GameObject; if (go == null) { return(null); } LCSCompHelper.InitTransform(go, UIRoot); return(LCSCompHelper.GetComponent <LAUIBehaviour>(go)); }
/// <summary> /// 异步打开界面回调 /// </summary> private static void AsyncOpenWindowCallback(string winName, GameObject go) { if (string.IsNullOrEmpty(winName)) { LCSConsole.WriteError("打开的界面名字为空! winName = string.Empty"); return; } if (go == null) { LCSConsole.WriteError("资源加载失败!"); return; } GameObject ui = GameObject.Instantiate(go) as GameObject; if (ui == null) { return; } LCSCompHelper.InitTransform(go, UIRoot); LAUIBehaviour win = LCSCompHelper.GetComponent <LAUIBehaviour>(ui); int depth = 1; LAUIBehaviour topWin = TopWindow(); if (topWin != null) { depth = topWin.WinDepth + LCSConfig.DepthSpan; } // 初始化当前界面 win.OnOpen(depth, winName); Add <LCSUIManage>(winName, win); }
/// <summary> /// 卸载资源 /// </summary> /// <param name="bundle">卸载的资源</param> /// <param name="unload"></param> public static void UnLoadSource(AssetBundle bundle, bool unload) { if (bundle == null) { LCSConsole.WriteError("卸载资源为空!"); return; } bundle.Unload(unload); }
/// <summary> /// 加载 ngui 图集 /// </summary> /// <param name="resName"></param> /// <param name="bundPath"></param> /// <returns></returns> public static UIAtlas LoadUIAtlas(string resName, string bundPath) { UnityEngine.Object load = SyncLoadSource(resName, bundPath); if (load == null) { LCSConsole.WriteError("同步加载资源 UIAtlas 失败!"); return(null); } return(load as UIAtlas); }
/// <summary> /// 加载视频 /// </summary> /// <param name="resName"></param> /// <param name="bundPath"></param> /// <returns></returns> public static AudioSource LoadAudioSource(string resName, string bundPath) { UnityEngine.Object load = SyncLoadSource(resName, bundPath); if (load == null) { LCSConsole.WriteError("同步加载资源 AudioSource 失败!"); return(null); } return(load as AudioSource); }
/// <summary> /// 加载贴图 /// </summary> /// <returns></returns> public static Texture2D LoadTexture(string resName, string bundPath) { UnityEngine.Object load = SyncLoadSource(resName, bundPath); if (load == null) { LCSConsole.WriteError("同步加载资源 Texture2D 失败!"); return(null); } return(load as Texture2D); }
/// <summary> /// 结束观察 /// </summary> /// <returns></returns> public double EndWatch() { RecordWatchEntity entity = CurrentWatch; if (entity == null) { LCSConsole.WriteError("观察还未开始..."); return(0); } entity.IsWatch = false; entity.EndTime = Time.realtimeSinceStartup; LCSConsole.Write(string.Format(" [RecordTime] {0} use {1}s.", entity.WatchKey, entity.DiffTime)); return(entity.DiffTime); }
/// <summary> /// 结束观察 /// </summary> public double EndWatch() { RecordWatchEntity entity = CurrentWatch; if (entity == null) { LCSConsole.WriteError("观察还未开始..."); return(0); } entity.EndWatch(); double watchTime = entity.GetWatchTime(); // 7位精度 LCSConsole.WriteWarning("[Watch] {0} use time : {1}s.", entity.WatchKey, watchTime.ToString("F7")); return(watchTime); }
/// <summary> /// 得到观察的时间 /// </summary> /// <returns></returns> public double GetWatchTime() { RecordWatchEntity entity = CurrentWatch; if (entity == null) { LCSConsole.WriteError("观察还未开始..."); return(0); } if (entity.IsWatch) { LCSConsole.WriteError("观察还未结束..."); return(0); } return(entity.DiffTime); }
/// <summary> /// 移出单个资源 /// </summary> /// <param name="resName">资源名字</param> /// <returns></returns> public static bool RemoveSource(string resName) { if (string.IsNullOrEmpty(resName)) { LCSConsole.WriteError("移出的资源名字为空!,resName = " + resName); return(false); } LoadSourceEntity entity; if (!TryFind <LCSManageSource>(resName, out entity)) { LCSConsole.WriteError("移出的资源不存在!,resName = " + resName); return(false); } LCSUnloadSource.UnLoadSource(entity.Bundle); Remove <LCSManageSource>(resName); return(true); }
/// <summary> /// 导入资源 /// /// 以二进制加载 /// </summary> /// <param name="bundPath">导入 AssetBundle 路径 </param> /// <param name="resName"> 从 AssetBundle 中导入的资源名 </param> /// <param name="type">加载资源的类型</param> /// <returns></returns> private static LoadSourceEntity LoadBinarySources(string bundPath, string resName) { if (string.IsNullOrEmpty(resName)) { LCSConsole.WriteError("导入资源名字为空,resName = " + resName); return(null); } if (string.IsNullOrEmpty(bundPath)) { LCSConsole.WriteError("导入 AssetBundle 路径为空, bundPath = " + bundPath); return(null); } if (!File.Exists(bundPath)) { LCSConsole.WriteError("导入 AssetBundle 路径不存在,bundPath = " + bundPath); return(null); } byte[] bytes = File.ReadAllBytes(bundPath); AssetBundle bundle = AssetBundle.CreateFromMemoryImmediate(bytes); if (bundle == null) { LCSConsole.WriteError("创建资源 AssetBundle 失败!"); return(null); } UnityEngine.Object retobj = bundle.Load(resName); if (retobj == null) { LCSConsole.WriteError("资源 AssetBundle 中不存在 resName = " + resName); return(null); } return(new LoadSourceEntity { LoadObj = retobj, Bundle = bundle, BundlePath = bundPath, ResName = resName }); }
/// <summary> /// 开始观察 /// </summary> /// <param name="key"></param> /// <returns></returns> public void StartWatch(string key) { RecordWatchEntity entity = CurrentWatch; if (entity != null && entity.IsWatch) { LCSConsole.WriteError("已经开始观察性能......"); return; } if (entity == null) { entity = new RecordWatchEntity { WatchKey = string.IsNullOrEmpty(key) ? LCSGuid.NewUpperGuid() : key } } ; entity.BeginWatch(); AddWatch(entity); }
/// <summary> /// 直接从 Resources 文件夹下加载资源 /// </summary> /// <param name="request"></param> /// <param name="callback"></param> /// <returns></returns> private IEnumerator StartLoad(ResourceRequest request, LoadSourceEntity entity, Action <LoadSourceEntity> callback) { if (entity == null) { yield return(0); } if (request == null) { LCSConsole.WriteError("异步加载 ResourceRequest 不存在!, request = null"); yield return(0); } yield return(request); if (callback == null) { yield return(0); } entity.LoadObj = request.asset; callback(entity); }
/// <summary> /// 同步打开界面 /// </summary> /// <param name="winName">界面的名字,唯一</param> /// <param name="winPath">界面加载路径</param> public static void OpenWindow(string winName, string winPath) { if (string.IsNullOrEmpty(winName)) { LCSConsole.WriteError("打开的界面名字为空! winName = string.Empty"); return; } if (string.IsNullOrEmpty(winPath)) { LCSConsole.WriteError("加载资源 AssetBundle 文件路径为空! bundlePath = string.Empty"); return; } LAUIBehaviour win = null; if (TryFind <LCSUIManage>(winName, out win)) { return; } int depth = 1; // 当前最高的界面失去焦点 LAUIBehaviour topWin = TopWindow(); if (topWin != null) { depth = topWin.WinDepth + LCSConfig.DepthSpan; topWin.OnLostFocus(); } if (!TryCreatePage(winName, winPath, out win)) { LCSConsole.WriteWarning("创建 ui 界面 LAUIBehaviour 失败!"); return; } // 初始化当前界面 win.OnOpen(depth, winName); Add <LCSUIManage>(winName, win); }
/// <summary> /// 同步导入资源 /// /// 直接加载 Resources 文件夹下资源 /// </summary> /// <param name="resPath"> /// /// 资源名字 /// /// Resources夹下路径 /// 例如:Scenes/battleScene.scene /// </param> /// <returns> /// 加载的资源 /// /// return null : 资源名字为空,或者资源不存在, 或者资源类型不对 /// /// </returns> private static LoadSourceEntity LoadResources(string resPath) { if (string.IsNullOrEmpty(resPath)) { LCSConsole.WriteError("导入资源名字为空,resPath = string.Empty"); return(null); } UnityEngine.Object load = Resources.Load(resPath); if (load == null) { LCSConsole.WriteError("导入资源不存在!!"); return(null); } return(new LoadSourceEntity { ResName = resPath, BundlePath = resPath, LoadObj = load, Bundle = null }); }
/// <summary> /// 使用二进制方式加载资源 /// </summary> /// <param name="entity"></param> /// <param name="loadPath">真实的加载路径</param> /// <param name="callback"></param> private void LoadBinarySources(LoadSourceEntity entity, string loadPath, Action <LoadSourceEntity> callback) { if (entity == null) { LCSConsole.WriteError("资源加载实体数据为空,不能加载!"); return; } if (string.IsNullOrEmpty(entity.ResName)) { LCSConsole.WriteError("导入资源名字为空, resName = string.Empty"); return; } if (string.IsNullOrEmpty(entity.BundlePath)) { LCSConsole.WriteError("导入 AssetBundle 路径为空, bundPath = string.Empty"); return; } if (string.IsNullOrEmpty(loadPath)) { LCSConsole.WriteError("导入 AssetBundle 真实路径为空, loadPath = string.Empty"); return; } if (!File.Exists(loadPath)) { LCSConsole.WriteError("导入 AssetBundle 路径不存在, loadPath = " + loadPath); return; } byte[] bytes = File.ReadAllBytes(loadPath); AssetBundleCreateRequest request = AssetBundle.CreateFromMemory(bytes); // 开始异步加载 LCCoroutine.Instance.StartCoroutine(StartLoad(request, entity, callback)); }
/// <summary> /// 开始观察 /// </summary> /// <param name="key">指定key值</param> public void StartWatch(string key) { RecordWatchEntity entity = CurrentWatch; if (entity != null && entity.IsWatch) { LCSConsole.WriteError("已经开始观察性能......"); return; } if (entity == null) { entity = new RecordWatchEntity { WatchKey = string.IsNullOrEmpty(key) ? LCSGuid.NewUpperGuid() : key } } ; entity.IsWatch = true; entity.StartTime = Time.realtimeSinceStartup; // 记录 自游戏开始的实时时间 AddWatch(entity); }
/// <summary> /// /// 直接在 Resources 文件夹下加载文件 /// /// </summary> /// <param name="entity"> /// /// 资源管理实体 /// /// LoadSourceEntity 资源实体的 BundlePath 为相对于 Resources 路径 /// /// </param> /// <param name="callback">资源管理类的回调函数</param> private void LoadResources(LoadSourceEntity entity, Action <LoadSourceEntity> callback) { if (entity == null) { LCSConsole.WriteError("资源加载实体数据为空,不能加载!"); return; } if (string.IsNullOrEmpty(entity.ResName)) { LCSConsole.WriteError("加载资源的名字为空, resName = string.Empty"); return; } if (string.IsNullOrEmpty(entity.BundlePath)) { LCSConsole.WriteError("加载资源的路径, BundlePath = string.Empty"); return; } ResourceRequest request = Resources.LoadAsync(entity.BundlePath); LCCoroutine.Instance.StartCoroutine(StartLoad(request, entity, callback)); }
/// <summary> /// 异步加载资源完成回调 /// /// 增加异步加载的资源管理 /// </summary> /// <param name="entity"></param> private static void AsyncLoadCallback(LoadSourceEntity entity) { if (null == entity) { LCSConsole.WriteError("资源加载回调数据为空! entity = null"); return; } if (null == entity.LoadObj) { LCSConsole.WriteError("资源回调中加载的资源为空! entity.LoadObj = null"); return; } switch (entity.Type) { case LoadType.Object: if (entity.CallObject == null) { return; } entity.CallObject(entity.ResName, entity.LoadObj); break; case LoadType.GameObject: if (entity.CallGameObject == null) { return; } entity.CallGameObject(entity.ResName, entity.LoadObj as GameObject); break; case LoadType.Texture2D: if (entity.CallTexture == null) { return; } entity.CallTexture(entity.ResName, entity.LoadObj as Texture2D); break; case LoadType.AudioClip: if (entity.CallAudioClip == null) { return; } entity.CallAudioClip(entity.ResName, entity.LoadObj as AudioClip); break; case LoadType.UIAtlas: if (entity.CallUIAtlas == null) { return; } entity.CallUIAtlas(entity.ResName, entity.LoadObj as UIAtlas); break; case LoadType.AudioSource: if (entity.CallAudioSource == null) { return; } entity.CallAudioSource(entity.ResName, entity.LoadObj as AudioSource); break; } Add <LCSManageSource>(entity.ResName, entity); }