unity BuildAssetBundles

本文介绍了一个用于Unity游戏资源打包AssetBundles的脚本实现方法。该脚本可以自动化清理旧的AssetBundles,并针对不同平台进行资源打包。此外,还实现了资源依赖性的自动管理及版本控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

		if (GUILayout.Button("Build AssetBundles"))
		{
			StartBuildAssetBundles();
		}

	public static void StartBuildAssetBundles()
	{
		string sourcePath = Application.dataPath + "/" + assetBundleSourcePath;
		var buildAssetBundlePath = Path.GetFullPath(Path.Combine(Application.dataPath, "../ab1/"));
		//ClearAssetBundlesName();
		packedFile.Clear();
		string outputPath = Path.Combine(buildAssetBundlePath, Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));
		if (Directory.Exists(outputPath))
		{
			Directory.Delete(outputPath, true);
		}
		Directory.CreateDirectory(outputPath);

		Pack(sourcePath);
		if (!Directory.Exists(outputPath))
		{
			Directory.CreateDirectory(outputPath);
		}
		//根据BuildSetting里面所激活的平台进行打包 设置过AssetBundleName的都会进行打包
		BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.ForceRebuildAssetBundle, EditorUserBuildSettings.activeBuildTarget);
		var versions = EnumFileMd5(outputPath, outputPath);
		File.WriteAllText(outputPath + "/versions.txt", versions);
        ClearAssetBundlesName();

        AssetDatabase.Refresh();

		System.Diagnostics.Process.Start(buildAssetBundlePath);
	}

	static void Pack(string source)
	{
		//Debug.Log("Pack source " + source);
		DirectoryInfo folder = new DirectoryInfo(source);
		FileSystemInfo[] files = folder.GetFileSystemInfos();
		int length = files.Length;
		for (int i = 0; i < length; i++)
		{
			if (files[i] is DirectoryInfo)
			{
				Pack(files[i].FullName);
			}
			else
			{
				if (!files[i].Name.EndsWith(".meta")
					&& !files[i].Name.EndsWith(".xlsx"))
				{
					fileWithDepends(files[i].FullName);
				}
			}
		}

		foreach (var d in dDependences)
		{
			PackItem(d.Key, AssetImporter.GetAtPath(d.Key));
		}
	}

	private static void PackItem(string dp, AssetImporter assetImporter)
	{
		string pathTmp = dp.Substring("Assets".Length + 1);
		string assetName = pathTmp.Substring(pathTmp.IndexOf("/") + 1).Replace(" ", "_");
		assetName = assetName.Replace(Path.GetExtension(assetName), UAssetBundleDownloader.AssetBundleSuffix);
		assetImporter.SetAssetBundleNameAndVariant(assetName, string.Empty);

		//fileWithDepends(assetImporter.assetPath);
	}

	//设置要打包的文件
	static void fileWithDepends(string source)
	{
		if (packedFile.Contains(source))
		{
			return;
		}
		packedFile.Add(source);

		string _source = Replace(source);
		string _assetPath = _source;
		if (_source.StartsWith(Application.dataPath))
		{
			_assetPath = "Assets" + _source.Substring(Application.dataPath.Length);
		}
		//if (dDependences.ContainsKey(_assetPath))
		//{
		//	dDependences[_assetPath]++;
		//}
		//else
		//{
		//	dDependences.Add(_assetPath, 1);
		//}
		//PackItem(_assetPath, AssetImporter.GetAtPath(_assetPath));

		//自动获取依赖项并给其资源设置AssetBundleName
		string[] dps = AssetDatabase.GetDependencies(_assetPath);
		foreach (var dp in dps)
		{
			if (dp.EndsWith(".cs"))
				continue;
			AssetImporter assetImporter = AssetImporter.GetAtPath(dp);
			if (assetImporter is TextureImporter)
			{
				TextureImporter tai = assetImporter as TextureImporter;
				if (!string.IsNullOrEmpty(tai.spritePackingTag))
				{
					tai.SetAssetBundleNameAndVariant(tai.spritePackingTag + UAssetBundleDownloader.AssetBundleSuffix, null);
				}
				else
				{
					if (dDependences.ContainsKey(dp))
					{
						dDependences[dp]++;
					}
					else
					{
						dDependences.Add(dp, 1);
					}
					//PackItem(dp, assetImporter);
				}
			}
			else
			{
				if (dDependences.ContainsKey(dp))
				{
					dDependences[dp]++;
				}
				else
				{
					dDependences.Add(dp, 1);
				}
				//PackItem(dp, assetImporter);
			}
		}
	}
	static string Replace(string s)
	{
		return s.Replace("\\", "/");
	}
	static void ClearAssetBundlesName()
	{
		int length = AssetDatabase.GetAllAssetBundleNames().Length;
		string[] oldAssetBundleNames = new string[length];
		for (int i = 0; i < length; i++)
		{
			oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
		}
		for (int j = 0; j < oldAssetBundleNames.Length; j++)
		{
			AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
		}
	}
	private static string EnumFileMd5(string sdir, string outputDir)
	{
		string sresult = "";
		foreach (var s in Directory.GetFiles(sdir))
		{
			FileInfo fi = new FileInfo(s);
			if (fi.Name.EndsWith(".aspx") || fi.Name.EndsWith(".config") || fi.Name.Contains("/mono/") || fi.Name.Contains("\\mono\\") || fi.Name.EndsWith("version.txt"))
			{
				continue;
			}

			sresult += fi.FullName.Replace("\\", "/").Replace(outputDir.Replace("\\", "/"), "");
			sresult += "|" + MD5String.GetMD5HashFromFile(s) + "|" + fi.Length;
			sresult += "\r\n";
		}
		foreach (var s in Directory.GetDirectories(sdir))
		{
			sresult += EnumFileMd5(s, outputDir);
		}
		return sresult;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值