- Home /
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.
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
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 :/
Can you take a snap shot of your inspector with your sprite goldcoin selected and all its components showing.
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();
}
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;
and what do you do if you have a spritesheet with multiple sprites, but only want to access one of it?
Your answer
Follow this Question
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