网上其实有很多了,我在这里就归拢一下
模型导入设置
using UnityEditor;
public class ModelImport : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter modelimporter = assetImporter as ModelImporter;
modelimporter.importVisibility = false;
modelimporter.importCameras = false;
modelimporter.importLights = false;
modelimporter.importNormals = ModelImporterNormals.Import;
modelimporter.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
modelimporter.materialLocation = ModelImporterMaterialLocation.InPrefab;
modelimporter.SearchAndRemapMaterials(ModelImporterMaterialName.BasedOnMaterialName, ModelImporterMaterialSearch.RecursiveUp);
if (assetImporter.assetPath.Contains("Assets/Art/Builds"))
{
modelimporter.importBlendShapes = false;
modelimporter.animationType = ModelImporterAnimationType.None;
modelimporter.importAnimation = false;
}
if (modelimporter.assetPath.Contains("Assets/Art/Role"))
{
modelimporter.importBlendShapes = true;
}
}
}
关于模型的导入方法里有2个
OnPostprocessModel(GameObject)
OnPreprocessModel()
贴图导入设置
using UnityEditor;
public class TextureImport : AssetPostprocessor
{
void OnPreprocessTexture()
{
TextureImporter textureimporter = (TextureImporter)assetImporter;
if (assetImporter.assetPath.Contains("Assets/Art"))
{
textureimporter.mipmapEnabled = false;
}
}
}
注释部分是一些修改贴图压缩的设置 关于贴图的导入方法里有4个
OnPostprocessTexture(Texture2D)
OnPostprocessTexture(Texture2DArray)
OnPostprocessTexture(Texture3D)
OnPreprocessTexture()
材质球导入设置
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class MaterialImport : AssetPostprocessor
{
static List<string> discards = new List<string>
{
"Standard",
"Standard (Specular setup)",
"Autodesk Interactive"
};
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
string path = "Assets";
foreach (string asset in importedAssets)
{
if (asset.EndsWith(".mat") && asset.Contains(path))
{
OnPostprocessMaterial(AssetDatabase.LoadAssetAtPath<Material>(asset));
}
}
}
static void OnPostprocessMaterial(Material material)
{
Shader shader = Shader.Find("Custom/SimulationPBR");
if (discards.Any(s => s.Equals(material.shader.name)))
{
material.shader = shader;
MatTools.CleanMat(material);
AssetDatabase.SaveAssets();
}
}
}
我遇到了一些问题,我也没有查到怎么用。 OnPostprocessMaterial() 描述上说:
将此函数添加到一个子类中,以在材质资源完成导入时获取通知。 使用此方法修改导入期间新创建的材质资源的属性。 此方法仅由 ModelImporter 调用。
但是实际上我怎么测试也没有找到何时调用OnPostprocessMaterial() 所以我就用了OnPostprocessAllAssets(string[],string[],string[],string[]) 来监测所有的导入,路径里的文件格式决定哪个是材质球来进行引用shader的修改 我在导入材质设置里还用了一个自己写的方法MatTools.CleanMat(Material); 这个是用来清理材质球数据的,把Standard材质里多余的数据块清理掉,减少材质球文本块数量
using UnityEditor;
using UnityEngine;
public class MatTools : Editor
{
[MenuItem("Assets/Tools/ClearMatProperties")]
public static void ClearMatProperties()
{
UnityEngine.Object[] objs = Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets);
for (int i = 0; i < objs.Length; ++i)
{
Material mat = objs[i] as Material;
CleanMat(mat);
}
AssetDatabase.SaveAssets();
}
public static void CleanMat(Material material)
{
if (material)
{
SerializedObject psSource = new SerializedObject(material);
SerializedProperty emissionProperty = psSource.FindProperty("m_SavedProperties");
SerializedProperty texEnvs = emissionProperty.FindPropertyRelative("m_TexEnvs");
SerializedProperty floats = emissionProperty.FindPropertyRelative("m_Floats");
SerializedProperty colos = emissionProperty.FindPropertyRelative("m_Colors");
CleanMaterialSerializedProperty(texEnvs, material);
CleanMaterialSerializedProperty(floats, material);
CleanMaterialSerializedProperty(colos, material);
psSource.ApplyModifiedProperties();
EditorUtility.SetDirty(material);
}
}
private static void CleanMaterialSerializedProperty(SerializedProperty property, Material mat)
{
for (int j = property.arraySize - 1; j >= 0; j--)
{
string propertyName = property.GetArrayElementAtIndex(j).displayName;
Debug.Log("Find property in serialized object : " + propertyName);
if (!mat.HasProperty(propertyName))
{
if (propertyName.Equals("_MainTex"))
{
if (property.GetArrayElementAtIndex(j).FindPropertyRelative("second").FindPropertyRelative("m_Texture").objectReferenceValue != null)
{
property.GetArrayElementAtIndex(j).FindPropertyRelative("second").FindPropertyRelative("m_Texture").objectReferenceValue = null;
Debug.Log("Set _MainTex is null");
}
}
else
{
property.DeleteArrayElementAtIndex(j);
Debug.Log("Delete property in serialized object : " + propertyName);
}
}
}
}
}
关于材质球的导入方法里有3个
OnAssignMaterialModel(Material,Renderer)
OnPostprocessMaterial(Material)
OnPreprocessMaterialDescription(MaterialDescription,Material, AnimationClip[])
但是网上我也没有搜到太多关于他们的用法,有会的大佬可以留言给我。等之后我哪天会了我也会更新。
|