Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- using EditorCallback = UnityEditor.EditorApplication.CallbackFunction;
- [CustomEditor(typeof(Test))]
- class Instantiater : Editor {
- private Transform parent {
- get {
- var find = GameObject.Find("Trees");
- if(find)
- return find.transform;
- return new GameObject("Trees").transform;
- }
- }
- private List<GameObject> prefabs = new List<GameObject>();
- private Vector2 scroll;
- public override void OnInspectorGUI() {
- scroll = EditorGUILayout.BeginScrollView(scroll, false, false);
- //parent = (Transform)EditorGUILayout.ObjectField("Parent transform", parent, typeof(Transform), false);
- ListSources();
- RemoveInvalidFiles();
- EditorGUILayout.EndScrollView();
- }
- private void OnSceneGUI() {
- if(prefabs.Count > 0) {
- HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
- var evt = Event.current;
- var cam = Camera.current;
- var mousePos = evt.mousePosition;
- mousePos.y = Mathf.Abs(mousePos.y - cam.pixelHeight);
- var ray = cam.ScreenPointToRay(mousePos);
- var hit = new RaycastHit();
- if(Physics.Raycast(ray, out hit)) {
- if(evt.type == EventType.MouseDown) {
- var go = PrefabUtility.InstantiatePrefab(randomPrefab) as GameObject;
- Undo.RegisterCreatedObjectUndo(go, go.name + " Creation");
- go.transform.up = hit.normal;
- go.transform.rotation = randomRot;
- go.transform.position = hit.point;
- go.transform.parent = parent;
- }
- }
- HandleUtility.Repaint();
- }
- }
- private Quaternion randomRot {
- get {
- return Quaternion.Euler(Vector3.up * Random.value * 360f);
- }
- }
- private GameObject randomPrefab {
- get {
- return prefabs[Random.Range(0, prefabs.Count)];
- }
- }
- private GameObject PrefabField(GameObject prefab) {
- var rect = EditorGUILayout.GetControlRect();
- var buttonRect = rect;
- buttonRect.xMin = rect.xMax - 15f;
- if(GUI.Button(buttonRect, "X", "ObjectFieldThumbOverlay"))
- return null;
- return PrefabField(prefab, rect);
- }
- private GameObject PrefabField(GameObject prefab, Rect rect) {
- rect.xMax -= 15f;
- return (GameObject)EditorGUI.ObjectField(rect, prefab, typeof(GameObject), false);
- }
- private GameObject[] DropAreaGUI() {
- var evt = Event.current;
- var dropArea = EditorGUILayout.GetControlRect();
- switch(evt.type) {
- case EventType.DragUpdated:
- case EventType.DragPerform:
- if(!dropArea.Contains(evt.mousePosition))
- return new GameObject[0];
- DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
- if(evt.type == EventType.DragPerform) {
- DragAndDrop.AcceptDrag();
- var references = DragAndDrop.objectReferences;
- var list = new GameObject[references.Length];
- for(int i = 0; i < references.Length; i++)
- list[i] = (GameObject)references[i];
- return list;
- }
- break;
- }
- var selectedFile = PrefabField(null, dropArea);
- if(selectedFile != null)
- return new GameObject[] { selectedFile };
- return new GameObject[0];
- }
- private void RemoveInvalidFiles() {
- for(int i = 0; i < prefabs.Count; i++)
- if(prefabs[i] == null)
- prefabs.RemoveAt(i);
- }
- private void ListSources() {
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Tree sources");
- for(int i = 0; i < prefabs.Count; i++)
- prefabs[i] = PrefabField(prefabs[i]);
- foreach(var file in DropAreaGUI())
- if(!prefabs.Contains(file))
- prefabs.Add(file);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement