Unity AssetPostprocessor模型相干函数详解
在Unity中,AssetPostprocessor是一个十分有用的工具,它能够在导入资源时主动执行一些操作。在本文中,咱们将重点介绍AssetPostprocessor中与模型相干的函数,并提供多个应用例子。
OnPostprocessModel
OnPostprocessModel是AssetPostprocessor中与模型相干的次要函数。它在导入模型时主动调用,并容许咱们对模型进行一些自定义操作。上面是一个简略的例子:
using UnityEngine;using UnityEditor;public class MyModelPostprocessor : AssetPostprocessor{ void OnPostprocessModel(GameObject model) { // 在这里对模型进行自定义操作 }}
在这个例子中,咱们创立了名为MyModelPostprocessor的AssetPostprocessor类,并重写了OnPostprocessModel函数。在这个函数中,咱们能够对导入的模型进行自定义操作。
上面是一些常见的用:
1. 批改模型的材质
void OnPostprocessModel(GameObject model){ Renderer[] renderers = model.GetComponentsInChildren<Renderer>(); foreach (Renderer renderer in renderers) { Material[] materials = renderer.sharedMaterials; for (int i = 0; i < materials.Length; i++) { // 批改材质 materials[i] = new Material(Shader.Find("Standard")); } renderer.sharedMaterials = materials; }}
在这个例子中,咱们获取了模型中所有的Renderer组件,并遍历每个Renderer的材质。而后,咱们将每个材质替换为一个新的Standard材质。
2. 批改模型的网格
void OnPostprocessModel(GameObject model){ MeshFilter[] meshFilters = model.GetComponentsInChildren<MeshFilter>(); foreach (MeshFilter meshFilter in meshFilters) { // 批改网格 Mesh mesh = meshFilter.sharedMesh; Vector3[] vertices = mesh.vertices; for (int i = 0; i < vertices.Length; i++) { vertices[i] += Vector3.up; } mesh.vertices = vertices; mesh.RecalculateNormals(); }}
在这个例子中,咱们获取了模型中所有的MeshFilter组件,并遍历每个MeshFilter的网格。而后,咱们将每个网格的顶点向上挪动一个单位。
3. 批改模型的Transform
void OnPostprocessModel(GameObject model){ model.transform.localScale = Vector3.one * 2; model.transform.position = Vector3.zero; model.transform.rotation = Quaternion.identity;}
在这个例子中,咱们间接批改了模型的Transform组件,将其缩放为原来的两倍,挪动到原点,旋转为默认的旋转。
OnPreprocessModel
OnPreprocessModel是AssetPostprocessor中与模型相干的另一个函数。它在导入模型之前主动调用,并容许咱们在导入之前对模型进行一些自定义操作。上面是一个简略的例子:
using UnityEngine;using UnityEditor;public class MyModelPostprocessor : AssetPostprocessor{ void OnPreprocessModel() { // 在这里对模型进行自定义操作 }}
在这个例子中,咱们创立了一个名为MyModelPostprocessor的Assetprocessor类,并重写了OnPreprocessModel函数。在这个函数中,咱们能够在导入模型之前对模型进行自定义操作。
上面是一些常见的用例:
1. 批改模型的导入设置
void OnPreprocessModel(){ ModelImporter importer =Importer as ModelImporter; importer.importMaterials = false; importer.importAnimation = false; importer.importTangents = ModelImporterTangents.None;}
在这个例子中,咱们获取了ModelImporter对象,并批改了导入模型的一些设置,例如不导入材质、动画和切线。
2. 批改模型的导入门路
void OnPreprocess(){ ModelImporter importer = assetImporter as ModelImporter; importer.importedTakeInfos[0].name = "MyAnimation"; importer.animationType = ModelImporterAnimationType.Generic; importer.animationCompression = ModelImporterAnimationCompression.KeyframeReductionAndCompression; importer.animationPositionError = 0.01f; importer.animationRotationError = 0.01f; importer.animationScaleError = 0.01f; importer.animationWrapMode = WrapMode.Loop; importer.clipAnimations = new ModelImporterClipAnimation[] { new ModelImporterClipAnimation { name = "MyAnimation", firstFrame = 0, lastFrame = 100, loopTime = true, takeName = "MyAnimation", } }; importer.clipAnimations[0].name = "MyAnimation"; importer.clipAnimations[0].firstFrame = 0; importer.clipAnimations[0].lastFrame = 100; importer.clipAnimations[0].loopTime = true; importer.clipAnimations[0].takeName = "MyAnimation"; importer.clipAnimations[0].wrapMode = WrapMode.Loop; importer.clipAnimations[0].lockRootRotation = true; importer.clipAnimations[0].lockRootHeightY = true; importer.clipAnimations[0].lockRootPositionXZ = true; importer.clipAnimations[0].curves = new AnimationClipCurveData[] { new AnimationClipCurveData { path = "MyObject", propertyName = "m_LocalPosition.x", curve = new AnimationCurve(new Keyframe[] { new Keyframe(0, 0), new Keyframe(1, 1), new Keyframe(2, 0), }), } }; importer.clipAnimations[0].events = new AnimationEvent[] { new AnimationEvent { time = 1, functionName = "MyFunction", stringParameter = "MyParameter", } }; importer.clipAnimations[0].maskType = ClipAnimationMaskType.CopyFromOther; importer.clipAnimations[0].maskSource = "MyOtherAnimation"; importer.clipAnimations[0].maskSourceInstance = importer; importer.clipAnimations[0].maskBlendType = ClipAnimationMaskBlendType.Additive; importer.clipAnimations[0].maskNeedsUpdating = true; importer.clipAnimations[0].lockCurves = new bool[] { true, false, true, }; importer.clipAnimations[0].loopPose = true; importer.clipAnimations[0].loopBlend = true; importer.clipAnimations[0].cycleOffset = 0.5f; importer.clipAnimations[0].loopBlendOrientation = true; importer.clipAnimations[0].loopBlendPositionY = true; importer.clipAnimations[0].loopBlendPositionXZ = true; importer.clipAnimations[0].keepOriginalOrientation = true; importer.clipAnimations[0].keepOriginalPositionY = true; importer.clipAnimations[0].keepOriginalPositionXZ = true; importer.clipAnimations[0].heightFromFeet = true; importer.clipAnimations[0].mirror = true; importer.clipAnimations[0].mirrorParameterCurveNames = new string[] { "MyParameter", }; importer.clipAnimations[0].lockRootRotationX = true; importer.clipAnimations[0].lockRootRotationY = true; importer.clipAnimations[0].lockRootRotationZ = true; importer.clipAnimations[0].lockRootHeightY = true; importer.clipAnimations[0].lockRootPositionXZ = true; importer.clipAnimations[0].lockRootPositionY = true; importer.clipAnimations[0].curves = new AnimationClipCurveData[] { new AnimationClipCurveData { path = "MyObject", propertyName = "m_LocalPosition.x", curve = new AnimationCurve(new Keyframe[] { new Keyframe(0, 0), new Keyframe(1, 1), new Keyframe(2, 0), }), } };}
在这个例子中,咱们获取ModelImporter对象,并批改了导入模型的门路和一些动画设置,例如动画名称、循环模式、曲线和事件。
OnPostprocessGameObjectWithUserProperties
OnPostprocessGameObjectWithUserProperties是AssetPostprocessor中与用户自定义属性相干的函数。它在导入带有用户自定义属性的游戏对象时主动调用,并容许咱们对游戏对象进行一些自定义操作。上面是一个简略的例子:
using UnityEngine;using UnityEditor;public class MyGameObjectPostprocessor : AssetPostprocessor{ void OnPostprocessGameObjectWithUserProperties(GameObject gameObject, string[] propNames, object[] values) { // 在这里对游戏对象进行自定义操作 }}
在这个例子中,咱们创立了一个名为MyGameObjectPostprocessor的AssetPostprocessor类,并重写了OnPostprocessGameObjectWithUserProperties函数。在这个函数中,咱们能够对导入的游戏对象进行自定义操作。
上面是一个常见的用例:
1. 批改游戏对象的材质
void OnPostprocessGameObjectWithUserProperties(GameObject gameObject, string[] propNames, object[] values){ Renderer[] renderers = gameObject.GetComponentsInChildren<Renderer>(); foreach (Renderer renderer in renderers) { Material[] materials = renderer.sharedMaterials; for (int i = 0; i < materials.Length; i++) { // 批改材质 materials[i] = new Material(Shader.Find("Standard")); } renderer.sharedMaterials = materials; }}
在这个例子中,咱们获取了游戏对象中所有的Renderer组件,并遍历每个Renderer的材质。而后,咱们将每个材质替换为一个新的Standard材质。
总结
在本文中,咱们介绍了AssetPostprocessor中与模型相干的函数,并提供了多个应用例子。通过应用这些函数,咱们能够导入模型时主动执行一些自定义操作,从而进步工作效率。