How to start coding with AssetPostprocessor?
I am a beginner in coding but I see the necessity for accessing the AssetPostprocessor. Apart from the official Documentation Here I found this very good example for automatically creating collision meshes, in c# (hmmm) Here I found examples like that very helpful to understand how things work. Are there any other nice examples around? Thanks
Answer by duck · Apr 08, 2010 at 05:00 PM
What you already have should be more than enough to get you started. Here's another very simple example which automatically attaches a script to any object with "enemy" in the name:
using UnityEngine; using UnityEditor; using System.Collections;
public class ModelPostProcessor : AssetPostprocessor {
void OnPostprocessModel (GameObject g) {
Apply(g.transform);
}
void Apply (Transform transform)
{
if (transform.name.ToLower().Contains("enemy"))
{
transform.gameObject.AddComponent<Enemy>();
}
// Recurse
foreach (Transform child in transform)
{
Apply(child);
}
}
}
You can use this system of detecting keywords in the model's name for a variety of very useful gameobject modifications during the import process - typically I use it for:
- adding scripts to objects
- adding colliders to objects
- adding rigidbodies to objects
- removing the renderer from objects (eg, if I'm using proxy collision objects)
You can also do similar processing based on artist-assigned model properties, using the incredibly large function, "AssetPostprocessor.OnPostprocessGameObjectWithUserProperties".
There are also other AssetPostprocessor functions, such as OnPostprocessAllAssets, which you can use for different tasks, the most useful I find being to automatically match up sets of related textures (such as bump map textures, reflection textures, lightmap textures) according to a naming convention, and create and assign the appropriate Materials.
Eg, if you had a base material with a texture called "Road_Diffuse.png", you could have the asset post processor automatically scan for "Road_Bumpmap.jpg" and "Road_Lightmap.jpg", and automatically create and assign a Material which uses the Lightmapped Bumped Specular shader, with all the textures already assigned!
(I will add examples of this to this answer when I have time - don't have time right now!)
Here's another example to apply settings to audio files:
using UnityEngine; using UnityEditor; using System.Collections;
class MyAudioPostprocessor : AssetPostprocessor {
void OnPreprocessAudio () {
AudioImporter audioImporter = (AudioImporter) assetImporter;
audioImporter.format = AudioImporterFormat.Compressed;
}
}
These kinds of automatic processing of art assets are invaluable when you have separate artists producing scenes with many elements which would otherwise need sorting out in Unity by hand after importing. You can then just give your artists the naming conventions to use.
ACE!!! Thanks, yep yep .. by name and WithUserProperties seems a really good way to go ! Thanks again.
"using the incredibly large function, AssetPostprocessor.OnPostprocessGameObjectWithUserProperties". You made me roll over and laugh and then some. That is a beast! The king of codes!