Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.