当前位置:   article > 正文

unity编译器打包类(BuildEditor)_unity3d编译

unity3d编译
public class BundleInfo
{
	 public List<string> ParentPaths = new List<string>();
}

public enum PlatformType
{
	None,
	Android,
	IOS,
	PC,
	MacOS,
}

public enum BuildType
{
	Development,
	Release,
}

public class BuildEditor : EditorWindow
{
	private readonly Dictionary<string, BundleInfo> dictionary = new Dictionary<string, BundleInfo>();

	private PlatformType platformType;
	private bool isBuildExe;
	private bool isContainAB;
	private BuildType buildType;
	private BuildOptions buildOptions = BuildOptions.AllowDebugging | BuildOptions.Development;
	private BuildAssetBundleOptions buildAssetBundleOptions = BuildAssetBundleOptions.None;

	[MenuItem("Tools/打包工具")]
	public static void ShowWindow()
	{
		GetWindow(typeof(BuildEditor));
	}

	private void OnGUI() 
	{
		this.platformType = (PlatformType)EditorGUILayout.EnumPopup("打包平台: ",platformType);
		this.isBuildExe = EditorGUILayout.Toggle("是否打包EXE: ", this.isBuildExe);
		this.isContainAB = EditorGUILayout.Toggle("是否同将资源打进EXE: ", this.isContainAB);
		this.buildType = (BuildType)EditorGUILayout.EnumPopup("BuildType: ", this.buildType);
		
		switch (buildType)
		{
			case BuildType.Development:
				this.buildOptions = BuildOptions.Development | BuildOptions.AutoRunPlayer | BuildOptions.ConnectWithProfiler | BuildOptions.AllowDebugging;
				break;
			case BuildType.Release:
				this.buildOptions = BuildOptions.None;
				break;
		}
		
		this.buildAssetBundleOptions = (BuildAssetBundleOptions)EditorGUILayout.EnumFlagsField("BuildAssetBundleOptions(可多选): ", this.buildAssetBundleOptions);

		if (GUILayout.Button("开始打包"))
		{
			if (this.platformType == PlatformType.None)
			{
				Log.Error("请选择打包平台!");
				return;
			}
			BuildHelper.Build(this.platformType, this.buildAssetBundleOptions, this.buildOptions, this.isBuildExe, this.isContainAB);
		}
	}

	private void SetPackingTagAndAssetBundle()
	{
		ClearPackingTagAndAssetBundle();

		SetIndependentBundleAndAtlas("Assets/Bundles/Independent");

		SetBundleAndAtlasWithoutShare("Assets/Bundles/UI");

		SetRootBundleOnly("Assets/Bundles/Unit");

		AssetDatabase.SaveAssets();
		AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
	}

	private static void SetNoAtlas(string dir)
	{
		List<string> paths = EditorResHelper.GetPrefabsAndScenes(dir);

		foreach (string path in paths)
		{
			List<string> pathes = CollectDependencies(path);

			foreach (string pt in pathes)
			{
				if (pt == path)
				{
					continue;
				}

				SetAtlas(pt, "", true);
			}
		}
	}

	// 会将目录下的每个prefab引用的资源强制打成一个包,不分析共享资源
	private static void SetBundles(string dir)
	{
		List<string> paths = EditorResHelper.GetPrefabsAndScenes(dir);
		foreach (string path in paths)
		{
			string path1 = path.Replace('\\', '/');
			Object go = AssetDatabase.LoadAssetAtPath<Object>(path1);

			SetBundle(path1, go.name);
		}
	}

	// 会将目录下的每个prefab引用的资源打成一个包,只给顶层prefab打包
	private static void SetRootBundleOnly(string dir)
	{
		List<string> paths = EditorResHelper.GetPrefabsAndScenes(dir);
		foreach (string path in paths)
		{
			string path1 = path.Replace('\\', '/');
			Object go = AssetDatabase.LoadAssetAtPath<Object>(path1);

			SetBundle(path1, go.name);
		}
	}

	// 会将目录下的每个prefab引用的资源强制打成一个包,不分析共享资源
	private static void SetIndependentBundleAndAtlas(string dir)
	{
		List<string> paths = EditorResHelper.GetPrefabsAndScenes(dir);
		foreach (string path in paths)
		{
			string path1 = path.Replace('\\', '/');
			Object go = AssetDatabase.LoadAssetAtPath<Object>(path1);

			AssetImporter importer = AssetImporter.GetAtPath(path1);
			if (importer == null || go == null)
			{
				Log.Error("error: " + path1);
				continue;
			}
			importer.assetBundleName = $"{go.name}.unity3d";

			List<string> pathes = CollectDependencies(path1);

			foreach (string pt in pathes)
			{
				if (pt == path1)
				{
					continue;
				}

				SetBundleAndAtlas(pt, go.name, true);
			}
		}
	}

	private static void SetBundleAndAtlasWithoutShare(string dir)
	{
		List<string> paths = EditorResHelper.GetPrefabsAndScenes(dir);
		foreach (string path in paths)
		{
			string path1 = path.Replace('\\', '/');
			Object go = AssetDatabase.LoadAssetAtPath<Object>(path1);

			SetBundle(path1, go.name);

			//List<string> pathes = CollectDependencies(path1);
			//foreach (string pt in pathes)
			//{
			//	if (pt == path1)
			//	{
			//		continue;
			//	}
			//
			//	SetBundleAndAtlas(pt, go.name);
			//}
		}
	}

	private static List<string> CollectDependencies(string o)
	{
		string[] paths = AssetDatabase.GetDependencies(o);

		//Log.Debug($"{o} dependecies: " + paths.ToList().ListToString());
		return paths.ToList();
	}

	// 分析共享资源
	private void SetShareBundleAndAtlas(string dir)
	{
		this.dictionary.Clear();
		List<string> paths = EditorResHelper.GetPrefabsAndScenes(dir);

		foreach (string path in paths)
		{
			string path1 = path.Replace('\\', '/');
			Object go = AssetDatabase.LoadAssetAtPath<Object>(path1);

			SetBundle(path1, go.name);

			List<string> pathes = CollectDependencies(path1);
			foreach (string pt in pathes)
			{
				if (pt == path1)
				{
					continue;
				}

				// 不存在则记录下来
				if (!this.dictionary.ContainsKey(pt))
				{
					// 如果已经设置了包
					if (GetBundleName(pt) != "")
					{
						continue;
					}
					Log.Info($"{path1}----{pt}");
					BundleInfo bundleInfo = new BundleInfo();
					bundleInfo.ParentPaths.Add(path1);
					this.dictionary.Add(pt, bundleInfo);

					SetAtlas(pt, go.name);

					continue;
				}

				// 依赖的父亲不一样
				BundleInfo info = this.dictionary[pt];
				if (info.ParentPaths.Contains(path1))
				{
					continue;
				}
				info.ParentPaths.Add(path1);

				DirectoryInfo dirInfo = new DirectoryInfo(dir);
				string dirName = dirInfo.Name;

				SetBundleAndAtlas(pt, $"{dirName}-share", true);
			}
		}
	}

	private static void ClearPackingTagAndAssetBundle()
	{
		//List<string> bundlePaths = EditorResHelper.GetAllResourcePath("Assets/Bundles/", true);
		//foreach (string bundlePath in bundlePaths)
		//{
		//	SetBundle(bundlePath, "", true);
		//}

		List<string> paths = EditorResHelper.GetAllResourcePath("Assets/Res", true);
		foreach (string pt in paths)
		{
			SetBundleAndAtlas(pt, "", true);
		}
	}

	private static string GetBundleName(string path)
	{
		string extension = Path.GetExtension(path);
		if (extension == ".cs" || extension == ".dll" || extension == ".js")
		{
			return "";
		}
		if (path.Contains("Resources"))
		{
			return "";
		}

		AssetImporter importer = AssetImporter.GetAtPath(path);
		if (importer == null)
		{
			return "";
		}

		return importer.assetBundleName;
	}

	private static void SetBundle(string path, string name, bool overwrite = false)
	{
		string extension = Path.GetExtension(path);
		if (extension == ".cs" || extension == ".dll" || extension == ".js")
		{
			return;
		}
		if (path.Contains("Resources"))
		{
			return;
		}

		AssetImporter importer = AssetImporter.GetAtPath(path);
		if (importer == null)
		{
			return;
		}

		if (importer.assetBundleName != "" && overwrite == false)
		{
			return;
		}

		//Log.Info(path);
		string bundleName = "";
		if (name != "")
		{
			bundleName = $"{name}.unity3d";
		}

		importer.assetBundleName = bundleName;
	}

	private static void SetAtlas(string path, string name, bool overwrite = false)
	{
		string extension = Path.GetExtension(path);
		if (extension == ".cs" || extension == ".dll" || extension == ".js")
		{
			return;
		}
		if (path.Contains("Resources"))
		{
			return;
		}

		TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
		if (textureImporter == null)
		{
			return;
		}

		if (textureImporter.spritePackingTag != "" && overwrite == false)
		{
			return;
		}

		textureImporter.spritePackingTag = name;
		AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
	}

	private static void SetBundleAndAtlas(string path, string name, bool overwrite = false)
	{
		string extension = Path.GetExtension(path);
		if (extension == ".cs" || extension == ".dll" || extension == ".js" || extension == ".mat")
		{
			return;
		}
		if (path.Contains("Resources"))
		{
			return;
		}

		AssetImporter importer = AssetImporter.GetAtPath(path);
		if (importer == null)
		{
			return;
		}

		if (importer.assetBundleName == "" || overwrite)
		{
			string bundleName = "";
			if (name != "")
			{
				bundleName = $"{name}.unity3d";
			}

			importer.assetBundleName = bundleName;
		}

		TextureImporter textureImporter = importer as TextureImporter;
		if (textureImporter == null)
		{
			return;
		}

		if (textureImporter.spritePackingTag == "" || overwrite)
		{
			textureImporter.spritePackingTag = name;
			AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/112014
推荐阅读
相关标签
  

闽ICP备14008679号