Skip to content

Instantly share code, notes, and snippets.

@bkevelham
Created December 2, 2021 12:17
Show Gist options
  • Save bkevelham/0afcec19a2126835a01cbe2afe1c6870 to your computer and use it in GitHub Desktop.
Save bkevelham/0afcec19a2126835a01cbe2afe1c6870 to your computer and use it in GitHub Desktop.
A minimal sample of how to handle ScriptableObject drag and drops into a scene view and onto a given GameObject.
//Minimal sample of how to drag a ScriptableObject into a scene
//onto a GameObject in that scene, and have something happen as a result
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(CustomScriptableObjectAsset))]
public class CustomScriptableObjectAssetEditor : Editor
{
//Note: Older versions of Unity do not have the index argument
//So if that gives you errors, just remove it.
internal void OnSceneDrag(SceneView sceneView, int index)
{
Event e = Event.current;
//Get the GameObject we're hovering over (if any)
GameObject go = HandleUtility.PickGameObject(e.mousePosition, false);
if (e.type == EventType.DragUpdated)
{
if (go)
{
DragAndDrop.visualMode = DragAndDropVisualMode.Link;
}
else
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
}
e.Use();
}
else if (e.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
e.Use();
//We've let go of the mouse here, so now we can do whatever we want to happen
//Get the ScriptableObject from the target
CustomScriptableObjectAsset asset = target as CustomScriptableObjectAsset;
//And now apply ...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment