比如说你换装,需要动态从磁盘load一个模型,正常情况下如果这个模型没有被场景引用到,它都根本不会被打进安装包的。再比如说我想存储一个自己定义的配置的文件,想把它放在磁盘的某个位置,动态的读,这些需求就要动态的加载资源。
1.首先总结下unity有哪几种资源类型
unity资源的类型:
-a) Unity内置的常用asset, fbx\jpg...
- b) textasset: txt、binary等,对应了它的TextAsset类,可以直接读入文本或者二进制byte
-c) scriptable object 它是序列化的Object实例,例如把一个Object实例保存成scriptable object,读入进来后就直接在内存中变成那个实例
- d) asset bundle 它是一个资源压缩包,里面包含了一堆资源
通常我们自定义的文件类型可以通过textasset 或scriptable object 来存储,区别在于前者是一个字节或文本流,后者要对应于程序中一个定义了的类型,textasset 还通常用于资源的加密。
2.动态load资源的几种途径:
-通过Resources模块,调用它的load函数:可以直接load并返回某个类型的Object,前提是要把这个资源放在Resource命名的文件夹下,Unity不关有没有场景引用,都会将其全部打入到安装包中。
-通过bundle的形式:即将资源打成 asset bundle 放在服务器或本地磁盘,然后使用WWW模块get 下来,然后从这个bundle中load某个object。
-通过AssetDatabase.loadasset :这种方式只在editor范围内有效,游戏运行时没有这个函数,它通常是在开发中调试用的
Resources的方式需要把所有资源全部打入安装包,这对游戏的分包发布(微端)和版本升级(patch)是不利的,所以unity推荐的方式是不用它,都用bundle的方式替代,把资源达成几个小的bundle,用哪个就load哪个,这样还能分包发布和patch,但是在开发过程中,不可能没更新一个资源就打一次bundle,所以editor环境下可以使用AssetDatabase来模拟,这通常需要我们封装一个dynamic resource的loader模块,在不同的环境下做不同实现。
public class AssetBuilder
{
[MenuItem("Assets/Create SceneConfig Asset", false, -20)]
public static void MakeScene()
{
SGGame.SceneConfig config = new SGGame.SceneConfig();
AssetDatabase.CreateAsset(config, "Assets/../StreamingAssets/Scene.asset");
}
[MenuItem("Assets/Create CreditsConfig Asset", false, -20)]
public static void MakeCreditsScene()
{
SGGame.CreditsConfig config = new SGGame.CreditsConfig();
AssetDatabase.CreateAsset(config, "Assets/Resources/Config/CreditsConfig.asset");
}
[MenuItem("Assets/Create TestConfig Asset", false, -20)]
public static void MakeTestScene()
{
SGGame.TestConfig config = new SGGame.TestConfig();
AssetDatabase.CreateAsset(config, "Assets/Resources/Config/TestConfig.asset");
}
[MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking", false, -20)]
public static void BuildWithoutDependencies()
{
if (Selection.activeObject == null)
{
EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
return;
}
string selectionName = Selection.activeObject.name;
string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "unity3d");
if (path.Length > 0)
{
// Build the resource file from the active selection.
if (Application.platform == RuntimePlatform.WindowsEditor)
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
else if (Application.platform == RuntimePlatform.OSXEditor)
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
}
}
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies", false, -20)]
public static void BuildTrackDependencies()
{
if (Selection.activeObject == null)
{
EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
return;
}
string selectionName = Selection.activeObject.name;
string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "assetbundle");
if (path.Length > 0)
{
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
// ToDo: The selection currently includes everything.
// A good optimization would be to filter this array so that it includes only what we need it to include.
if (Application.platform == RuntimePlatform.WindowsEditor)
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
else if (Application.platform == RuntimePlatform.OSXEditor)
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
Selection.objects = selection;
}
}
[MenuItem("Assets/[Windows_Editor]Build AssetBundle From Selection - Track dependencies", false, -20)]
public static void BuildWindowsTrackDependencies()
{
if (Selection.activeObject == null)
{
EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
return;
}
Object[] SelectionObjects = Selection.objects;
foreach (Object activeObject in SelectionObjects)
{
string selectionName = activeObject.name;
string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "assetbundle");
if (path.Length > 0)
{
Selection.activeObject = activeObject;
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object selObj in selection)
{
Debug.Log(selObj.name);
}
// ToDo: The selection currently includes everything.
// A good optimization would be to filter this array so that it includes only what we need it to include.
BuildPipeline.BuildAssetBundle(activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
}
}
}
[MenuItem("Assets/[Android]Build AssetBundle From Selection - Track dependencies", false, -20)]
public static void BuildAndroidTrackDependencies()
{
if (Selection.activeObject == null)
{
EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
return;
}
Object[] SelectionObjects = Selection.objects;
foreach (Object activeObject in SelectionObjects)
{
string selectionName = activeObject.name;
string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "assetbundle");
if (path.Length > 0)
{
Selection.activeObject = activeObject;
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object selObj in selection)
{
Debug.Log(selObj.name);
}
// ToDo: The selection currently includes everything.
// A good optimization would be to filter this array so that it includes only what we need it to include.
BuildPipeline.BuildAssetBundle(activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android);
}
}
}
[MenuItem("Assets/[iPhone]Build AssetBundle From Selection - Track dependencies", false, -20)]
public static void BuildIPhoneTrackDependencies()
{
if (Selection.activeObject == null)
{
EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
return;
}
Object[] SelectionObjects = Selection.objects;
foreach (Object activeObject in SelectionObjects)
{
string selectionName = activeObject.name;
string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "assetbundle");
if (path.Length > 0)
{
Selection.activeObject = activeObject;
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object selObj in selection)
{
Debug.Log(selObj.name);
}
// ToDo: The selection currently includes everything.
// A good optimization would be to filter this array so that it includes only what we need it to include.
BuildPipeline.BuildAssetBundle(activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
}
}
}
}
3.动态资源的存放
有时我需要存放一些自己的文件在磁盘上,例如我想把几个bundle放在初始的安装里, unity有一个streaming asset的概念,用于提供存储接口的访问。我们需要在编辑器建立一个StreamingAssets名字的文件夹,把需要我们放在客户磁盘上的动态文件放在这个文件夹下面,这样安装后,这些文件会放在用户磁盘的指定位置,这个位置可以通过Application.streamingAssetsPath来得到。