Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- public class AnimImporter : AssetPostprocessor
- {
- // Always require a prefix to use this Custom Importer script
- const bool REQUIRE_PREFIX_MATCH_IN_ASSET_NAME = true;
- // The prefix in the Asset name to trigger this script
- const String CUSTOM_IMPORT_PREFIX = "AG_";
- // Overrides existing clips already imported in Unity
- const bool ALWAYS_OVERWRITE_EXISTING_CLIPS = false;
- // If set to true, clip name ending with any of the following suffixes will be looped
- const bool AUTODETECT_LOOP = true;
- static String[] AUTOLOOP_SUFFIXES = { "_Held", "_Loop" };
- void OnPreprocessAnimation()
- {
- ModelImporter modelImporter = assetImporter as ModelImporter;
- String assetName = System.IO.Path.GetFileNameWithoutExtension(modelImporter.assetPath);
- //Debug.Log($"Importing animations from \"{assetName}\"");
- if (REQUIRE_PREFIX_MATCH_IN_ASSET_NAME == true && assetName.StartsWith(CUSTOM_IMPORT_PREFIX) == false)
- {
- //Debug.Log($"Does not have the import prefix, skipping {assetName}");
- return;
- }
- ModelImporterClipAnimation[] importedClips = modelImporter.clipAnimations;
- ModelImporterClipAnimation[] defaultClips = modelImporter.defaultClipAnimations;
- List<ModelImporterClipAnimation> importedAnimations = new List<ModelImporterClipAnimation>();
- int modifiedCount = 0;
- for (int i = 0; i < defaultClips.Length; i++)
- {
- ModelImporterClipAnimation anim = defaultClips[i];
- if (ALWAYS_OVERWRITE_EXISTING_CLIPS == false && importedClips != null && importedClips.Length > 0)
- {
- bool skipClip = false;
- for (int j = 0; j < importedClips.Length; j++)
- {
- if (importedClips[j].name == anim.name)
- {
- // Use the already existing imported clip without modification
- importedAnimations.Add(importedClips[j]);
- skipClip = true;
- break;
- }
- }
- if (skipClip == true)
- {
- continue;
- }
- }
- // This is a new clip, set default parameters
- modifiedCount++;
- if (AUTODETECT_LOOP == true)
- {
- foreach (String suffix in AUTOLOOP_SUFFIXES)
- {
- if (anim.name.EndsWith(suffix) == true)
- {
- anim.loopTime = true;
- }
- }
- }
- anim.heightFromFeet = false;
- anim.keepOriginalOrientation = true;
- anim.keepOriginalPositionXZ = true;
- anim.keepOriginalPositionY = true;
- anim.lockRootHeightY = true;
- anim.lockRootPositionXZ = true;
- anim.lockRootRotation = true;
- importedAnimations.Add(anim);
- }
- // Assign all the modified default animations as the imported clips
- modelImporter.clipAnimations = importedAnimations.ToArray();
- //Debug.Log($"Imported {modelImporter.clipAnimations.Length} clip(s) | Modified: {modifiedCount}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement