unity 代码设置要打包的AssetBundle

1、AssetBundle 在unity 5 中要先标志好,然后就可以打包时根据平台直接打包即可,手动设置不现实,根据文件路径来确定其归属是一个不错的选择
把需要打包的资源都存在 <span style="font-family: Arial, Helvetica, sans-serif;">AssetBundle 中,然后在Editor 目录加入下面代码</span>

using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.IO;

public class AssetBundlePostProcessor : AssetPostprocessor
{
	private const string assetBundleRootPath = "Assets/AssetBundle/";

	static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) 
	{
		foreach (var str in importedAssets)
		{
			bool isChanged = CheckModifyIsTarget (str);
			if (isChanged) 
			{
				Debug.Log("isChanged Asset: " + str);
				SetAssetBundleName (str);
			}
		}
	}

	// 检测是否是我希望设置的文件
	static bool CheckModifyIsTarget(string path)
	{
		if(Directory.Exists (path))
		{
			return false;
		}

		string fileName = Path.GetFileName (path);
		if (string.IsNullOrEmpty (fileName) || fileName.EndsWith (".meta")) 
		{
			return false;
		} 
		else 
		{
			return path.Contains (assetBundleRootPath);
		}
	}

	static void SetAssetBundleName(string path)
	{
		string asName = GetAssetNameByPath (path);

		AssetImporter importer = AssetImporter.GetAtPath (path);
		importer.SetAssetBundleNameAndVariant (asName, "");
	}

	static string GetAssetNameByPath(string path)
	{
		int index = assetBundleRootPath.Length;
		string substr = path.Substring (index);

		int lastFlashIndex = substr.LastIndexOf ('/');
		return substr.Substring (0, lastFlashIndex);
	}
}


2、在网络上看到别人的做法,感觉更好,那就是在打包的时候先设置好,然后再打包。

因为上面 1 的方法没有 reimport 就可能会有问题,而这个方法是在打包时全部设置一次,挺好的。。不过就是可能打包时间比较长。。。

每次打包都设置一次,似乎也会重复,如果要打包两次可能就会重复


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值