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
2
Question by vertti · Apr 24, 2015 at 11:14 AM · editorspriteeditorwindow

Display a Sprite in an EditorWindow

I have multiple spites in an atlas, I want display some of these in an EditorWindow.

     public static Sprite[] patterns;
 
     [MenuItem ("Window/Tile Selector")]
     static void Init () {
         patterns = Resources.LoadAll <Sprite> ("puzzle-pieces");  
         // Get existing open window or if none, make a new one:
         TileSelectWindow window = (TileSelectWindow)EditorWindow.GetWindow (typeof (TileSelectWindow));
         window.Show();
         Debug.Log ("Found: " + patterns.Length);
     }
 

This correctly displays Found: 9 but then running

     void OnGUI () {
         Texture2D aTexture = SpriteUtility.GetSpriteTexture(patterns[1], false);
         GUILayout.Label (aTexture);
     }
 

displays weirdly distorted texture atlas with ALL of the sprites..

Any way to display single sprites from multi sprite atlas?

Comment
Add comment · Show 1
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 vexe · May 01, 2015 at 03:38 AM 0
Share

@vertti please give us feedback. is your problem solved?

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Bunny83 · Apr 24, 2015 at 12:30 PM

The old GUI system isn't ment to be used with sprites. The old GUI system usually expects single textures to be displayed. However you can use GUI.DrawTextureWithTexCoords and pass the appropriate texcoords to the method.

I haven't tried this myself, but i think you should use Sprite.rect which gives you the texcoords in pixels. However DrawTextureWithTexCoords expects the coords to be in range (0..1). So you have to divide xMin and xMax by the texture's width and yMin and yMax by the texture's height.

This wilö work but is a bit complicated:

 void DrawOnGUISprite(Sprite aSprite)
 {
     Rect c = aSprite.rect;
     float spriteW = c.width;
     float spriteH = c.height;
     Rect rect = GUILayoutUtility.GetRect(spriteW, spriteH);
     if (Event.current.type == EventType.Repaint)
     {
         var tex = aSprite.texture;
         c.xMin /= tex.width;
         c.xMax /= tex.width;
         c.yMin /= tex.height;
         c.yMax /= tex.height;
         GUI.DrawTextureWithTexCoords(rect, tex, c);
     }
 }

If you want to displays textures in the old GUI system, it's better to have them as seperate textures. The old GUI system doesn't do any drawcall batching so there's no gain in using an atlas. Sprites are ment for the 2d framework or the new GUI system.

ps:
Note that my method uses the pixel size of the sprite to determine the size of the GUILayout rect. Ultimately you can use any screen rect to display the sprite's section of the texture. Keep in mind that if you do the texture might get zoomed / stretched.

If you need / want to use that atlas to bring the textures into the project, you could also split the atlas manually and create seperate textures at start by copying the relevant pixels into a seperate texture.

edit

With thie little helper function you can generate a seperate texture from a Sprite:

 Texture2D GenerateTextureFromSprite(Sprite aSprite)
 {
     var rect = aSprite.rect;
     var tex = new Texture2D((int)rect.width, (int)rect.height);
     var data = aSprite.texture.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
     tex.SetPixels(data);
     tex.Apply(true);
     return tex;
 }

Note: This method requires you to enable read / write access to the atlas texture. You have to switch the texture type to "advanced" and select "Read / Write Enable". Otherwise the GetPixel call will fail.

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 makaka-org · Feb 04, 2018 at 03:32 PM

☄ Header Image 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
avatar image
0

Answer by foxtrot117 · May 06, 2020 at 05:46 PM

if you just want to use it with EditorGUILayout handling position you can do it like that:

GUILayout.Box (spriteRef.texture);

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer

Set MinWidth for EditorWindow 1 Answer

Tell difference between Undo & Redo on callback 0 Answers

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

Editor Window Views 0 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