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实现多点触控代码
- 在Unity中实现动画的正反播放代码
- Unity UGUI教程之实现滑页效果
- unity实现摄像头跟随
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- Unity3D-5.3.5发布VR项目到Android
- [软件资讯]Unity已支持将3D游戏导出成Flash
- ShaderLab: Pass
- Unity 武器拖尾效果
- unity常见问题之20题
- Unity3D动画存储插件
- unity 赛车相机跟随物体移动
- uLua中遇到的问题
- Unity3d 协程、调用函数、委托
- Unity3d摄像机Camera参数详解
- Unity3d多线程