unity 导入自动强制修改资源属性
网上其实有很多了,我在这里就归拢一下模型导入设置
using UnityEditor;
public class ModelImport : AssetPostprocessor
{
//模型导入前设置
void OnPreprocessModel()
{
ModelImporter modelimporter = assetImporter as ModelImporter;
//所有都适用
//Model
modelimporter.importVisibility = false;
modelimporter.importCameras = false;
modelimporter.importLights = false;
modelimporter.importNormals = ModelImporterNormals.Import;
//Materials
modelimporter.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
modelimporter.materialLocation = ModelImporterMaterialLocation.InPrefab;
modelimporter.SearchAndRemapMaterials(ModelImporterMaterialName.BasedOnMaterialName, ModelImporterMaterialSearch.RecursiveUp);
// 判定路径可随意修改 --->"/***/"
//场景模型
if (assetImporter.assetPath.Contains("Assets/Art/Builds"))
{
//Model
modelimporter.importBlendShapes = false;
//Rig
modelimporter.animationType = ModelImporterAnimationType.None;
//Animation
modelimporter.importAnimation = false;
}
//角色模型
if (modelimporter.assetPath.Contains("Assets/Art/Role"))
{
//Model
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;
//textureimporter.maxTextureSize = 512;
}
//var importer = AssetImporter.GetAtPath("Assets/Arts/TileRes") as TextureImporter;
//{
// TextureImporterPlatformSettings androidSetting = new TextureImporterPlatformSettings();
// {
// androidSetting.name = "Android";
// androidSetting.maxTextureSize = 512;
// androidSetting.overridden = true;
// }
// // ios设置
// TextureImporterPlatformSettings iosSetting = new TextureImporterPlatformSettings();
// {
// iosSetting.name = "iPhone";
// iosSetting.maxTextureSize = 512;
// iosSetting.overridden = true;
// }
// //
// importer.SetPlatformTextureSettings(androidSetting);
// importer.SetPlatformTextureSettings(iosSetting);
// importer.SaveAndReimport();
//}
}
}
注释部分是一些修改贴图压缩的设置
关于贴图的导入方法里有4个
OnPostprocessTexture(Texture2D) //2D贴图导入后执行
OnPostprocessTexture(Texture2DArray) //2DArray贴图导入后执行
OnPostprocessTexture(Texture3D) //3D贴图导入后执行
OnPreprocessTexture() //贴图导入前执行
# 材质球导入设置
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class MaterialImport : AssetPostprocessor
{
static List<string> discards = new List<string> //如果监测到有这些shader,都会替换成上面的shader
{
&#34;Standard&#34;,
&#34;Standard (Specular setup)&#34;,
&#34;Autodesk Interactive&#34;
};
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
string path = &#34;Assets&#34;; //需要替换的路径
foreach (string asset in importedAssets)
{
if (asset.EndsWith(&#34;.mat&#34;) && asset.Contains(path))
{
OnPostprocessMaterial(AssetDatabase.LoadAssetAtPath<Material>(asset));
}
}
}
static void OnPostprocessMaterial(Material material)
{
Shader shader = Shader.Find(&#34;Custom/SimulationPBR&#34;);
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
{
public static void ClearMatProperties()
{
UnityEngine.Object[] objs = Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets);
for (int i = 0; i < objs.Length; ++i)
{
Material mat = objs as Material;
CleanMat(mat);
}
AssetDatabase.SaveAssets();
}
public static void CleanMat(Material material)
{
if (material)
{
SerializedObject psSource = new SerializedObject(material);
SerializedProperty emissionProperty = psSource.FindProperty(&#34;m_SavedProperties&#34;);
SerializedProperty texEnvs = emissionProperty.FindPropertyRelative(&#34;m_TexEnvs&#34;);
SerializedProperty floats = emissionProperty.FindPropertyRelative(&#34;m_Floats&#34;);
SerializedProperty colos = emissionProperty.FindPropertyRelative(&#34;m_Colors&#34;);
CleanMaterialSerializedProperty(texEnvs, material);
CleanMaterialSerializedProperty(floats, material);
CleanMaterialSerializedProperty(colos, material);
psSource.ApplyModifiedProperties();
EditorUtility.SetDirty(material);
}
}
/// <summary>
/// true: has useless propeties
/// </summary>
/// <param name=&#34;property&#34;></param>
/// <param name=&#34;mat&#34;></param>
private static void CleanMaterialSerializedProperty(SerializedProperty property, Material mat)
{
for (int j = property.arraySize - 1; j >= 0; j--)
{
string propertyName = property.GetArrayElementAtIndex(j).displayName;
//string propertyName = property.GetArrayElementAtIndex(j).FindPropertyRelative(&#34;first&#34;).FindPropertyRelative(&#34;name&#34;).stringValue;
Debug.Log(&#34;Find property in serialized object : &#34; + propertyName);
if (!mat.HasProperty(propertyName))
{
if (propertyName.Equals(&#34;_MainTex&#34;))
{
//_MainTex是内建属性,是置空不删除,否则UITexture等控件在获取mat.maintexture的时候会报错
if (property.GetArrayElementAtIndex(j).FindPropertyRelative(&#34;second&#34;).FindPropertyRelative(&#34;m_Texture&#34;).objectReferenceValue != null)
{
property.GetArrayElementAtIndex(j).FindPropertyRelative(&#34;second&#34;).FindPropertyRelative(&#34;m_Texture&#34;).objectReferenceValue = null;
Debug.Log(&#34;Set _MainTex is null&#34;);
}
}
else
{
property.DeleteArrayElementAtIndex(j);
Debug.Log(&#34;Delete property in serialized object : &#34; + propertyName);
}
}
}
}
}
关于材质球的导入方法里有3个
//提供源材质。返回的材质将分配给渲染器。 如果返回 null,Unity 将使用其默认材质查找/生成方法来分配材质。 系统会在导入 sourceMaterial 之前直接从模型中生成 sourceMaterial,并在执行 OnAssignMaterial 之后立即将其销毁。
OnAssignMaterialModel(Material,Renderer)
//将此函数添加到一个子类中,以在材质资源完成导入时获取通知。使用此方法修改导入期间新创建的材质资源的属性。此方法仅由 ModelImporter 调用。
OnPostprocessMaterial(Material)
//将此函数添加到一个子类中,以在材质从 Model Importer 导入时接收通知。仅当 ModelImporter.UseMaterialDescriptionPostprocessor 为 true 时调用此函数。此函数提供在模型导入过程中对材质属性和动画的用户控制。MaterialDescription 结构包含在导入的文件中读取的所有材质数据,可用于填充参数中的材质和动画剪辑。
OnPreprocessMaterialDescription(MaterialDescription,Material, AnimationClip[])
但是网上我也没有搜到太多关于他们的用法,有会的大佬可以留言给我。等之后我哪天会了我也会更新。
页:
[1]