Advertisement
Muk99

TreeInstantiater

Aug 11th, 2015
275
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 UnityEditor;
  3. using UnityEngine;
  4. using EditorCallback = UnityEditor.EditorApplication.CallbackFunction;
  5.  
  6. [CustomEditor(typeof(Test))]
  7. class Instantiater : Editor {
  8.  
  9.     private Transform parent {
  10.         get {
  11.             var find = GameObject.Find("Trees");
  12.             if(find)
  13.                 return find.transform;
  14.             return new GameObject("Trees").transform;
  15.         }
  16.     }    
  17.    
  18.     private List<GameObject> prefabs = new List<GameObject>();
  19.     private Vector2 scroll;
  20.  
  21.     public override void OnInspectorGUI() {
  22.         scroll = EditorGUILayout.BeginScrollView(scroll, false, false);
  23.  
  24.         //parent = (Transform)EditorGUILayout.ObjectField("Parent transform", parent, typeof(Transform), false);
  25.  
  26.         ListSources();
  27.         RemoveInvalidFiles();
  28.  
  29.         EditorGUILayout.EndScrollView();
  30.     }
  31.  
  32.     private void OnSceneGUI() {
  33.         if(prefabs.Count > 0) {
  34.             HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
  35.             var evt = Event.current;
  36.             var cam = Camera.current;
  37.             var mousePos = evt.mousePosition;
  38.             mousePos.y = Mathf.Abs(mousePos.y - cam.pixelHeight);
  39.             var ray = cam.ScreenPointToRay(mousePos);
  40.             var hit = new RaycastHit();
  41.             if(Physics.Raycast(ray, out hit)) {
  42.                 if(evt.type == EventType.MouseDown) {
  43.                     var go = PrefabUtility.InstantiatePrefab(randomPrefab) as GameObject;
  44.                     Undo.RegisterCreatedObjectUndo(go, go.name + " Creation");
  45.                     go.transform.up = hit.normal;
  46.                     go.transform.rotation = randomRot;
  47.                     go.transform.position = hit.point;
  48.                     go.transform.parent = parent;
  49.                 }
  50.             }
  51.             HandleUtility.Repaint();
  52.         }
  53.     }
  54.  
  55.     private Quaternion randomRot {
  56.         get {
  57.             return Quaternion.Euler(Vector3.up * Random.value * 360f);
  58.         }
  59.     }
  60.  
  61.     private GameObject randomPrefab {
  62.         get {
  63.             return prefabs[Random.Range(0, prefabs.Count)];
  64.         }
  65.     }
  66.  
  67.     private GameObject PrefabField(GameObject prefab) {
  68.         var rect = EditorGUILayout.GetControlRect();
  69.         var buttonRect = rect;
  70.         buttonRect.xMin = rect.xMax - 15f;
  71.  
  72.         if(GUI.Button(buttonRect, "X", "ObjectFieldThumbOverlay"))
  73.             return null;
  74.  
  75.         return PrefabField(prefab, rect);
  76.     }
  77.  
  78.     private GameObject PrefabField(GameObject prefab, Rect rect) {
  79.         rect.xMax -= 15f;
  80.         return (GameObject)EditorGUI.ObjectField(rect, prefab, typeof(GameObject), false);
  81.     }
  82.  
  83.     private GameObject[] DropAreaGUI() {
  84.         var evt = Event.current;
  85.         var dropArea = EditorGUILayout.GetControlRect();
  86.  
  87.         switch(evt.type) {
  88.             case EventType.DragUpdated:
  89.             case EventType.DragPerform:
  90.             if(!dropArea.Contains(evt.mousePosition))
  91.                 return new GameObject[0];
  92.  
  93.             DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  94.  
  95.             if(evt.type == EventType.DragPerform) {
  96.                 DragAndDrop.AcceptDrag();
  97.  
  98.                 var references = DragAndDrop.objectReferences;
  99.                 var list = new GameObject[references.Length];
  100.  
  101.                 for(int i = 0; i < references.Length; i++)
  102.                     list[i] = (GameObject)references[i];
  103.  
  104.                 return list;
  105.             }
  106.             break;
  107.         }
  108.  
  109.         var selectedFile = PrefabField(null, dropArea);
  110.         if(selectedFile != null)
  111.             return new GameObject[] { selectedFile };
  112.         return new GameObject[0];
  113.     }
  114.  
  115.     private void RemoveInvalidFiles() {
  116.         for(int i = 0; i < prefabs.Count; i++)
  117.             if(prefabs[i] == null)
  118.                 prefabs.RemoveAt(i);
  119.     }
  120.  
  121.     private void ListSources() {
  122.         EditorGUILayout.Space();
  123.         EditorGUILayout.LabelField("Tree sources");
  124.  
  125.         for(int i = 0; i < prefabs.Count; i++)
  126.             prefabs[i] = PrefabField(prefabs[i]);
  127.  
  128.         foreach(var file in DropAreaGUI())
  129.             if(!prefabs.Contains(file))
  130.                 prefabs.Add(file);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement