游戏中的资源是海量的,这就带来了新的问题,大量的资源格式需要设置,比如贴图格式、模型格式、音频格式和文件格式等。我们需要结合自动化来管理资源。另外,游戏打包这一步也需要自动化。例如,每次打包前,需要对包体做一些修改,如果全都手动操作,就非常麻烦了。这一章学习Unity自动化接口。

资源导入

在Unity中导入资源后,都可以通过设置参数来直接修改它的格式,而引擎会重新压缩资源。游戏中的大量资源都是美术人员或者策划人员导入的,这个过程很容易设置成错误的资源类型,或者由于没有设置成默认的效率不高的资源类型。为了避免出现这种低级错误,我们要做的就是帮他们手动设置。

监听导入事件

资源导入后,Unity会根据设置信息自动压缩。而导入事件又分两种:压缩前的时间和压缩后的事件。如果需要修改导入参数,那么就可以监听压缩前的事件来修改它,后面就会自动执行导入操作了。如果想等导入结束后生成点什么对象,可以监听导入后事件。

继承AssetPostprocessor后,就可以监听它们的事件了

相关参考,拓展编辑器一监听资源事件:Unity游戏开发拓展编辑器1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using UnityEngine;
using UnityEditor;

public class ListeningImportEvents : AssetPostprocessor
{
//导入声音前
void OnPreprocessAudio()
{
AudioImporter audioImporter = (AudioImporter)assetImporter;
}
//导入动画前
void OnPreprocessAnimation()
{
ModelImporter modelImporter = (ModelImporter)assetImporter;
}
//导入模型前
void OnPreprocessModel()
{
ModelImporter modelImporter = (ModelImporter)assetImporter;
}
//导入贴图前
void OnPreprocessTexture()
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
}
//导入声音后
void OnPostprocessAudio(AudioClip clip)
{
Debug.Log(AssetDatabase.GetAssetPath(clip));
}
//导入模型后
void OnPostprocessModel(GameObject g)
{
Debug.Log(AssetDatabase.GetAssetPath(g));
}
//导入材质后
void OnPostprocessMaterial(Material material)
{
Debug.Log(AssetDatabase.GetAssetPath(material));
}
//导入精灵后
void OnPostprocessSprites(Texture2D texture,Sprite[] sprites)
{
Debug.Log("Sprites: " + sprites.Length);
}
//导入贴图
void OnPostprocessTexture(Texture2D texture)
{
Debug.Log("Texture2D:(" + texture.width + "x" + texture.height + ")");
}
}

资源的种类有很多,在上述代码中,我们可以分别监听资源导入前以及导入后的事件,导入前事件用于设置资源的压缩参数而导出后事件可以修改最终保存的资源文件

自动设置贴图压缩格式

比如,项目有两个目录:一个是UI、另一个是Texture。当贴图拖入UI目录下时,自动设置贴图的类型:而当贴图拖入Texture目录时,则自动设置普通模型使用的贴图。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using UnityEditor;

public class AutoCompressTextureByFolder : AssetPostprocessor
{
//导入贴图前
void OnPreprocessTexture()
{
TextureImporter textureImporter = (TextureImporter)assetImporter;

if(textureImporter.assetPath.Contains("UI"))
{
textureImporter.textureType = TextureImporterType.Sprite;
textureImporter.mipmapEnabled = false;
//设置UI贴图在3个平台下的压缩格式以及大小
TextureImporterPlatformSettings standaloneSettings = new TextureImporterPlatformSettings();
standaloneSettings.name = "Standalone";
standaloneSettings.maxTextureSize = 2048;
standaloneSettings.format = TextureImporterFormat.RGBA32;
textureImporter.SetPlatformTextureSettings(standaloneSettings);
TextureImporterPlatformSettings iPhoneSettings = new TextureImporterPlatformSettings();
iPhoneSettings.name = "iPhone";
iPhoneSettings.maxTextureSize = 2048;
iPhoneSettings.format = TextureImporterFormat.RGBA32;
iPhoneSettings.compressionQuality = 100;
iPhoneSettings.allowsAlphaSplitting = true;
textureImporter.SetPlatformTextureSettings(iPhoneSettings);
TextureImporterPlatformSettings AndroidSettings = new TextureImporterPlatformSettings();
AndroidSettings.name = "Android";
AndroidSettings.maxTextureSize = 2048;
AndroidSettings.format = TextureImporterFormat.RGBA32;
AndroidSettings.allowsAlphaSplitting = true;
textureImporter.SetPlatformTextureSettings(AndroidSettings);
}
else if(textureImporter.assetPath.Contains("Texture"))
{
textureImporter.textureType = TextureImporterType.Default;
textureImporter.mipmapEnabled = true;
//设置UI贴图在3个平台下的压缩格式以及大小
TextureImporterPlatformSettings standaloneSettings = new TextureImporterPlatformSettings();
standaloneSettings.name = "Standalone";
standaloneSettings.maxTextureSize = 2048;
standaloneSettings.format = TextureImporterFormat.DXT5;
textureImporter.SetPlatformTextureSettings(standaloneSettings);
TextureImporterPlatformSettings iPhoneSettings = new TextureImporterPlatformSettings();
iPhoneSettings.name = "iPhone";
iPhoneSettings.maxTextureSize = 2048;
iPhoneSettings.format = TextureImporterFormat.ASTC_4x4;
iPhoneSettings.compressionQuality = 100;
iPhoneSettings.allowsAlphaSplitting = true;
textureImporter.SetPlatformTextureSettings(iPhoneSettings);
TextureImporterPlatformSettings AndroidSettings = new TextureImporterPlatformSettings();
AndroidSettings.name = "Android";
AndroidSettings.maxTextureSize = 2048;
AndroidSettings.format = TextureImporterFormat.ETC2_RGB4;
AndroidSettings.allowsAlphaSplitting = true;
textureImporter.SetPlatformTextureSettings(AndroidSettings);
}
}
}

UI文件夹中的图片

当UI或者Texture文件夹中的资源是从Project窗口内别的目录移动过来的时候,我们在移动过来的资源上右键——Reimport,即可重新执行导入流程,即时更新资源设置信息。

自动设置模型

模型是FBX文件,我们可以监听模型的导入事件,自动设置它的一些参数。当导入完成后,会自动生成prefab文件。

当模型拖入Project视图中,就会立刻在根目录下生成和它同名的Prefab。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using UnityEngine;
using UnityEditor;

public class AutoGenerateFBXPrefab : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter modelImporter = (ModelImporter)assetImporter;
//设置模型动画
modelImporter.animationType = ModelImporterAnimationType.Generic;
}

void OnPostprocessModel(GameObject g)
{
PrefabUtility.SaveAsPrefabAsset(g,string.Format("Assets/{0}.prefab",g.name));
}
}

禁止模型生成材质文件

如果美术人员在三维软件中指定了模型的材质,那么在导入模型的时候,就会自动生成这个材质。

如果有两个模型,它们的FBX包含了相同的引用材质,但是有一个模型在Unity中又重新指定了不同的材质,将这两个模型设为prefab,然后再打包成AssetBundle,其中的在Unity中使用新材质的模型会因为自己本身的FBX信息将旧的材质再打包一遍,所以可以考虑关闭自动导入材质

1
2
3
4
5
6
7
8
9
10
11
using UnityEngine;
using UnityEditor;

public class StopGenerateMaterial : AssetPostprocessor
{
void OnPreprocessModel(){
ModelImporter modelImporter = (ModelImporter)assetImporter;
//禁止导入材质
modelImporter.importMaterials = false;
}
}

删除移动资源事件

导入资源不一定能满足所有要求。很多时候,资源很可能只是再内部移动一个位置,比如从一个目录移动到另一个目录下,此时我们可以监听资源删除和移动的事件

AssetModificationProcessor不同,AssetPostprocessor.OnPostprocessAllAssets方法是在所有资源完成移动和删除之后调用的,可以任意调用AssetDataBase的API,而前者不能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using UnityEngine;
using UnityEditor;

public class AssetPostModify : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedAssets,string[] deletedAssets,string[] movedAssets,string[] movedFromAssetPaths)
{
foreach (string str in importedAssets)
{
Debug.LogFormat("新导入的资源:{0}",str);
}
foreach (string str in deletedAssets)
{
Debug.LogFormat("删除的资源:{0}",str);
}
for (int i = 0; i < movedAssets.Length; i++)
{
Debug.LogFormat("移动资源位置 : from: {0} to :{1}",movedFromAssetPaths[i],movedAssets[i]);
}
}
}

我们可以通过判断资源路径来特殊处理工程中的某些资源

选择性自动设置

通常情况下,我们会设置某一个文件夹下的资源格式保持一致。但某些时候,一些相同的资源可能并不支持此格式,比如说某些贴图不支持设置压缩格式,如果另外设置新的文件夹,那么就需要修改程序的加载代码。我们可以设置一个导入的默认项,这样就能满足90%的情况,剩下的10%可以由美术人员自己来把握。

例如,以移动平台为例,Android下贴图导入自动设置成ETC2压缩格式,如果某张图确实无法用ETC2压缩,美术人员就自行勾选RGBA32,这样程序的逻辑完全不受影响了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using UnityEditor;

public class OptionalTextureSettings : AssetPostprocessor
{
void OnPreprocessTexture()
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
TextureImporterPlatformSettings textureImporterPlatformSettings = textureImporter.GetPlatformTextureSettings("Android");

if(textureImporterPlatformSettings.format != TextureImporterFormat.ETC2_RGBA8 &&
textureImporterPlatformSettings.format != TextureImporterFormat.RGBA32){
TextureImporterPlatformSettings AndroidSettings = new TextureImporterPlatformSettings();
AndroidSettings.name = "Android";
AndroidSettings.maxTextureSize = 2048;
AndroidSettings.format = TextureImporterFormat.ETC2_RGBA8;
AndroidSettings.allowsAlphaSplitting = true;
textureImporter.SetPlatformTextureSettings(AndroidSettings);
}
}
}

主动设置

前面的代码都是依赖资源导入的回调来做的操作,有时并不需要导入资源而是主动去设置某个资源格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using UnityEditor;

public class InitiativeTextureSettings : AssetPostprocessor
{
[MenuItem("Assets/SetTextureFormat/RGBA32",false,-1)]
static void SetTextureFormat()
{
//确保在Project视图中选择一个文件
if(Selection.assetGUIDs.Length > 0)
{
AssetImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(Selection.activeGameObject));
//确保选择一个贴图文件
if(importer is TextureImporter)
{
TextureImporterPlatformSettings standaloneSettings = new TextureImporterPlatformSettings();
standaloneSettings.name = "Standalone";
standaloneSettings.maxTextureSize = 2048;
standaloneSettings.format = TextureImporterFormat.RGBA32;
(importer as TextureImporter).SetPlatformTextureSettings(standaloneSettings);
importer.SaveAndReimport();//保存并且重新导入
}
}
}
}

设置TextureFormat

待保存状态

只有对象变成Dirty后,才可以进行保存。如果主动修改场景中的任意对象,场景就会变成dirty状态,此时场景旁边就会出现一个*符号。

有时通过代码设置游戏对象或者对象身上的序列化信息时,很可能不会造成dirty状态,Unity不会进行保存,所以必要时应该通过代码强制设置dirty状态。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;

public class MakeSceneModified : AssetPostprocessor
{
[MenuItem("Assets/SetSceneDirty",false)]
static void SetSceneDirty()
{
//设置场景的Dirty状态
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
//设置Prefab dirty
EditorUtility.SetDirty(AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Cube.prefab"));
//如果Prefab是Scene场景中的一个实例,需要额外调用
//PrefabUtility.RecordPrefabInstancePropertyModifications(gameObject.transform);
}
}

自动执行MenuItem

Unity内部有很多MenuItem,使用EditorApplication.ExecuteMenuItem()来快捷应用

使用下面的代码,快捷选择和复制Project中的对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using UnityEngine;
using UnityEditor;

public class ExecuteMenuItemExample
{
[MenuItem("Assets/AutoMenuItem",false)]
static void AutoMenuItem()
{
//自动选择场景
Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object>("Assets/Scenes/SampleScene.unity");
//模拟Ctrl + D的操作
EditorApplication.ExecuteMenuItem("Edit/Duplicate");
//自动选择Prefab
Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object>("Assets/Resources/Cube 1.prefab");
//模拟Ctrl + D的操作
EditorApplication.ExecuteMenuItem("Edit/Duplicate");
}
}