Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 13th, 2012  |  syntax: None  |  size: 6.74 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class MultiPanel : EditorWindow
  6. {
  7.         string addComponentString = "";
  8.         string theLayer = "";
  9.         string theRenameName = "";
  10.         string theRenameNameSuf = "";
  11.         string theYPos = "5.0";
  12.  
  13.         //Adding a menu entry
  14.         [MenuItem("EditorTools/MultiPanel")]
  15.        
  16.         static void Init ()
  17.         {
  18.                 // Get existing open window or if none, make a new one:
  19.                 MultiPanel window = (MultiPanel)EditorWindow.GetWindow (typeof(MultiPanel));
  20.                 window.Show ();
  21.         }
  22.        
  23.         /*
  24.          * GUI Generation
  25.          */            
  26.         void OnGUI ()
  27.         {
  28.                 GUILayout.Label ("MultiPanel by GTJuggler", EditorStyles.boldLabel);
  29.                 GUILayout.Label (" Operations for multiple selected objects", EditorStyles.miniLabel);
  30.                 GUILayout.Space (15);
  31.                
  32.                 EditorGUILayout.BeginHorizontal ();
  33.                
  34.                 /*
  35.                 * LEFT COLUMN
  36.                 */     
  37.                 EditorGUILayout.BeginVertical ();
  38.                 GUILayout.Space (5);
  39.                
  40.                 GUILayout.Label ("Zero out Transform");
  41.                
  42.                 GUILayout.Space (5);
  43.                
  44.                 GUILayout.Label ("Inherit Parent Transform");
  45.                
  46.                 GUILayout.Space (15);
  47.                
  48.                 GUILayout.Label ("Toggle Renderers");
  49.                
  50.                 GUILayout.Space (5);
  51.                
  52.                 GUILayout.Label ("Set Active Recursively");
  53.                
  54.                 GUILayout.Space (15);
  55.                
  56.                 GUILayout.Label ("Add Component");
  57.                
  58.                 addComponentString = EditorGUILayout.TextField ("Rigidbody");
  59.                
  60.                 GUILayout.Space (10);
  61.                
  62.                 GUILayout.Label ("Remove Component");
  63.                
  64.                 addComponentString = EditorGUILayout.TextField ("Rigidbody");
  65.                
  66.                 GUILayout.Space (25);
  67.                
  68.                 GUILayout.Label ("Layer and Tag Assignment");
  69.                 theLayer = EditorGUILayout.TextField (theLayer);
  70.                
  71.                 GUILayout.Space (5);
  72.                 GUILayout.Label ("Rename exactly");
  73.                 theRenameName = EditorGUILayout.TextField (theRenameName);
  74.                
  75.                 GUILayout.Space (5);
  76.                 GUILayout.Label ("Rename w/ numerical suffix");
  77.                 theRenameNameSuf = EditorGUILayout.TextField (theRenameNameSuf);
  78.                
  79.                 EditorGUILayout.EndVertical ();
  80.                
  81.                 /*
  82.                 * RIGHT COLUMN
  83.                 */
  84.                 EditorGUILayout.BeginVertical ();
  85.                 GUILayout.Space (5);
  86.                 if (GUILayout.Button ("Go")) {
  87.                         ZeroOutTransform ();
  88.                 }
  89.                 GUILayout.Space (2);
  90.                
  91.                 if (GUILayout.Button ("Go")) {
  92.                         InheritParentTransform ();
  93.                 }
  94.                 GUILayout.Space (20);
  95.                 if (GUILayout.Button ("Toggle")) {
  96.                         ToggleRenderers ();
  97.                 }
  98.                 GUILayout.Space (2);
  99.                 if (GUILayout.Button ("Toggle")) {
  100.                         ToggleAllSelected ();
  101.                 }
  102.                 GUILayout.Space (35);
  103.                 if (GUILayout.Button ("Add")) {
  104.                         AddComponent ();
  105.                 }
  106.                 GUILayout.Space (29);
  107.                 if (GUILayout.Button ("Remove")) {
  108.                         RemoveComponent ();
  109.                 }
  110.                 GUILayout.Space (44);
  111.                
  112.                
  113.                 EditorGUILayout.BeginHorizontal ();
  114.                 if (GUILayout.Button ("Layer")) {
  115.                         ChangeLayer ();
  116.                 }
  117.                 if (GUILayout.Button ("Tag")) {
  118.                         ChangeTag ();
  119.                 }
  120.                 EditorGUILayout.EndHorizontal ();
  121.                
  122.                
  123.                 GUILayout.Space (24);
  124.                 if (GUILayout.Button ("Rename")) {
  125.                         Rename (false);
  126.                 }
  127.                
  128.                 GUILayout.Space (24);
  129.                 if (GUILayout.Button ("Rename")) {
  130.                         Rename (true);
  131.                 }
  132.                
  133.                 EditorGUILayout.EndVertical ();
  134.                 EditorGUILayout.EndHorizontal ();
  135.         }
  136.  
  137.         /*
  138.      * Functions
  139.      */
  140.         void MoveToYPos ()
  141.         {
  142.                 float yPos = float.Parse (theYPos);
  143.                 Transform[] transforms = Selection.GetTransforms (SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
  144.                
  145.                 foreach (Transform tr in transforms) {
  146.                         tr.position = new Vector3 (tr.position.x, yPos, tr.position.z);
  147.                 }
  148.         }
  149.  
  150.         void ZeroOutTransform ()
  151.         {
  152.                 Transform[] transforms = Selection.GetTransforms (SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
  153.                
  154.                 foreach (Transform tr in transforms) {
  155.                         tr.position = Vector3.zero;
  156.                         tr.localScale = Vector3.zero;
  157.                         tr.rotation = Quaternion.identity;
  158.                 }
  159.         }
  160.  
  161.         void InheritParentTransform ()
  162.         {
  163.                 Transform[] transforms = Selection.GetTransforms (SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
  164.                
  165.                 foreach (Transform tr in transforms) {
  166.                         tr.transform.position = tr.parent.transform.position;
  167.                         tr.transform.rotation = tr.parent.transform.rotation;
  168.                         tr.transform.localScale = tr.parent.transform.localScale;
  169.                 }
  170.         }
  171.  
  172.         void ToggleRenderers ()
  173.         {
  174.                 Transform[] transforms = Selection.GetTransforms (SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
  175.                
  176.                 foreach (Transform transform in transforms) {
  177.                         if (transform.renderer)
  178.                                 transform.renderer.enabled = !transform.renderer.enabled;
  179.                 }
  180.         }
  181.  
  182.         void AddComponent ()
  183.         {
  184.                 int total = 0;
  185.                 foreach (Transform currentTransform in Selection.transforms) {
  186.                         //add component
  187.                         Component existingComponent = currentTransform.GetComponent (addComponentString);
  188.                         if (!existingComponent) {
  189.                                 currentTransform.gameObject.AddComponent (addComponentString);
  190.                                 total++;
  191.                         }
  192.                 }
  193.                 if (total == 0)
  194.                         Debug.Log ("No components added.");
  195.                 else
  196.                         Debug.Log (total + " components of type \"" + addComponentString + "\" created.");
  197.         }
  198.  
  199.         void RemoveComponent ()
  200.         {
  201.                 int total = 0;
  202.                 foreach (Transform currentTransform in Selection.transforms) {
  203.                         //remove component
  204.                         Component existingComponent = currentTransform.GetComponent (addComponentString);
  205.                         if (existingComponent) {
  206.                                 DestroyImmediate (existingComponent);
  207.                                 total++;
  208.                         }
  209.                 }
  210.                 if (total == 0)
  211.                         Debug.Log ("No components destroyed.");
  212.                 else
  213.                         Debug.Log (total + " components of type \"" + addComponentString + "\" destroyed.");
  214.         }
  215.  
  216.         void ToggleAllSelected ()
  217.         {
  218.                 foreach (Transform t in Selection.transforms) {
  219.                         t.gameObject.SetActiveRecursively (!t.gameObject.active);
  220.                 }
  221.         }
  222.  
  223.         void ChangeLayer ()
  224.         {
  225.                 Object[] selectedObjects;
  226.                
  227.                 selectedObjects = Selection.GetFiltered (typeof(GameObject), SelectionMode.TopLevel);
  228.                
  229.                 foreach (GameObject go in selectedObjects) {
  230.                         LayerMask aLayer = LayerMask.NameToLayer (theLayer);
  231.                         if (aLayer.value != -1)
  232.                                 go.layer = aLayer;
  233.                 }
  234.         }
  235.  
  236.         void ChangeTag ()
  237.         {
  238.                 Object[] selectedObjects;
  239.                
  240.                 selectedObjects = Selection.GetFiltered (typeof(GameObject), SelectionMode.TopLevel);
  241.                
  242.                 foreach (GameObject go in selectedObjects) {
  243.                         go.tag = theLayer;
  244.                 }
  245.         }
  246.        
  247.         void Rename (bool numberedSuffix)
  248.         {
  249.                 Transform[] transforms = Selection.GetTransforms (SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
  250.                
  251.                 ArrayList transformArray = new ArrayList ();
  252.                
  253.                 foreach (Transform tr in transforms) {
  254.                         transformArray.Add (tr);
  255.                 }
  256.                
  257.                 for (int i = 0; i < transformArray.Count; i++) {
  258.                         if(numberedSuffix)
  259.                                 ((Transform)transformArray[i]).name = theRenameNameSuf + i.ToString ();
  260.                         else
  261.                                 ((Transform)transformArray[i]).name = theRenameName;
  262.                 }
  263.                
  264.                 //This hack below will ensure proper name sorting :/
  265.                 Transform origParent = ((Transform)transformArray[0]).parent;
  266.                
  267.                 for (int i = 0; i < transformArray.Count; i++) {
  268.                         ((Transform)transformArray[i]).parent = (((Transform)transformArray[i]).parent).parent;
  269.                         ((Transform)transformArray[i]).parent = origParent;
  270.                 }
  271.         }
  272. }