Advertisement
Domukas64

Unity object placement tool

Aug 27th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. using System.IO;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor;
  6.  
  7. public class SceneToolWindow : EditorWindow
  8. {
  9.     bool changeScale;
  10.     FloatMinMax scale = FloatMinMax.One;
  11.  
  12.     bool changeRotation;
  13.     FloatMinMax rotation = new FloatMinMax(-360, 360);
  14.  
  15.     float positionOffset;
  16.  
  17.     GameObject[] prefabs = new GameObject[1];
  18.  
  19.     [MenuItem("Tools/Show Scene Tool")]
  20.     public static void ShowBuildWindow()
  21.     {
  22.         var window = CreateInstance(typeof(SceneToolWindow)) as SceneToolWindow;
  23.         window.Show();
  24.     }
  25.  
  26.     void OnGUI()
  27.     {
  28.         if (prefabs == null || prefabs.Length < 1)
  29.         {
  30.             prefabs = new GameObject[1];
  31.         }
  32.         using (new EditorGUILayout.HorizontalScope())
  33.         {
  34.             using (new EditorGUILayout.HorizontalScope(GUILayout.MaxWidth(20)))
  35.             {
  36.                 changeScale = EditorGUILayout.Toggle(changeScale);
  37.             }
  38.             GUI.enabled = changeScale;
  39.             EditorGUILayout.MinMaxSlider("Scale", ref scale.min, ref scale.max, 0, 10);
  40.             scale = EditorGUILayout.Vector2Field("", new Vector2(scale.min, scale.max));
  41.             GUI.enabled = true;
  42.         }
  43.  
  44.         using (new EditorGUILayout.HorizontalScope())
  45.         {
  46.             using (new EditorGUILayout.HorizontalScope(GUILayout.MaxWidth(20)))
  47.             {
  48.                 changeRotation = EditorGUILayout.Toggle(changeRotation);
  49.             }
  50.             GUI.enabled = changeRotation;
  51.             EditorGUILayout.MinMaxSlider("Rotation", ref rotation.min, ref rotation.max, -360, 360);
  52.             rotation = EditorGUILayout.Vector2Field("", new Vector2(rotation.min, rotation.max));
  53.             GUI.enabled = true;
  54.         }
  55.  
  56.         if (GUILayout.Button("Randomize"))
  57.         {
  58.             foreach (var go in Selection.gameObjects)
  59.             {
  60.                 if (changeScale)
  61.                 {
  62.                     var scaleValue = scale.Lerp(Random.value);
  63.                     go.transform.localScale = Vector3.one * scaleValue;
  64.                 }
  65.                 if (changeRotation)
  66.                 {
  67.                     go.transform.localRotation = Math2D.AngleToRotation(rotation.Lerp(Random.value));
  68.                 }
  69.                 Undo.RegisterFullObjectHierarchyUndo(go, "Randomize Values");
  70.  
  71.             }
  72.  
  73.         }
  74.  
  75.         EditorGUILayout.LabelField("Replace Selected Objects:");
  76.         for (int i = 0; i < prefabs.Length; i++)
  77.         {
  78.             using (new EditorGUILayout.HorizontalScope())
  79.             {
  80.                 prefabs[i] = EditorGUILayout.ObjectField(prefabs[i], typeof(GameObject), false) as GameObject;
  81.  
  82.                 using (new EditorGUILayout.HorizontalScope(GUILayout.MaxWidth(64)))
  83.                 {
  84.                     if (GUILayout.Button("+"))
  85.                     {
  86.                         ArrayUtility.Insert(ref prefabs, i, prefabs[i]);
  87.                     }
  88.                     if (i > 0)
  89.                     {
  90.                         if (GUILayout.Button("-"))
  91.                         {
  92.                             ArrayUtility.RemoveAt(ref prefabs, i);
  93.                             break;
  94.                         }
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.         GUI.enabled = prefabs[0];
  100.         if (GUILayout.Button("Replace"))
  101.         {
  102.             ReplaceSelectedObjects();
  103.         }
  104.         GUI.enabled = true;
  105.  
  106.     }
  107.  
  108.     void ReplaceSelectedObjects()
  109.     {
  110.         var newGos = new List<GameObject>();
  111.         foreach (var go in Selection.gameObjects)
  112.         {
  113.             var prefab = prefabs[Random.Range(0, prefabs.Length)];
  114.             if (prefab != null)
  115.             {
  116.                 var newGo = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
  117.                 newGo.transform.SetParent(go.transform.parent, false);
  118.                 newGo.transform.position = go.transform.localPosition;
  119.                 newGo.transform.rotation = go.transform.localRotation;
  120.                 newGo.transform.localScale = go.transform.localScale;
  121.                 Undo.RegisterCreatedObjectUndo(newGo, "Create Object");
  122.                 newGos.Add(newGo);
  123.             }
  124.             Undo.DestroyObjectImmediate(go);
  125.         }
  126.         Selection.objects = newGos.ToArray();
  127.     }
  128. }
  129.  
  130.  
  131. //Optional struct for easy lerps
  132. [System.Serializable]
  133. public struct FloatMinMax {
  134.  
  135.     public float min;
  136.     public float max;
  137.  
  138.     public FloatMinMax(float min, float max)
  139.     {
  140.         this.min = min;
  141.         this.max = max;
  142.     }
  143.  
  144.     public float Lerp(float t)
  145.     {
  146.         return Mathf.Lerp(min, max, t);
  147.     }
  148.  
  149.     public float InverseLerp(float t)
  150.     {
  151.         return Mathf.InverseLerp(min, max, t);
  152.     }
  153.  
  154.     public bool Includes(float value)
  155.     {
  156.         return value > min && value < max;
  157.     }
  158.  
  159.     public static FloatMinMax One
  160.     {
  161.         get
  162.         {
  163.             FloatMinMax value;
  164.             value.min = value.max = 1f;
  165.             return value;
  166.         }
  167.     }
  168.  
  169.     public static implicit operator FloatMinMax (Vector2 v)
  170.     {
  171.         return new FloatMinMax(v.x, v.y);
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement