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
1
Question by erlingno · Dec 22, 2013 at 06:01 PM · guitexturesprite

How can I access a sprites texture and add it to a GUI button?

Hi there!

I have a list of sprite gameObjects that I would like to be displayed in the GUI as a sort of inventory, but I'm unable to access the texture. I would like the buttons to have the same image on them as the sprite.

I'm probably going about this in the wrong way, but I thought this would be the simplest way to display an item that had been picked up into the inventory.

Any assistance would be greatly appreciated!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class BrainScript : TouchScript {

 public List<GameObject> Inventory;


}

 void Start () {
     
             Inventory = new List<GameObject>();
 
                 // Adding a gameobject manually for testing
             Inventory.Add(GameObject.Find("goldCoin"));
 
             Debug.Log("Inventory contains: " + Inventory[0]);
             Debug.Log(Inventory[0].renderer.material.mainTexture);
             Debug.Log(Inventory[0].renderer.material.GetTexture("_MainTex"));
     }
 

 void OnGUI()
     {
         GUILayout.BeginHorizontal ();
 
         foreach(GameObject obj in Inventory)
         {
             //Texture2D testTexture = obj.renderer.material.mainTexture as Texture2D;
             Texture2D testTexture = obj.renderer.material.GetTexture("_MainTex") as Texture2D;
 
             GUILayout.Button(testTexture);
         }
         GUILayout.EndHorizontal();
 }


Debug.Log's output:

 Inventory contains: goldCoin (UnityEngine.GameObject)
 Null
 UnityEngine.Debug:Log(Object)
 Null
 UnityEngine.Debug:Log(Object)


The buttons are displayed, but with no contents.

Comment
Add comment · Show 6
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 Sundar · Dec 23, 2013 at 02:43 AM 0
Share

Try to debug in OnGUI like

 void OnGUI()
     {
        GUILayout.BeginHorizontal ();
 
        foreach(GameObject obj in Inventory)
        {
              if( obj.material.mainTexture != null )
              {
                      GUILayout.Button( obj.material.mainTexture );
              }
              else Debug.Log("no texture attached to "+ obj.name);
        }
        GUILayout.EndHorizontal();
 }


We will go from there

avatar image erlingno · Dec 23, 2013 at 02:05 PM 0
Share

Thanks for you reply!

As expected your code prints:

 no texture attached to goldCoin
 UnityEngine.Debug:Log(Object)


Though it is textured in the scene! The object I add with Inventory.Add(GameObject.Find("goldCoin")); is a fully visible sprite with a coin texture on it.

I'm at a loss :/

avatar image Sundar · Dec 23, 2013 at 02:45 PM 0
Share

Can you take a snap shot of your inspector with your sprite goldcoin selected and all its components showing.

avatar image erlingno · Dec 23, 2013 at 03:03 PM 0
Share

Sure thing: alt text

inspectorsnap.png (78.6 kB)
avatar image Sundar · Dec 23, 2013 at 05:02 PM 0
Share

Try this

     void OnGUI()
     {
     GUILayout.BeginHorizontal ();
      
     foreach(GameObject obj in Inventory)
     {
         Texture sTex = obj.GetComponent<SpriteRenderer>().material.mainTexture;
         if( sTex != null )
         {
             GUILayout.Button( sTex );
         }
         else Debug.Log("no texture attached to "+ obj.name);
     }
     GUILayout.EndHorizontal();
     }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by georgeq · May 21, 2014 at 06:27 PM

You must first get the sprite renderer, like this:

 SpriteRenderer r = obj.GetComponent<SpriteRenderer>();

then you can easily access the texture like this:

 Texture2D t = r.sprite.texture;



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 allesisda · Nov 13, 2014 at 09:40 PM 2
Share

and what do you do if you have a spritesheet with multiple sprites, but only want to access one of it?

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

Gui.Box - Sprite 0 Answers

Sprites and minmizing memory usage 1 Answer

ObjectField Sprite picker: The texCoords size is always one, on Single Sprite textures 0 Answers

Gui Box - Texture - Spritesheet 0 Answers

Sprite editor and texture 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