• 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
Question by AnsonRutherford · Jan 17, 2014 at 06:14 AM · c#2dguitexture

Equivelant of GUI.DrawSprite() ?

Here's what I want to have happen: I want to use Resource.LoadAll to grab a spritesheet with six cells, and put them into an array, with each cell as its own position in the array, so Array[0] is frame 1, Array[1] is frame 2, so on. Then, I want to be able to draw those images directly to the screen, like with GUI.DrawTexture.

Here's what I have to so far:

 Texture2D[] Textures = new Texture2D[6];
 Textures = Resources.LoadAll<Texture2D>("filepath");
 GUI.DrawTexture (new Rect(x,y,w,l), Textures[0]);

The problem is that the spritesheet is a Texture2D, but I can't figure out how to separate it into its individual frames. I've also heard using Textures = Resources.LoadAll("filepath"); would get the individual sprite frames, but then I can't draw them with GUI.DrawTexture.

Is there someway to either draw sprites, or to import each sprite frame as Texture2Ds?

Comment
rakkarage
rahuljain1311

People who like this

2 Show 3
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 robertbu · Jan 17, 2014 at 06:16 AM 0
Share

Here is an approach that may or may not work depending on how regular the presentation of each frame is within the sheet. You can use GUI.DrawTextureWithTexCoords() to select a section of the sprite sheet to display.

avatar image SimonDarksideJ · Apr 11, 2014 at 03:38 PM 0
Share

Using GUI.DrawTextureWithTexCoords() does not work. Tested repeatedly. Doing GUI.GUI.DrawTexture() using the sprites texture does work fine through, but obviously this does not work well with spritesheets.

avatar image arzi · Jun 02, 2014 at 03:17 PM 2
Share

Using GUI.DrawTextureWithTexCoords() works fine.

 Sprite s = sprites[0];
 
             Texture t = s.texture;
             Rect tr = s.textureRect;
             Rect r = new Rect(tr.x / t.width, tr.y / t.height, tr.width / t.width, tr.height / t.height );
 
             GUI.DrawTextureWithTexCoords(new Rect(0, 0, tr.width, tr.height), t, r);

0 Replies

· Add your reply
  • Sort: 

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How to gradient-wise fill a bitmap programmatically 1 Answer

Problem with playmaker gui and "Get mouse button down" action. 1 Answer

Texture2D not showing in build, but shows up in editor 1 Answer

How can i show number of lives as image during gameplay 1 Answer

Checking for Player to Object distance for other scripts to check state. 1 Answer


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