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
0
Question by Endless_Aftermath · Jun 06, 2014 at 01:03 AM · texturespritematerialoffsetgui-button

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"));
                             
    }
 }
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 Endless_Aftermath · Jun 08, 2014 at 04:04 AM 0
Share

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...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

alt text

 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 ;
 }




unitygridsolution.png (66.0 kB)
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

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

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


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