Browse Source

1.修复windows上更新资源删除Version.txt的bug
2.ResourceComponent更新,不使用拼接path的方法来记录资源,减少拼接path的gc。
3.增加ab包依赖关系缓存,不用每次重新计算依赖关系

tanghai 7 years ago
parent
commit
9008436348

+ 1 - 1
Unity/Assets/Editor/BuildEditor/BuildEditor.cs

@@ -45,7 +45,7 @@ namespace ETEditor
 			GetWindow(typeof(BuildEditor));
 		}
 
-		private void OnGUI()
+		private void OnGUI() 
 		{
 			this.platformType = (PlatformType)EditorGUILayout.EnumPopup(platformType);
 			this.isBuildExe = EditorGUILayout.Toggle("是否打包EXE: ", this.isBuildExe);

+ 0 - 4
Unity/Assets/Editor/BuildEditor/BuildHelper.cs

@@ -117,10 +117,6 @@ namespace ETEditor
 		{
 			foreach (string file in Directory.GetFiles(dir))
 			{
-				if (file.EndsWith(".manifest"))
-				{
-					continue;
-				}
 				string md5 = MD5Helper.FileMD5(file);
 				FileInfo fi = new FileInfo(file);
 				long size = fi.Length;

+ 0 - 50
Unity/Assets/Scripts/Helper/ResourcesHelper.cs

@@ -16,55 +16,5 @@ namespace ETModel
 		{
 			return Resources.Load(path);
 		}
-
-		public static string[] GetDependencies(string assetBundleName)
-		{
-			string[] dependencies = new string[0];
-			if (!Define.IsAsync)
-			{
-#if UNITY_EDITOR
-				dependencies = AssetDatabase.GetAssetBundleDependencies(assetBundleName, true);
-#endif
-			}
-			else
-			{
-				dependencies = ResourcesComponent.AssetBundleManifestObject.GetAllDependencies(assetBundleName);
-			}
-			return dependencies;
-		}
-
-		public static string[] GetSortedDependencies(string assetBundleName)
-		{
-			Dictionary<string, int> info = new Dictionary<string, int>();
-			List<string> parents = new List<string>();
-			CollectDependencies(parents, assetBundleName, info);
-			string[] ss = info.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
-			return ss;
-		}
-
-		public static void CollectDependencies(List<string> parents, string assetBundleName, Dictionary<string, int> info)
-		{
-			parents.Add(assetBundleName);
-			string[] deps = GetDependencies(assetBundleName);
-			foreach (string parent in parents)
-			{
-				if (!info.ContainsKey(parent))
-				{
-					info[parent] = 0;
-				}
-				info[parent] += deps.Length;
-			}
-
-
-			foreach (string dep in deps)
-			{
-				if (parents.Contains(dep))
-				{
-					throw new Exception($"包有循环依赖,请重新标记: {assetBundleName} {dep}");
-				}
-				CollectDependencies(parents, dep, info);
-			}
-			parents.RemoveAt(parents.Count - 1);
-		}
 	}
 }

+ 5 - 0
Unity/Assets/Scripts/Module/AssetsBundle/BundleDownloaderComponent.cs

@@ -74,6 +74,11 @@ namespace ETModel
 					{
 						continue;
 					}
+
+					if (fileInfo.Name == "Version.txt")
+					{
+						continue;
+					}
 					
 					fileInfo.Delete();
 				}

+ 158 - 69
Unity/Assets/Scripts/Module/AssetsBundle/ResourcesComponent.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
+using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using UnityEngine;
@@ -32,6 +33,8 @@ namespace ETModel
 
 		public ABInfo(string name, AssetBundle ab)
 		{
+			this.InstanceId = IdGenerater.GenerateId();
+			
 			this.Name = name;
 			this.AssetBundle = ab;
 			this.RefCount = 1;
@@ -47,23 +50,136 @@ namespace ETModel
 
 			base.Dispose();
 
-			//Log.Debug($"desdroy assetbundle: {this.Name}");
+			Log.Debug($"desdroy assetbundle: {this.Name}");
 
 			this.AssetBundle?.Unload(true);
 		}
 	}
+	
+	// 用于字符串转换,减少GC
+	public static class AssetBundleHelper
+	{
+		public static readonly Dictionary<int, string> IntToStringDict = new Dictionary<int, string>();
+		
+		public static readonly Dictionary<string, string> StringToABDict = new Dictionary<string, string>();
+
+		public static readonly Dictionary<string, string> BundleNameToLowerDict = new Dictionary<string, string>() 
+		{
+			{ "StreamingAssets", "StreamingAssets" }
+		};
+		
+		// 缓存包依赖,不用每次计算
+		public static Dictionary<string, string[]> DependenciesCache = new Dictionary<string, string[]>();
+
+		public static string IntToString(this int value)
+		{
+			string result;
+			if (IntToStringDict.TryGetValue(value, out result))
+			{
+				return result;
+			}
+
+			result = value.ToString();
+			IntToStringDict[value] = result;
+			return result;
+		}
+		
+		public static string StringToAB(this string value)
+		{
+			string result;
+			if (StringToABDict.TryGetValue(value, out result))
+			{
+				return result;
+			}
+
+			result = value + ".unity3d";
+			StringToABDict[value] = result;
+			return result;
+		}
+
+		public static string IntToAB(this int value)
+		{
+			return value.IntToString().StringToAB();
+		}
+		
+		public static string BundleNameToLower(this string value)
+		{
+			string result;
+			if (BundleNameToLowerDict.TryGetValue(value, out result))
+			{
+				return result;
+			}
+
+			result = value.ToLower();
+			BundleNameToLowerDict[value] = result;
+			return result;
+		}
+		
+		public static string[] GetDependencies(string assetBundleName)
+		{
+			string[] dependencies = new string[0];
+			if (DependenciesCache.TryGetValue(assetBundleName,out dependencies))
+			{
+				return dependencies;
+			}
+			if (!Define.IsAsync)
+			{
+#if UNITY_EDITOR
+				dependencies = AssetDatabase.GetAssetBundleDependencies(assetBundleName, true);
+#endif
+			}
+			else
+			{
+				dependencies = ResourcesComponent.AssetBundleManifestObject.GetAllDependencies(assetBundleName);
+			}
+			DependenciesCache.Add(assetBundleName, dependencies);
+			return dependencies;
+		}
+
+		public static string[] GetSortedDependencies(string assetBundleName)
+		{
+			Dictionary<string, int> info = new Dictionary<string, int>();
+			List<string> parents = new List<string>();
+			CollectDependencies(parents, assetBundleName, info);
+			string[] ss = info.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
+			return ss;
+		}
+
+		public static void CollectDependencies(List<string> parents, string assetBundleName, Dictionary<string, int> info)
+		{
+			parents.Add(assetBundleName);
+			string[] deps = GetDependencies(assetBundleName);
+			foreach (string parent in parents)
+			{
+				if (!info.ContainsKey(parent))
+				{
+					info[parent] = 0;
+				}
+				info[parent] += deps.Length;
+			}
+
+
+			foreach (string dep in deps)
+			{
+				if (parents.Contains(dep))
+				{
+					throw new Exception($"包有循环依赖,请重新标记: {assetBundleName} {dep}");
+				}
+				CollectDependencies(parents, dep, info);
+			}
+			parents.RemoveAt(parents.Count - 1);
+		}
+	}
+	
 
 	public class ResourcesComponent : Component
 	{
 		public static AssetBundleManifest AssetBundleManifestObject { get; set; }
 
-		private readonly Dictionary<string, UnityEngine.Object> resourceCache = new Dictionary<string, UnityEngine.Object>();
+		private readonly Dictionary<string, Dictionary<string, UnityEngine.Object>> resourceCache = new Dictionary<string, Dictionary<string, UnityEngine.Object>>();
 
 		private readonly Dictionary<string, ABInfo> bundles = new Dictionary<string, ABInfo>();
 
-		// lru缓存队列
-		private readonly QueueDictionary<string, ABInfo> cacheDictionary = new QueueDictionary<string, ABInfo>();
-
 		public override void Dispose()
 		{
 			if (this.IsDisposed)
@@ -78,26 +194,22 @@ namespace ETModel
 				abInfo.Value?.AssetBundle?.Unload(true);
 			}
 
-			while (cacheDictionary.Count > 0)
-			{
-				ABInfo abInfo = this.cacheDictionary.FirstValue;
-				this.cacheDictionary.Dequeue();
-				abInfo.AssetBundle?.Unload(true);
-			}
-
 			this.bundles.Clear();
-			this.cacheDictionary.Clear();
 			this.resourceCache.Clear();
 		}
 
 		public UnityEngine.Object GetAsset(string bundleName, string prefab)
 		{
-			string path = $"{bundleName}/{prefab}".ToLower();
+			Dictionary<string, UnityEngine.Object> dict;
+			if (!this.resourceCache.TryGetValue(bundleName.BundleNameToLower(), out dict))
+			{
+				throw new Exception($"not found asset: {bundleName} {prefab}");
+			}
 
 			UnityEngine.Object resource = null;
-			if (!this.resourceCache.TryGetValue(path, out resource))
+			if (!dict.TryGetValue(prefab, out resource))
 			{
-				throw new Exception($"not found asset: {path}");
+				throw new Exception($"not found asset: {bundleName} {prefab}");
 			}
 
 			return resource;
@@ -107,7 +219,7 @@ namespace ETModel
 		{
 			assetBundleName = assetBundleName.ToLower();
 
-			string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
+			string[] dependencies = AssetBundleHelper.GetSortedDependencies(assetBundleName);
 
 			//Log.Debug($"-----------dep unload {assetBundleName} dep: {dependencies.ToList().ListToString()}");
 			foreach (string dependency in dependencies)
@@ -125,10 +237,11 @@ namespace ETModel
 			{
 				throw new Exception($"not found assetBundle: {assetBundleName}");
 			}
-
-			//Log.Debug($"---------- unload one bundle {assetBundleName} refcount: {abInfo.RefCount}");
+			
+			Log.Debug($"---------- unload one bundle {assetBundleName} refcount: {abInfo.RefCount - 1}");
 
 			--abInfo.RefCount;
+            
 			if (abInfo.RefCount > 0)
 			{
 				return;
@@ -136,15 +249,7 @@ namespace ETModel
 
 
 			this.bundles.Remove(assetBundleName);
-
-			// 缓存10个包
-			this.cacheDictionary.Enqueue(assetBundleName, abInfo);
-			if (this.cacheDictionary.Count > 10)
-			{
-				abInfo = this.cacheDictionary[this.cacheDictionary.FirstKey];
-				this.cacheDictionary.Dequeue();
-				abInfo.Dispose();
-			}
+			abInfo.Dispose();
 			//Log.Debug($"cache count: {this.cacheDictionary.Count}");
 		}
 
@@ -156,8 +261,7 @@ namespace ETModel
 		public void LoadBundle(string assetBundleName)
 		{
 			assetBundleName = assetBundleName.ToLower();
-			string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
-
+			string[] dependencies = AssetBundleHelper.GetSortedDependencies(assetBundleName);
 			//Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
 			foreach (string dependency in dependencies)
 			{
@@ -167,29 +271,30 @@ namespace ETModel
 				}
 				this.LoadOneBundle(dependency);
 			}
-		}
+        }
 
-		public void LoadOneBundle(string assetBundleName)
+		public void AddResource(string bundleName, string assetName, UnityEngine.Object resource)
 		{
-			//Log.Debug($"---------------load one bundle {assetBundleName}");
-			ABInfo abInfo;
-			if (this.bundles.TryGetValue(assetBundleName, out abInfo))
+			Dictionary<string, UnityEngine.Object> dict;
+			if (!this.resourceCache.TryGetValue(bundleName.BundleNameToLower(), out dict))
 			{
-				++abInfo.RefCount;
-				return;
+				dict = new Dictionary<string, UnityEngine.Object>();
+				this.resourceCache[bundleName] = dict;
 			}
 
+			dict[assetName] = resource;
+		}
 
-			if (this.cacheDictionary.ContainsKey(assetBundleName))
+		public void LoadOneBundle(string assetBundleName)
+		{
+			Log.Debug($"---------------load one bundle {assetBundleName}");
+			ABInfo abInfo;
+			if (this.bundles.TryGetValue(assetBundleName, out abInfo))
 			{
-				abInfo = this.cacheDictionary[assetBundleName];
 				++abInfo.RefCount;
-				this.bundles[assetBundleName] = abInfo;
-				this.cacheDictionary.Remove(assetBundleName);
 				return;
 			}
 
-
 			if (!Define.IsAsync)
 			{
 				string[] realPath = null;
@@ -198,9 +303,8 @@ namespace ETModel
 				foreach (string s in realPath)
 				{
 					string assetName = Path.GetFileNameWithoutExtension(s);
-					string path = $"{assetBundleName}/{assetName}".ToLower();
 					UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
-					this.resourceCache[path] = resource;
+					AddResource(assetBundleName, assetName, resource);
 				}
 
 				this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
@@ -231,8 +335,7 @@ namespace ETModel
 				UnityEngine.Object[] assets = assetBundle.LoadAllAssets();
 				foreach (UnityEngine.Object asset in assets)
 				{
-					string path = $"{assetBundleName}/{asset.name}".ToLower();
-					this.resourceCache[path] = asset;
+					AddResource(assetBundleName, asset.name, asset);
 				}
 			}
 
@@ -246,11 +349,10 @@ namespace ETModel
 		/// <returns></returns>
 		public async Task LoadBundleAsync(string assetBundleName)
 		{
-			assetBundleName = assetBundleName.ToLower();
-			string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
-
-			// Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
-			foreach (string dependency in dependencies)
+            assetBundleName = assetBundleName.ToLower();
+			string[] dependencies = AssetBundleHelper.GetSortedDependencies(assetBundleName);
+            // Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
+            foreach (string dependency in dependencies)
 			{
 				if (string.IsNullOrEmpty(dependency))
 				{
@@ -258,11 +360,10 @@ namespace ETModel
 				}
 				await this.LoadOneBundleAsync(dependency);
 			}
-		}
+        }
 
 		public async Task LoadOneBundleAsync(string assetBundleName)
 		{
-			//Log.Debug($"---------------load one bundle {assetBundleName}");
 			ABInfo abInfo;
 			if (this.bundles.TryGetValue(assetBundleName, out abInfo))
 			{
@@ -270,18 +371,8 @@ namespace ETModel
 				return;
 			}
 
-
-			if (this.cacheDictionary.ContainsKey(assetBundleName))
-			{
-				abInfo = this.cacheDictionary[assetBundleName];
-				++abInfo.RefCount;
-				this.bundles[assetBundleName] = abInfo;
-				this.cacheDictionary.Remove(assetBundleName);
-				return;
-			}
-
-
-			if (!Define.IsAsync)
+            //Log.Debug($"---------------load one bundle {assetBundleName}");
+            if (!Define.IsAsync)
 			{
 				string[] realPath = null;
 #if UNITY_EDITOR
@@ -289,9 +380,8 @@ namespace ETModel
 				foreach (string s in realPath)
 				{
 					string assetName = Path.GetFileNameWithoutExtension(s);
-					string path = $"{assetBundleName}/{assetName}".ToLower();
 					UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
-					this.resourceCache[path] = resource;
+					AddResource(assetBundleName, assetName, resource);
 				}
 
 				this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
@@ -326,8 +416,7 @@ namespace ETModel
 				}
 				foreach (UnityEngine.Object asset in assets)
 				{
-					string path = $"{assetBundleName}/{asset.name}".ToLower();
-					this.resourceCache[path] = asset;
+					AddResource(assetBundleName, asset.name, asset);
 				}
 			}
 

+ 1 - 1
Unity/Assets/StreamingAssets/Version.txt

@@ -1 +1 @@
-{"Version":0,"TotalSize":0,"FileInfoDict":{"code.unity3d":{"File":"code.unity3d","MD5":"717b465e058569de3338bd35e3b61010","Size":53153},"config.unity3d":{"File":"config.unity3d","MD5":"a12f131715c3317791fadca3d5a09501","Size":1832},"StreamingAssets":{"File":"StreamingAssets","MD5":"e5088c233f6a4d00e219c532113ab8e1","Size":1100},"uilobby.unity3d":{"File":"uilobby.unity3d","MD5":"2bbc2e612dc5a3a75ced90889bf599e4","Size":6983},"uilogin.unity3d":{"File":"uilogin.unity3d","MD5":"adceeab2dff8ef77e7cf1a936f5b7f19","Size":7716},"unit.unity3d":{"File":"unit.unity3d","MD5":"88f3ced17e15a52a8da806a53cf60300","Size":563234},"Version.txt":{"File":"Version.txt","MD5":"ce8f31db907b74aee34a2c53d25c3e64","Size":710}}}
+{"Version":0,"TotalSize":0,"FileInfoDict":{"code.unity3d":{"File":"code.unity3d","MD5":"571dd8efb2718cbad37d3d662f2609ec","Size":61778},"code.unity3d.manifest":{"File":"code.unity3d.manifest","MD5":"8c085ae636cc57ee9ab9c2eb07a92cdc","Size":657},"config.unity3d":{"File":"config.unity3d","MD5":"5d66c8fe19b295bf0fbb9643ba49367d","Size":1832},"config.unity3d.manifest":{"File":"config.unity3d.manifest","MD5":"30d1d4f7e691402897e060bbca3b6dd7","Size":623},"StreamingAssets":{"File":"StreamingAssets","MD5":"a7de6c4a297376ad9387d84a404a355d","Size":1100},"StreamingAssets.manifest":{"File":"StreamingAssets.manifest","MD5":"c301563dbb951e3dbb0ff87966a55c80","Size":388},"uilobby.unity3d":{"File":"uilobby.unity3d","MD5":"6a99502cbef4d2d3ec0ae14e1d98870a","Size":6983},"uilobby.unity3d.manifest":{"File":"uilobby.unity3d.manifest","MD5":"0c5b8c3ee1dcb36d228e531429bcf72c","Size":1110},"uilogin.unity3d":{"File":"uilogin.unity3d","MD5":"1914ea28a75e47fcd5f2f4c047f42067","Size":7716},"uilogin.unity3d.manifest":{"File":"uilogin.unity3d.manifest","MD5":"b263c81e4a771be64f89e2becc46307d","Size":1202},"unit.unity3d":{"File":"unit.unity3d","MD5":"8f9a8f9db6ba375e857d42c60fb13fe9","Size":563234},"unit.unity3d.manifest":{"File":"unit.unity3d.manifest","MD5":"a1feb8b63641c1736b3674e9b2fe2974","Size":850},"Version.txt":{"File":"Version.txt","MD5":"1d76720b1850fe3b1d24a63e9633f671","Size":1389}}}

+ 0 - 2
Unity/Assets/StreamingAssets/Version.txt.meta

@@ -1,7 +1,5 @@
 fileFormatVersion: 2
 guid: d0629e7fcea294e479c86a7b5fe33268
-timeCreated: 1531539482
-licenseType: Pro
 DefaultImporter:
   externalObjects: {}
   userData: 

+ 7 - 2
Unity/ProjectSettings/UnityConnectSettings.asset

@@ -3,12 +3,14 @@
 --- !u!310 &1
 UnityConnectSettings:
   m_ObjectHideFlags: 0
-  m_Enabled: 0
+  m_Enabled: 1
   m_TestMode: 0
   m_TestEventUrl: 
   m_TestConfigUrl: 
+  m_TestInitMode: 0
   CrashReportingSettings:
     m_EventUrl: https://perf-events.cloud.unity3d.com/api/events/crashes
+    m_NativeEventUrl: https://perf-events.cloud.unity3d.com/symbolicate
     m_Enabled: 0
     m_CaptureEditorExceptions: 1
   UnityPurchasingSettings:
@@ -24,6 +26,9 @@ UnityConnectSettings:
     m_Enabled: 0
     m_InitializeOnStartup: 1
     m_TestMode: 0
-    m_EnabledPlatforms: 4294967295
     m_IosGameId: 
     m_AndroidGameId: 
+    m_GameIds: {}
+    m_GameId: 
+  PerformanceReportingSettings:
+    m_Enabled: 0