您的位置:首页 > 移动开发 > Unity3D

unity 代码设置要打包的AssetBundle

2016-06-12 13:34 609 查看
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 就可能会有问题,而这个方法是在打包时全部设置一次,挺好的。。不过就是可能打包时间比较长。。。每次打包都设置一次,似乎也会重复,如果要打包两次可能就会重复
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity