Practical application of Model related functions in Unity AssetPostprocessor
Unity AssetPostprocessor is an important function in the Unity engine, which can automate some scripts when importing resources in order to customize the processing of resources. Among them, Model-related functions can be used to process the imported 3D model, including modifying the material, texture, grid and other properties of the model. This article will introduce the practical application of Model-related functions and provide multiple usage examples.
ModelImporterMaterialName
The ModelImporterMaterialName function can be used to modify the material name of the model. For example, we can change the material name of all models to "DefaultMaterial". The specific implementation is as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.materialName = "DefaultMaterial";
}
}
ModelImporterMaterialSearch
The ModelImporterMaterialSearch function can be used to modify the material search path of the model. For example, we can modify the material search path of all models to "Assets/Materials". The specific implementation is as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.materialSearch = ModelImporterMaterialSearch.Local;
importer.materialSearchPath = "Assets/Materials";
}
}
ModelImporterMaterialImportMode
The ModelImporterMaterialImportMode function can be used to modify the material import mode of the model. For example, we can change the material import mode of all models to "None". The specific implementation is as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.materialImportMode = ModelImporterMaterialImportMode.None;
}
}
ModelImporterMeshCompression
The ModelImporterMeshCompression function can be used to modify the mesh compression method of the model. For example, we can modify the mesh compression method to "Off" for all models. The specific implementation is as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.meshCompression = ModelImporterMeshCompression.Off;
}
}
ModelImporterNormals
The ModelImporterNormals function can be used to modify the normal calculation method of the model. For example, we can modify the normal calculation method of all models to "Calculate". The specific implementation is as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.importNormals = ModelImporterNormals.Calculate;
}
}
ModelImporterTangents
The ModelImporterTangents function can be used to modify the tangent calculation method of the model. For example, we can modify the tangent calculation method of all models to "Calculate". The specific implementation is as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.importTangents = ModelImporterTangents.Calculate;
}
}
ModelImporterAnimationCompression
The ModelImporterAnimationCompression function can be used to modify the animation compression method of the model. For example, we can modify the animation compression method of all models to "Off". The specific implementation is as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocess()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.animationCompression = ModelImporterAnimationCompression.Off;
}
}
ModelImporterAnimationType
The ModelImporterAnimationType function can be used to modify the animation type of the model. For example, we can change the animation type of all models to "Legacy". The specific implementation is as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.animationType = ModelImporterAnimationType.Legacy;
}
}
ModelImporterRigImportMode
The ModelImporterRigImportMode function can be used to modify the bone import mode of the model. For example, we can modify the bone import mode of all models to "None". The body is implemented as follows:
using UnityEngine;
using UnityEditor;
public class MyModelPostprocessor : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter importer = assetImporter as ModelImporter;
importer.animationType = ModelImporterAnimationType.None;
}
}
in conclusion
This article introduces the practical application of Model-related functions in Unity AssetPostprocessor, and provides multiple usage examples. These functions can be used to customize the imported 3D model, including modifying the material, texture, mesh and other properties of the model. By using these functions, we can process imported 3D models more flexibly to meet different needs.