Advertisement
Guest User

AnimationPathReplacer

a guest
Apr 22nd, 2016
625
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6. using System.IO;
  7.  
  8. public class AnimationPathReplacer : EditorWindow
  9. {
  10.     private static int columnWidth = 300;
  11.    
  12.     private List<AnimationClip> animationClips;
  13.     private ArrayList pathsKeys;
  14.     private Hashtable paths;
  15.    
  16.     private Vector2 scrollPos = Vector2.zero;
  17.    
  18.     [MenuItem("Window/Animation Path Replacer")]
  19.     static void ShowWindow() {
  20.         EditorWindow.GetWindow<AnimationPathReplacer>();
  21.     }
  22.  
  23.     public AnimationPathReplacer(){
  24.         animationClips = new List<AnimationClip>();
  25.     }
  26.    
  27.     void OnSelectionChange() {
  28.         refreshSelection();
  29.     }
  30.  
  31.     private void refreshSelection()
  32.     {
  33.         if (Selection.objects.Length > 1 )
  34.         {
  35.             animationClips.Clear();
  36.             foreach ( Object o in Selection.objects )
  37.             {
  38.                 if ( o is AnimationClip ) animationClips.Add((AnimationClip)o);
  39.             }
  40.             FillModel();
  41.         }
  42.         else if (Selection.activeObject is AnimationClip) {
  43.             animationClips.Clear();
  44.             animationClips.Add((AnimationClip)Selection.activeObject);
  45.             FillModel();
  46.         } else {
  47.             animationClips.Clear();
  48.         }
  49.        
  50.         this.Repaint();
  51.     }
  52.    
  53.     private string sOriginalRoot = "OldPath/Old";
  54.     private string sNewRoot = "NewPath/New";
  55.    
  56.     void OnGUI() {
  57.         if (Event.current.type == EventType.ValidateCommand) {
  58.             switch (Event.current.commandName) {
  59.             case "UndoRedoPerformed":
  60.                 FillModel();
  61.                 break;
  62.             }
  63.         }
  64.        
  65.         if (animationClips.Count > 0 ) {
  66.             scrollPos = GUILayout.BeginScrollView(scrollPos, GUIStyle.none);
  67.  
  68.             EditorGUILayout.BeginHorizontal();
  69.             GUILayout.Label("Animation Clip:", GUILayout.Width(columnWidth));
  70.            
  71.             if ( animationClips.Count == 1 )
  72.             {
  73.                 animationClips[0] = ((AnimationClip)EditorGUILayout.ObjectField(
  74.                     animationClips[0],
  75.                     typeof(AnimationClip),
  76.                     true,
  77.                     GUILayout.Width(columnWidth))
  78.                                      );
  79.             }          
  80.             else
  81.             {
  82.                 GUILayout.Label("Multiple Anim Clips: " + animationClips.Count, GUILayout.Width(columnWidth));
  83.             }
  84.             EditorGUILayout.EndHorizontal();
  85.            
  86.             GUILayout.Space(20);
  87.            
  88.             EditorGUILayout.BeginHorizontal();
  89.            
  90.             sOriginalRoot = EditorGUILayout.TextField(sOriginalRoot, GUILayout.Width(columnWidth));
  91.             sNewRoot = EditorGUILayout.TextField(sNewRoot, GUILayout.Width(columnWidth));
  92.  
  93.             EditorGUILayout.EndHorizontal();
  94.             EditorGUILayout.BeginHorizontal();
  95.  
  96.             if (GUILayout.Button("Replace Path")) {
  97.                 ReplacePath(sOriginalRoot, sNewRoot);
  98.             }
  99.             EditorGUILayout.EndHorizontal();
  100.  
  101.  
  102.            
  103.             EditorGUILayout.BeginHorizontal();
  104.             GUILayout.Label("Paths in Clip:", GUILayout.Width(columnWidth));
  105.             GUILayout.Label("Animated properties:", GUILayout.Width(columnWidth*0.5f));
  106.             EditorGUILayout.EndHorizontal();
  107.            
  108.             if (paths != null)
  109.             {
  110.                 foreach (string path in pathsKeys)
  111.                 {
  112.                     GUICreatePathItem(path);
  113.                 }
  114.             }
  115.            
  116.             GUILayout.Space(40);
  117.             GUILayout.EndScrollView();
  118.         } else {
  119.             GUILayout.Label("Please select an Animation Clip");
  120.         }
  121.     }
  122.    
  123.    
  124.     void GUICreatePathItem(string path) {
  125.  
  126.         ArrayList properties = (ArrayList)paths[path];
  127.  
  128.         EditorGUILayout.BeginHorizontal();
  129.        
  130.         EditorGUILayout.LabelField(path, GUILayout.Width(columnWidth));
  131.                
  132.         EditorGUILayout.LabelField(
  133.             properties != null ? properties.Count.ToString() : "0",
  134.             GUILayout.Width(60)
  135.             );
  136.  
  137.         EditorGUILayout.EndHorizontal();
  138.     }
  139.    
  140.     void OnInspectorUpdate() {
  141.         this.Repaint();
  142.     }
  143.    
  144.     void FillModel() {
  145.         paths = new Hashtable();
  146.         pathsKeys = new ArrayList();
  147.        
  148.         foreach ( AnimationClip animationClip in animationClips )
  149.         {
  150.             FillModelWithCurves(AnimationUtility.GetCurveBindings(animationClip));
  151.             FillModelWithCurves(AnimationUtility.GetObjectReferenceCurveBindings(animationClip));
  152.         }
  153.     }
  154.    
  155.     private void FillModelWithCurves(EditorCurveBinding[] curves) {
  156.         foreach (EditorCurveBinding curveData in curves) {
  157.             string key = curveData.path;
  158.            
  159.             if (paths.ContainsKey(key)) {
  160.                 ((ArrayList)paths[key]).Add(curveData);
  161.             } else {
  162.                 ArrayList newProperties = new ArrayList();
  163.                 newProperties.Add(curveData);
  164.                 paths.Add(key, newProperties);
  165.                 pathsKeys.Add(key);
  166.             }
  167.         }
  168.     }
  169.    
  170.     void ReplacePath(string oldPath, string newPath)
  171.     {
  172.         AssetDatabase.StartAssetEditing();
  173.        
  174.         for (int iCurrentClip = 0; iCurrentClip < animationClips.Count; iCurrentClip++ )
  175.         {
  176.             AnimationClip animationClip =  animationClips[iCurrentClip];
  177.             Undo.RecordObject(animationClip, "Animation Path Replacement");
  178.  
  179.             string assetPath = AssetDatabase.GetAssetPath(animationClip);
  180.  
  181.             string fileContents = File.ReadAllText(assetPath);
  182.             string newFileContents = "";
  183.  
  184.             int replacementCount = 0;
  185.             string[] allLines = fileContents.Split("\r\n".ToCharArray());
  186.             for(int i=0; i<allLines.Length; ++i)
  187.             {
  188.                 if (allLines[i].Contains("path: "))
  189.                 {
  190.                     string replacedLine = allLines[i].Replace(oldPath, newPath);
  191.                     if (allLines[i] != replacedLine)
  192.                     {
  193.                         allLines[i] = replacedLine;
  194.                         replacementCount++;
  195.                     }
  196.                 }
  197.                 newFileContents += allLines[i] + "\r\n";
  198.             }
  199.  
  200.             if (replacementCount > 0)
  201.             {
  202.                 File.WriteAllText(assetPath, newFileContents);
  203.             }
  204.  
  205.             Debug.Log("Replaced "+replacementCount+" paths in "+assetPath);
  206.         }
  207.         AssetDatabase.StopAssetEditing();
  208.         EditorUtility.ClearProgressBar();
  209.  
  210.         AssetDatabase.Refresh();
  211.        
  212.         refreshSelection();
  213.     }
  214. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement