- Home /
Changing button texture by setting a texture offset of a sprite sheet?(SOLVED)
My issue seems as though it should be really simple and I perhaps am way over thinking this, but regardless I haven't been able to solve it. I have a 1280x1280 texture that is a sprite sheet that consists of a 10x10 array of images that I'm using for shirt logos (and other body part textures). I'm currently making a GUI button system that displays all of the parts on buttons, which the user will be able to press to apply the textures to their character.
The problem I'm having is I can't figure out how to set the offset to the buttons properly. No matter what methods I use, each button ends up displaying the entire texture and not the offset within the the texture.
This is what I currently have, but I've also tried creating a public material, changing it's "mainTextureOffset" and throwing the mainTexture into the texture spot at the end of the GUI.Box function as well and I get the same exact results every time.
Btw, I'm using boxes currently, just to get this working, but this will end up being buttons and also, I will only be displaying 9 buttons at a time.
Thanks ahead of time for any help. Again, I'm probably just too deep in thought over this. It seems like it should be simple.
for(int x = 0; x<3; x++)
{
for(int y = 0; y<3; y++)
{
mySkin.GetStyle("logo").contentOffset = new Vector2((x*-.1f), (y*-.1f));
GUI.Box(new Rect(scrX+arrayButX*x, scrY+arrayButy*y, arrayButX, arrayButy),"",mySkin.GetStyle("plainButton") );
GUI.Box(new Rect(scrX+arrayButX*x, scrY+arrayButy*y, arrayButX, arrayButy),"",mySkin.GetStyle("logo"));
}
}
I've searched around a bit more and what I've found is apparently you can't offset a gui element from a texture!? That sucks if it's true! There's got to be a solution to this somewhere! This seems like it should be a no brainer element to add to GUI. So, you'd have to make a separate texture for each button? Seems like a huge waste of data storage. I have more than 400 textures that need to be on buttons...this is horrible!
Please, someone give an assist here and show me that Unity isn't that horrible....especially for the price tag put on it...
Answer by Endless_Aftermath · Jun 08, 2014 at 09:49 PM
"Hey, Endless_Aftermath!"
"Yeah, Endless_Aftermath?"
"I solved your problem!"
"Great! Show me and everyone else!"
"Sure thing, Endless_Aftermath!"
The following code will place a GUI box in a 9x9 grid on the screen and display texture from a texture that is a grid of sprites/pics divided into a 10x10 grid (100 squares...example: 1280x1280 pic with 10x10 squares 128x128). It will display them in proper order. 81 texture squares will be included in the code for switching through sections of the texture's grid. 19 squares of the grid get wasted. (sorry about the crappy "Paint" drawings...and for any typos...I typed this all out. I use NotePad++ and there was an issue with my copy and paste for some reason).
using UnityEngine;
using System.Collections;
public Material logoMat; //add your material with the texture you want on your buttons
int texPage = 0; //variable for changing texture page/array displayed on buttons
public GUISkin mySkin; //GUI skin
///CreateOffsetButton()
Vector2 CreateOffsetButton(int page, Material myMat, Vector2 offset)
{
float offsetX =offset.x;
float offsetY=offset.y;
float scrX = Screen.width*.66f;
float butWidth = Screen.width*.0825f;
float butHeight = Screen.height*.18f;
float arrayButX = Screen.width*.11f;
float arrayButy = Screen.height*.26f;
float scrY = Screen.height*.01f+butHeight;
Vector2 myOffset =offset;
for(int x = 0; x<3; x++)
{
for(int y = 0; y<3; y++)
{
if (page<3)
{
offsetX = page*3+x;
offsetY =y;
}
if (page>2&&page<6)
{
offsetX = (page-3)*3+x;
offsetY = y+3;
}
if(page>5)
{
offsetX =( page-6)*3+x;
offsetY = y+6;
}
int off = 10;
int off2 =10;
Rect myRect = new Rect(scrX+arrayButX*x, scrY+arrayButy*y, arrayButX+5, arrayButy+5);
Rect myTexRect = new Rect((scrX+arrayButX*x)+5, (scrY+arrayButy*y)+off, arrayButX-off2, arrayButy-off2);
Rect myCoord = new Rect(offsetX*.1f,offsetY*.1f,.1f, .1f);
if(GUI.Button(myRect,"",mySkin.GetStyle("plainButton") ))
{
myOffset= new Vector2(offsetX*.1f,offsetY*.1f);
myOffset=new Vector2(myOffset.x, myOffset.y+.1f);
//return myOffset;
}
if (Event.current.type == EventType.Repaint)
{
Graphics.DrawTexture(myTexRect, myMat.mainTexture, myCoord, 0, 0, 0, 0, null);
}
}
}
return myOffset ;
}
Your answer
Follow this Question
Related Questions
Texture / Material offset doesn't work 1 Answer
Offset detail texture in c# 1 Answer
Overlay texture on sprite 0 Answers
Why can't I animate texture offset? 1 Answer
Scroll Ball Texture Smoothly 0 Answers