duck

Unity 3D Multiple Transforms Copy / Paster

Jun 23rd, 2011
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. // Improved TransformCopier
  2. // by @robotduck 2011
  3.  
  4. // Copy single or multiple local transforms from and to sets of gameobjects.
  5.  
  6. // based on original single-object-copy script from UnifyCommunity Wiki by Steve Allison-Bunnell (with thanks to aarku)
  7.  
  8.  
  9. using UnityEngine;
  10. using UnityEditor;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13.  
  14. public class TransformCopier : ScriptableObject
  15. {
  16.     private static Vector3 position;
  17.     private static Quaternion rotation;
  18.     private static Vector3 scale;
  19.    
  20.     private static List<StoredLocalTransform> storedTransforms;
  21.  
  22.     [MenuItem("Custom/Transform Copier/Copy Transform")]
  23.     static void DoRecord ()
  24.     {
  25.         storedTransforms = new List<StoredLocalTransform>();
  26.        
  27.         foreach (Transform t in Selection.transforms) {
  28.             storedTransforms.Add(new StoredLocalTransform(t));
  29.         }
  30.        
  31.         if (storedTransforms.Count == 1)
  32.         {
  33.             EditorUtility.DisplayDialog ("Transform Copy", "Local position, rotation, & scale of " + Selection.activeTransform.name + " copied.", "OK", "");
  34.         } else {
  35.             EditorUtility.DisplayDialog ("Transform Copy", "Local position, rotation, & scale of " + storedTransforms.Count + " objects copied.", "OK", "");
  36.         }
  37.     }
  38.  
  39.     [MenuItem("Custom/Transform Copier/Paste Transform")]
  40.     static void DoApply ()
  41.     {
  42.         string resultString = "";
  43.         if (storedTransforms.Count == 1)
  44.         {
  45.             StoredLocalTransform stored = storedTransforms[0];
  46.            
  47.             // paste single transform to all selected: 
  48.             foreach (Transform t in Selection.transforms) {
  49.                 t.localPosition = stored.localPosition;
  50.                 t.localRotation = stored.localRotation;
  51.                 t.localScale = stored.localScale;
  52.             }
  53.            
  54.             int num = Selection.transforms.Length;
  55.             resultString = "Local position, rotation, and scale of " + stored.name + " pasted to ";
  56.             resultString += (num == 1 ? Selection.activeTransform.name : num+" selected objects")+".";
  57.  
  58.         } else {
  59.             // stored & selected counts must match:
  60.             if (storedTransforms.Count == Selection.transforms.Length)
  61.             {
  62.                 int n=0;
  63.                 foreach (Transform t in Selection.transforms)
  64.                 {
  65.                     t.localPosition = storedTransforms[n].localPosition;   
  66.                     t.localRotation = storedTransforms[n].localRotation;   
  67.                     t.localScale = storedTransforms[n].localScale; 
  68.                     ++n;
  69.                 }
  70.                
  71.                 resultString = "Local position, rotation, & scale of " + storedTransforms.Count + " objects pasted.";
  72.                
  73.             } else {
  74.                 resultString = "Error: When copying from multiple objects, the number of objects selected must match the number of objects copied from.";
  75.             }
  76.            
  77.         }
  78.        
  79.         EditorUtility.DisplayDialog ("Transform Paste", resultString, "OK", "");
  80.        
  81.     }
  82. }
  83.  
  84. struct StoredLocalTransform
  85. {
  86.  
  87.     public Vector3 localPosition;
  88.     public Quaternion localRotation;
  89.     public Vector3 localScale;
  90.     public string name;
  91.  
  92.     public StoredLocalTransform (Transform t)
  93.     {
  94.         this.localPosition = t.localPosition;
  95.         this.localRotation = t.localRotation;
  96.         this.localScale = t.localScale;
  97.         this.name = t.name;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment