- Home /
Allowing User to Drag a Texture2D to a region of a custom editor
I want to write a custom editor, where in the user can drag a Texture2D onto a region (ScrollView) and show a Window/ScriptableWizard to allow the user to define how to convert the Texture2D to the underlying structure. Is there a way to allow this behavior. Only thing I care about is having a region to accept only Texture2D, of a custom size. The rest of the behavior after that should be trivial.
Answer by victu · May 18, 2014 at 09:50 PM
Rect scrollViewRect = GUILayoutUtility.GetLastRect ();
if(Event.current.type == EventType.DragExited || Event.current.type == EventType.DragUpdated)
{
if(scrollViewRect.Contains(Event.current.mousePosition))
{
Texture2D[] objects = new Texture2D[DragAndDrop.objectReferences.Length];
int i = 0;
foreach(UnityEngine.Object obj in DragAndDrop.objectReferences)
{
if(obj is Texture2D)
{
objects[i] = obj as Texture2D;
++i;
}
}
for(int j = 0; j < i; ++j)
{
Debug.Log (objects[j]);
}
}
}
This is the cleanest way I could find of doing it. Do to the way Unity's selection works it is not really possible to do this in a custom Editor with mulit-select. However using an EditorWindow you should be able to.
Answer by Jeff-Kesselman · May 17, 2014 at 10:55 PM
I haven't tried this with a Texture2D but, in general, if you declare a public field with a specific type, then if the editor displays it at it all it will restrict drag and drop to that type.
There is a special variant on this behavior where, if you declare a public field of a Component type, then it will accept a GameObject but ONLY one that has that type as a component, and assign that game object's matching component to the field.
I don't want a field. I want a region. i.e a Rect that I can specify that overlaps a ScrollView allowing the user to drag Texture2D onto the specified region and allow me to process them as needed.
What you suggest works if I have a variable that backs the field. However I want to support the user dragging N Texture2D objects into an area I defined.
Next I need N windows/steps in a window/etc to allow the user to set up some values. Then from this I will generate a prefab in a user specified location.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145257im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ShaderGraph-like EditorWindow 0 Answers
Remove or disable an EditorGUILayout.PropertyField 0 Answers
Updating Player Settings with code 2 Answers
Is there a way to mass assign materials? 4 Answers