DEV Community

Cover image for Automating Unity3D Assets Import
Omar
Omar

Posted on

Automating Unity3D Assets Import

Games aren't always made by a one-man-team but involve several developers, who are people akin to making mistakes,that in turn increase dev cost time.
Importing assets is no joke, with the default settings and different rules for different parts of your project, let's start with this premise and ask ourselves: "can we write tools to prevent common, costly mistakes ?"

the answer is yes and let's discover one cool Unity3D Class.

AssetPostprocessor:

the AssetPostProcessor class receive callbacks on importing something. it implements OnPreProcess* and OnPostProcess* methods and apply your rules to the assetImporter instance.
therefore we can write rules that prevent some common mistakes, let's take textures for instance, we can ensure:

  • setting the texture type (deault, sprite, etc..)
  • making sure Read/Write is disabled.
  • disabling mipmaps for anything that is 2D or UI (it is enabled by default).
  • forcing a max texture size
public class AssetImportSample : AssetPostprocessor {

    void OnPreprocessTexture()
    {
        if (assetPath.Contains("_UI") || assetPath.Contains("_Sprite"))
        {
            TextureImporter textureImporter = (TextureImporter)assetImporter;
            textureImporter.textureType = TextureImporterType.Sprite;
            textureImporter.spriteImportMode = SpriteImportMode.Multiple;
            textureImporter.mipmapEnabled = false; // we don't need mipmaps for 2D/UI Atlases

            if (textureImporter.isReadable)
            {
                textureImporter.isReadable = false; // make sure Read/Write is disabled
            }
            textureImporter.maxTextureSize = 1024; // force a max texture size
            Debug.Log("UI/Sprite Audit Complete");
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Now with the above script, is it easier for the team to comply to naming conventions rather than cherrypick settings for different parts of a project.

we can also implement the same OnPreprocessModel() method for meshes to audit some common errors:

• Make sure Read/Write is disabled
• Disabling rig on non-character models (it fixes the auto added Animator component)
• Copy avatars for characters with shared rigs
• Enable mesh compression

and of course, also applies for Audio.

Top comments (0)