Advertisement
SvOzMaS

AnimationClipDuplicationTool.cs

Mar 31st, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. /// <summary>
  6. /// @author: Sergio Vozmediano Ávilas
  7. /// 31/03/2019
  8. ///
  9. ///
  10. /// Simple tool that takes all the fbx files from a folder (loadPath), and looks for animation clips inside each fbx.
  11. /// If there are any animations, it duplicates them in the savePath folder with the same name as the fbx files.
  12. /// </summary>
  13.  
  14. public class AnimationClipDuplicationTool : EditorWindow {
  15.  
  16.     public string loadPath = "Load Path";
  17.     public string savePath = "Save Path";
  18.     public List<AnimationClipOverrider> clipsToDuplicate = new List<AnimationClipOverrider>();
  19.  
  20.     Vector2 scrollPos;
  21.  
  22.     [MenuItem("Window/ACDT")]
  23.     public static void ShowWindow() {
  24.         GetWindow(typeof(AnimationClipDuplicationTool));
  25.     }
  26.  
  27.  
  28.     void OnGUI() {
  29.  
  30.         LoadPathGUI();      
  31.  
  32.         EditorGUILayout.Space();
  33.  
  34.         SavePathGUI();
  35.  
  36.         EditorGUILayout.Space();
  37.  
  38.         if (GUILayout.Button(" Duplicate ")) {
  39.             duplicateClips();
  40.         }
  41.  
  42.         EditorGUILayout.Space();
  43.  
  44.         ClipPathListGUI();
  45.     }
  46.  
  47.  
  48.     private void poolClipsToDuplicateList(string[] files) {
  49.  
  50.         clipsToDuplicate = new List<AnimationClipOverrider>();
  51.  
  52.         for (int i = 0; i < files.Length; i++) {
  53.             if (files[i].EndsWith(".fbx")) {
  54.                 clipsToDuplicate.Add(new AnimationClipOverrider {file = "Assets" + files[i].Substring(Application.dataPath.Length), duplicate = true });
  55.             }
  56.         }
  57.     }
  58.  
  59.     private void LoadPathGUI() {
  60.         // Loading Path
  61.         EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  62.         EditorGUILayout.LabelField("Load Path");
  63.         EditorGUI.BeginDisabledGroup(true);
  64.         EditorGUILayout.TextField(loadPath);
  65.         EditorGUI.EndDisabledGroup();
  66.  
  67.         if (GUILayout.Button(" Open ")) {
  68.             string sP = EditorUtility.OpenFolderPanel("Duplicate Animation Clips From...", "", "");
  69.             loadPath = sP != null && !sP.Equals("") ? sP : loadPath;
  70.             string[] files = Directory.GetFiles(loadPath);
  71.             poolClipsToDuplicateList(files);
  72.         }
  73.  
  74.         EditorGUILayout.EndVertical();
  75.     }
  76.  
  77.     private void SavePathGUI() {
  78.         // Saving Path
  79.         EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  80.         EditorGUILayout.LabelField("Save Path");
  81.         EditorGUI.BeginDisabledGroup(true);
  82.         EditorGUILayout.TextField(savePath);
  83.         EditorGUI.EndDisabledGroup();
  84.  
  85.         if (GUILayout.Button(" Open ")) {
  86.             string sP = EditorUtility.OpenFolderPanel("Save Duplicated Animation Clips to...", "", "");
  87.             savePath = sP != null && !sP.Equals("") ? sP : savePath;
  88.         }
  89.  
  90.         EditorGUILayout.EndVertical();
  91.     }
  92.  
  93.     private void ClipPathListGUI() {
  94.         EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  95.         scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
  96.  
  97.         // Print ugly inspector list
  98.         ScriptableObject scriptableObj = this;
  99.         SerializedObject serialObj = new SerializedObject(scriptableObj);
  100.         SerializedProperty serialProp = serialObj.FindProperty("clipsToDuplicate");
  101.  
  102.         EditorGUILayout.PropertyField(serialProp, true);
  103.  
  104.         EditorGUILayout.EndScrollView();
  105.         EditorGUILayout.EndVertical();
  106.         serialObj.ApplyModifiedProperties();
  107.     }
  108.  
  109.     private void duplicateClips() {
  110.  
  111.         for (int i = 0; i < clipsToDuplicate.Count; i++) {
  112.             if (clipsToDuplicate[i].duplicate) {
  113.                 // Load the original animation clip from the prefab
  114.                 AnimationClip orgClip = Instantiate((AnimationClip)AssetDatabase.LoadAssetAtPath(clipsToDuplicate[i].file, typeof(AnimationClip)));
  115.  
  116.                 if (orgClip) {
  117.                    AssetDatabase.CreateAsset(orgClip, "Assets" + savePath.Substring(Application.dataPath.Length) + "/" + Path.GetFileNameWithoutExtension(clipsToDuplicate[i].file) + ".anim");
  118.                 }  
  119.             }
  120.         }
  121.  
  122.         AssetDatabase.Refresh();
  123.     }
  124.  
  125.     [System.Serializable]
  126.     public struct AnimationClipOverrider {
  127.         public string file;
  128.         public bool duplicate;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement