Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by victu · May 17, 2014 at 10:52 PM · editoreditor-scripting

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image victu · May 18, 2014 at 09:25 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ShaderGraph-like EditorWindow 0 Answers

Remove or disable an EditorGUILayout.PropertyField 0 Answers

Updating Player Settings with code 2 Answers

Created Prefabs and added them into a List in Editor Mode, after PlayMode, these GameObject are not in the List anymore 0 Answers

Is there a way to mass assign materials? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges