Created
September 28, 2012 15:19
-
-
Save bzgeb/3800476 to your computer and use it in GitHub Desktop.
Slightly more elaborate example Drag & Drop area in a custom inspector for the Unity editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEditor; | |
[CustomEditor (typeof(TriggerContainer))] | |
public class TriggerContainerEditor : Editor | |
{ | |
private SerializedObject obj; | |
public void OnEnable () | |
{ | |
obj = new SerializedObject (target); | |
} | |
public override void OnInspectorGUI () | |
{ | |
DrawDefaultInspector (); | |
EditorGUILayout.Space (); | |
DropAreaGUI (); | |
} | |
public void DropAreaGUI () | |
{ | |
Event evt = Event.current; | |
Rect drop_area = GUILayoutUtility.GetRect (0.0f, 50.0f, GUILayout.ExpandWidth (true)); | |
GUI.Box (drop_area, "Add Trigger"); | |
switch (evt.type) { | |
case EventType.DragUpdated: | |
case EventType.DragPerform: | |
if (!drop_area.Contains (evt.mousePosition)) | |
return; | |
DragAndDrop.visualMode = DragAndDropVisualMode.Copy; | |
if (evt.type == EventType.DragPerform) { | |
DragAndDrop.AcceptDrag (); | |
// Do stuff with our newly dragged object | |
foreach (Object dragged_object in DragAndDrop.objectReferences) { | |
AudioClip clip = dragged_object as AudioClip; | |
if (clip == null) | |
continue; | |
Object trigger_prefab = AssetDatabase.LoadAssetAtPath ("Assets/prefabs/Trigger.prefab", typeof(UnityEngine.Object)); | |
TriggerContainer t = obj.targetObject as TriggerContainer; | |
GameObject new_object = Instantiate (trigger_prefab) as GameObject; | |
new_object.transform.position = t.transform.position; | |
new_object.transform.parent = t.transform; | |
AudioSource ac = new_object.GetComponent<AudioSource> (); | |
ac.clip = clip; | |
} | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment