SHARE
TWEET

Unity Clips Auto Baking Importer

a guest Oct 17th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5.  
  6. public class AnimImporter : AssetPostprocessor
  7. {
  8.     // Always require a prefix to use this Custom Importer script
  9.     const bool REQUIRE_PREFIX_MATCH_IN_ASSET_NAME = true;
  10.     // The prefix in the Asset name to trigger this script
  11.     const String CUSTOM_IMPORT_PREFIX = "AG_";
  12.     // Overrides existing clips already imported in Unity
  13.     const bool ALWAYS_OVERWRITE_EXISTING_CLIPS = false;
  14.     // If set to true, clip name ending with any of the following suffixes will be looped
  15.     const bool AUTODETECT_LOOP = true;
  16.     static String[] AUTOLOOP_SUFFIXES = { "_Held", "_Loop" };
  17.  
  18.     void OnPreprocessAnimation()
  19.     {
  20.         ModelImporter modelImporter = assetImporter as ModelImporter;
  21.         String assetName = System.IO.Path.GetFileNameWithoutExtension(modelImporter.assetPath);
  22.         //Debug.Log($"Importing animations from \"{assetName}\"");
  23.         if (REQUIRE_PREFIX_MATCH_IN_ASSET_NAME == true && assetName.StartsWith(CUSTOM_IMPORT_PREFIX) == false)
  24.         {
  25.             //Debug.Log($"Does not have the import prefix, skipping {assetName}");
  26.             return;
  27.         }
  28.  
  29.         ModelImporterClipAnimation[] importedClips = modelImporter.clipAnimations;
  30.         ModelImporterClipAnimation[] defaultClips = modelImporter.defaultClipAnimations;
  31.         List<ModelImporterClipAnimation> importedAnimations = new List<ModelImporterClipAnimation>();
  32.  
  33.         int modifiedCount = 0;
  34.         for (int i = 0; i < defaultClips.Length; i++)
  35.         {
  36.             ModelImporterClipAnimation anim = defaultClips[i];
  37.             if (ALWAYS_OVERWRITE_EXISTING_CLIPS == false && importedClips != null && importedClips.Length > 0)
  38.             {
  39.                 bool skipClip = false;
  40.                 for (int j = 0; j < importedClips.Length; j++)
  41.                 {
  42.                     if (importedClips[j].name == anim.name)
  43.                     {                      
  44.                         // Use the already existing imported clip without modification
  45.                         importedAnimations.Add(importedClips[j]);
  46.                         skipClip = true;
  47.                         break;
  48.                     }
  49.                 }
  50.                 if (skipClip == true)
  51.                 {
  52.                     continue;
  53.                 }
  54.             }
  55.             // This is a new clip, set default parameters
  56.             modifiedCount++;
  57.             if (AUTODETECT_LOOP == true)
  58.             {
  59.                 foreach (String suffix in AUTOLOOP_SUFFIXES)
  60.                 {
  61.                     if (anim.name.EndsWith(suffix) == true)
  62.                     {
  63.                         anim.loopTime = true;
  64.                     }
  65.                 }
  66.             }
  67.             anim.heightFromFeet = false;
  68.             anim.keepOriginalOrientation = true;
  69.             anim.keepOriginalPositionXZ = true;
  70.             anim.keepOriginalPositionY = true;
  71.             anim.lockRootHeightY = true;
  72.             anim.lockRootPositionXZ = true;
  73.             anim.lockRootRotation = true;
  74.  
  75.             importedAnimations.Add(anim);          
  76.         }
  77.         // Assign all the modified default animations as the imported clips
  78.         modelImporter.clipAnimations = importedAnimations.ToArray();
  79.         //Debug.Log($"Imported {modelImporter.clipAnimations.Length} clip(s) | Modified: {modifiedCount}");
  80.     }
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top