• 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
1
Question by Brainshack · Jul 18, 2014 at 06:15 AM · editorsprite

How to best render a sprite in an EditorWindow and add a Click Event to it?

Hi,

I am currently working on a little Tile based Map Editor, using unity sprites. The problem right now is, that I dont know the best way to render a single sprite (not the whole sheet) inside an editor window.

What I currently do is, that I take the sprites texture, and render a part of it into a new Texture2D, using the GetPixels function and the sprites textureRect. However, to do this inside a Script, the texture needs to have some specific import settings for me to be allowed to read data from it. (Where do i set the import properties?)

Also I would like to know how I add a click event to this sprite.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by benbenmushi · Jul 18, 2014 at 09:03 AM

Hi !

You can render a Sprite into EditorGUI using...

GUI.DrawTextureWithTexCoords or Graphics.DrawTexture

Here's some code from one of my project. I'm using thoses functions to display my sprites on my own EditorWindow

     public static void DrawTexture(Rect position, Sprite sprite, Vector2 size)
     {
         Rect spriteRect = new Rect(sprite.rect.x / sprite.texture.width, sprite.rect.y / sprite.texture.height,
                                    sprite.rect.width / sprite.texture.width, sprite.rect.height / sprite.texture.height);
         Vector2 actualSize = size;
 
         actualSize.y *= (sprite.rect.height / sprite.rect.width);
         Graphics.DrawTexture(new Rect(position.x, position.y + (size.y - actualSize.y) / 2, actualSize.x, actualSize.y), sprite.texture, spriteRect, 0, 0, 0, 0);
     }

 
     public static void DrawTextureGUI(Rect position, Sprite sprite, Vector2 size)
     {
         Rect spriteRect = new Rect(sprite.rect.x / sprite.texture.width, sprite.rect.y / sprite.texture.height,
                                    sprite.rect.width / sprite.texture.width, sprite.rect.height / sprite.texture.height);
         Vector2 actualSize = size;
 
         actualSize.y *= (sprite.rect.height / sprite.rect.width);
         GUI.DrawTextureWithTexCoords(new Rect(position.x, position.y + (size.y - actualSize.y) / 2, actualSize.x, actualSize.y), sprite.texture, spriteRect);
     }


Hope it's help !

Comment
Add comment · Show 3 · 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 Brainshack · Jul 18, 2014 at 09:24 AM 0
Share

Thanks mate, I am going to try that out when i come back home from the office :)

avatar image Gru · Oct 14, 2015 at 02:53 PM 0
Share

This didn't work for me. Brainshack, did you find a solution to this?

avatar image Ronnie_0 · Jul 30, 2016 at 05:29 AM 0
Share

Wonderful! Thank you.

avatar image
0

Answer by kamuzai1226 · Jul 18, 2014 at 07:05 AM

I don't quite understand the problem, but setting the sprite sheet's mode to multiple and then in the Sprite Editor, you slice them using the automatic slicer on the top left. Does this help?

http://docs.unity3d.com/Manual/class-TextureImporter.html

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 Brainshack · Jul 18, 2014 at 07:31 AM 0
Share

Well, not really.

I allready have my sprite sheet set up properly and now want to render a sprite inside an Editor Window using the GUI / GUILayout / EditorGUI / EditorGUILayout classes and treat it basically like a button.

avatar image
0

Answer by makaka-org · Feb 04, 2018 at 03:29 PM

☄ Header Image with Click Event (Open Link) in Publisher Window

Publisher Window is a welcome screen where the user can learn all the important information about the product (unity asset or project) and your company by visiting important links.

☄ Download: http://u3d.as/HKG
☄ Documentation: https://makaka.org/publisher-tools-unity-asset-store/

☄ Video: https://www.youtube.com/watch?v=RQ2fxox_JkU

alt text

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Highlighting multiple sprites automatically changes all their materials in Unity 5?! 1 Answer

Editor script to slice sprites 4 Answers

Run sprite animation in editor mode? 0 Answers

Sprite Previews not displaying! 0 Answers

Serialize class that derives from abstract class 2 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