Unity?AssetPostprocessor模型函數(shù)Model實(shí)用案例深入解析
Unity AssetPostprocessor模型相關(guān)函數(shù)詳解
在Unity中,AssetPostprocessor是一個非常有用的工具,它可以在導(dǎo)入資源時自動執(zhí)行一些操作。在本文中,我們將重點(diǎn)介紹AssetPostprocessor中與模型相關(guān)的函數(shù),并提供多個使用例子。
OnPostprocessModel
OnPostprocessModel是AssetPostprocessor中與模型相關(guān)的主要函數(shù)。它在導(dǎo)入模型時自動調(diào)用,并允許我們對模型進(jìn)行一些自定義操作。下面是一個簡單的例子:
using UnityEngine; using UnityEditor; public class MyModelPostprocessor : AssetPostprocessor { void OnPostprocessModel(GameObject model) { // 在這里對模型進(jìn)行自定義操作 } }
在這個例子中,我們創(chuàng)建了名為MyModelPostprocessor的AssetPostprocessor類,并重寫了OnPostprocessModel函數(shù)。在這個函數(shù)中,我們可以對導(dǎo)入的模型進(jìn)行自定義操作。
下面是一些常見的用:
1. 修改模型的材質(zhì)
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++) { // 修改材質(zhì) materials[i] = new Material(Shader.Find("Standard")); } renderer.sharedMaterials = materials; } }
在這個例子中,我們獲取了模型中所有的Renderer組件,并遍歷每個Renderer的材質(zhì)。然后,我們將每個材質(zhì)替換為一個新的Standard材質(zhì)。
2. 修改模型的網(wǎng)格
void OnPostprocessModel(GameObject model) { MeshFilter[] meshFilters = model.GetComponentsInChildren<MeshFilter>(); foreach (MeshFilter meshFilter in meshFilters) { // 修改網(wǎng)格 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的網(wǎng)格。然后,我們將每個網(wǎng)格的頂點(diǎn)向上移動一個單位。
3. 修改模型的Transform
void OnPostprocessModel(GameObject model) { model.transform.localScale = Vector3.one * 2; model.transform.position = Vector3.zero; model.transform.rotation = Quaternion.identity; }
在這個例子中,我們直接修改了模型的Transform組件,將其縮放為原來的兩倍,移動到原點(diǎn),旋轉(zhuǎn)為默認(rèn)的旋轉(zhuǎn)。
OnPreprocessModel
OnPreprocessModel是AssetPostprocessor中與模型相關(guān)的另一個函數(shù)。它在導(dǎo)入模型之前自動調(diào)用,并允許我們在導(dǎo)入之前對模型進(jìn)行一些自定義操作。下面是一個簡單的例子:
using UnityEngine; using UnityEditor; public class MyModelPostprocessor : AssetPostprocessor { void OnPreprocessModel() { // 在這里對模型進(jìn)行自定義操作 } }
在這個例子中,我們創(chuàng)建了一個名為MyModelPostprocessor的Assetprocessor類,并重寫了OnPreprocessModel函數(shù)。在這個函數(shù)中,我們可以在導(dǎo)入模型之前對模型進(jìn)行自定義操作。
下面是一些常見的用例:
1. 修改模型的導(dǎo)入設(shè)置
void OnPreprocessModel() { ModelImporter importer =Importer as ModelImporter; importer.importMaterials = false; importer.importAnimation = false; importer.importTangents = ModelImporterTangents.None; }
在這個例子中,我們獲取了ModelImporter對象,并修改了導(dǎo)入模型的一些設(shè)置,例如不導(dǎo)入材質(zhì)、動畫和切線。
2. 修改模型的導(dǎo)入路徑
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對象,并修改了導(dǎo)入模型的路徑和一些動畫設(shè)置,例如動畫名稱、循環(huán)模式、曲線和事件。
OnPostprocessGameObjectWithUserProperties
OnPostprocessGameObjectWithUserProperties是AssetPostprocessor中與用戶自定義屬性相關(guān)的函數(shù)。它在導(dǎo)入帶有用戶自定義屬性的游戲?qū)ο髸r自動調(diào)用,并允許我們對游戲?qū)ο筮M(jìn)行一些自定義操作。下面是一個簡單的例子:
using UnityEngine; using UnityEditor; public class MyGameObjectPostprocessor : AssetPostprocessor { void OnPostprocessGameObjectWithUserProperties(GameObject gameObject, string[] propNames, object[] values) { // 在這里對游戲?qū)ο筮M(jìn)行自定義操作 } }
在這個例子中,我們創(chuàng)建了一個名為MyGameObjectPostprocessor的AssetPostprocessor類,并重寫了OnPostprocessGameObjectWithUserProperties函數(shù)。在這個函數(shù)中,我們可以對導(dǎo)入的游戲?qū)ο筮M(jìn)行自定義操作。
下面是一個常見的用例:
1. 修改游戲?qū)ο蟮牟馁|(zhì)
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++) { // 修改材質(zhì) materials[i] = new Material(Shader.Find("Standard")); } renderer.sharedMaterials = materials; } }
在這個例子中,我們獲取了游戲?qū)ο笾兴械腞enderer組件,并遍歷每個Renderer的材質(zhì)。然后,我們將每個材質(zhì)替換為一個新的Standard材質(zhì)。
總結(jié)
在本文中,我們介紹了AssetPostprocessor中與模型相關(guān)的函數(shù),并提供了多個使用例子。通過使用這些函數(shù),我們可以導(dǎo)入模型時自動執(zhí)行一些自定義操作,從而提高工作效率。
以上就是Unity AssetPostprocessor模型Model實(shí)用案例深入解析的詳細(xì)內(nèi)容,更多關(guān)于Unity AssetPostprocessor Model的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
深入C# 4.0 新特性dynamic、可選參數(shù)、命名參數(shù)的詳細(xì)介紹
本篇文章是對C# 4.0 新特性dynamic、可選參數(shù)、命名參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#中datagridview使用tooltip控件顯示單元格內(nèi)容的方法
這篇文章主要介紹了C#中datagridview使用tooltip控件顯示單元格內(nèi)容的方法,實(shí)例分析了C#控件的相關(guān)使用技巧,需要的朋友可以參考下2016-06-06C#利用DesignSurface如何實(shí)現(xiàn)簡單的窗體設(shè)計(jì)器
這篇文章主要介紹了C#利用DesignSurface如何實(shí)現(xiàn)簡單窗體設(shè)計(jì)器的相關(guān)資料,文中通過圖文及示例代碼介紹的很詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-02-02C#導(dǎo)出pdf的實(shí)現(xiàn)方法(瀏覽器不預(yù)覽直接下載)
這篇文章主要給大家介紹了關(guān)于C#導(dǎo)出pdf的實(shí)現(xiàn)方法,實(shí)現(xiàn)后瀏覽器不預(yù)覽就可以直接下載,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Unity使用LineRender實(shí)現(xiàn)簽名效果
這篇文章主要為大家詳細(xì)介紹了Unity使用LineRender實(shí)現(xiàn)簽名效果,制作簽名功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10C# 設(shè)計(jì)模式系列教程-狀態(tài)模式
狀態(tài)模式主要解決的是當(dāng)控制一個對象狀態(tài)轉(zhuǎn)換的條件表達(dá)式過于復(fù)雜時的情況。把狀態(tài)的判斷邏輯轉(zhuǎn)移到表示不同的一系列類當(dāng)中,可以把復(fù)雜的邏輯判斷簡單化。2016-06-06快速解決owin返回json字符串多帶了雙引號"多了重string轉(zhuǎn)義字符串
下面小編就為大家?guī)硪黄焖俳鉀Qowin返回json字符串多帶了雙引號"多了重string轉(zhuǎn)義字符串。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08