From 70a8c3d6b20088a2961038bd5d2b60a97d067f56 Mon Sep 17 00:00:00 2001 From: tanghai Date: Thu, 22 Jun 2017 18:06:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E7=AE=A1=E7=90=86=E7=B1=BB?= =?UTF-8?q?=E5=BA=94=E8=AF=A5=E4=B8=8D=E9=9C=80=E8=A6=81=E7=83=AD=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/Component/ResourcesComponent.cs | 145 ++++++++++++++++++ .../Component/ResourcesComponent.cs.meta | 12 ++ Unity/Unity.csproj | 13 +- 3 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 Unity/Assets/Scripts/Component/ResourcesComponent.cs create mode 100644 Unity/Assets/Scripts/Component/ResourcesComponent.cs.meta diff --git a/Unity/Assets/Scripts/Component/ResourcesComponent.cs b/Unity/Assets/Scripts/Component/ResourcesComponent.cs new file mode 100644 index 00000000..5cb7f460 --- /dev/null +++ b/Unity/Assets/Scripts/Component/ResourcesComponent.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using UnityEngine; + +namespace Model +{ + [ObjectEvent(EntityEventId.ResourcesComponent)] + public class ResourcesComponent: Component + { + public static AssetBundleManifest AssetBundleManifestObject { get; set; } + + private readonly Dictionary resourceCache = new Dictionary(); + + private readonly Dictionary bundleCaches = new Dictionary(); + + public K GetReference(string bundle, string prefab, string key) where K : class + { + GameObject gameObject = this.GetAsset(bundle, prefab); + return gameObject.GetComponent().Get(key); + } + + public K GetAsset(string bundleName, string prefab) where K : class + { + string path = $"{bundleName}.unity3d/{prefab}".ToLower(); + + UnityEngine.Object resource = null; + if (this.resourceCache.TryGetValue(path, out resource)) + { + return resource as K; + } + + if (Define.LoadResourceType == LoadResourceType.Async) + { + if (!this.bundleCaches.ContainsKey($"{bundleName}.unity3d".ToLower())) + { + return null; + } + + throw new Exception($"异步加载资源,资源不在resourceCache中: {bundleName} {path}"); + } + + try + { + resource = ResourceHelper.LoadResource(bundleName, prefab); + this.resourceCache.Add(path, resource); + } + catch (Exception e) + { + throw new Exception($"加载资源出错,输入路径:{path}", e); + } + + return resource as K; + } + + public async Task DownloadAndCacheAsync(string uri, string assetBundleName) + { + assetBundleName = (assetBundleName + ".unity3d").ToLower(); + + AssetBundle assetBundle; + // 异步下载资源 + string url = uri + "StreamingAssets/" + assetBundleName; + int count = 0; + TimerComponent timerComponent = Game.Scene.GetComponent(); + while (true) + { + WWWAsync wwwAsync = null; + try + { + ++count; + if (count > 1) + { + await timerComponent.WaitAsync(2000); + } + + if (this.Id == 0) + { + return; + } + + wwwAsync = new WWWAsync(); + + await wwwAsync.LoadFromCacheOrDownload(url, ResourcesComponent.AssetBundleManifestObject.GetAssetBundleHash(assetBundleName)); + assetBundle = wwwAsync.www.assetBundle; + + break; + } + catch (Exception e) + { + Log.Error(e.ToString()); + } + finally + { + wwwAsync?.Dispose(); + } + } + + if (!assetBundle.isStreamedSceneAssetBundle) + { + // 异步load资源到内存cache住 + UnityEngine.Object[] assets; + AssetBundleLoaderAsync assetBundleLoaderAsync = null; + try + { + assetBundleLoaderAsync = new AssetBundleLoaderAsync(assetBundle); + + assets = await assetBundleLoaderAsync.LoadAllAssetsAsync(); + + } + finally + { + assetBundleLoaderAsync?.Dispose(); + } + + + foreach (UnityEngine.Object asset in assets) + { + string path = $"{assetBundleName}/{asset.name}".ToLower(); + this.resourceCache[path] = asset; + } + } + + if (this.bundleCaches.ContainsKey(assetBundleName)) + { + throw new Exception($"重复加载资源: {assetBundleName}"); + } + this.bundleCaches[assetBundleName] = assetBundle; + } + + public override void Dispose() + { + if (this.Id == 0) + { + return; + } + + base.Dispose(); + + foreach (var assetBundle in bundleCaches) + { + assetBundle.Value?.Unload(true); + } + } + } +} \ No newline at end of file diff --git a/Unity/Assets/Scripts/Component/ResourcesComponent.cs.meta b/Unity/Assets/Scripts/Component/ResourcesComponent.cs.meta new file mode 100644 index 00000000..9e4d0368 --- /dev/null +++ b/Unity/Assets/Scripts/Component/ResourcesComponent.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 509cf7df1ed2a52438a824cb162c208a +timeCreated: 1498124686 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Unity/Unity.csproj b/Unity/Unity.csproj index d393b4ae..df0f8628 100644 --- a/Unity/Unity.csproj +++ b/Unity/Unity.csproj @@ -12,15 +12,12 @@ {E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} .NETFramework v4.6 - - - - + + Game:1 StandaloneWindows64:19 2017.1.0b8 - - + 6 @@ -377,6 +374,7 @@ + @@ -440,7 +438,6 @@ - - \ No newline at end of file + -- GitLab