Skip to content

Instantly share code, notes, and snippets.

@DomDomHaas
Created November 14, 2014 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DomDomHaas/bad737591dcf51896eca to your computer and use it in GitHub Desktop.
Save DomDomHaas/bad737591dcf51896eca to your computer and use it in GitHub Desktop.
Unity Editor Drag'n'Drop
using UnityEngine;
using UnityEditor;
using System.Collections;
using JSONPersistency;
[CustomEditor(typeof(DragNDrop))]
public class DragNDropInspector : Editor
{
Color picked = Color.green;
public override void OnInspectorGUI ()
{
GUILayout.Space (20);
Rect colRect = GUILayoutUtility.GetRect (20, 40);
Rect horizontalRect = EditorGUILayout.BeginHorizontal ();
picked = EditorGUI.ColorField (colRect, picked);
EditorGUILayout.EndHorizontal ();
checkDragable (colRect, picked);
GUILayout.Space (20);
}
private void checkDragable (Rect colRect, Color col)
{
string DragKey = "Color";
if (Event.current.type == EventType.DragUpdated
&& colRect.Contains (Event.current.mousePosition)) {
// if ok show texture Color
//if (gotDragableWithRender (DragAndDrop.objectReferences)) {
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (DragAndDrop.GetGenericData (DragKey) == null) {
DragAndDrop.SetGenericData (DragKey, col);
}
/* } else {
// show a rejected sign on the mousecursor
DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
}
*/
Event.current.Use ();
}
if (Event.current.type == EventType.DragPerform
&& colRect.Contains (Event.current.mousePosition)) {
Color color = (Color)DragAndDrop.GetGenericData (DragKey);
//if (gotDragableWithRender (DragAndDrop.objectReferences)) {
applyColorToRenderer (DragAndDrop.objectReferences, color);
//}
Event.current.Use ();
}
}
private void applyColorToRenderer (Object[] objs, Color color)
{
foreach (Object obj in objs) {
if (obj.GetType () == typeof(GameObject)) {
Renderer render = ((GameObject)obj).GetComponent<Renderer> ();
render.sharedMaterial.color = color;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment