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 Teriander · Jan 18, 2012 at 07:30 AM · texturebuttonswap

GUI Texture on Button Swap

I have a button. I want it to switch textures when its clicked and return to normal when let go. Its a GUI Texture. Right now both "Up" and "Down" versions of the texture are in my project folder and not attached to any object. In the Inspector both texture types are set to "GUI".

The primary issue Im having it getting my variables to call for the textures and getting my IF statement to do the swap. This is what I have:

var btnSoldier_Up = GUI_Soldier_Up.Texture(109,96);

var btnSoldier_Down = GUI_Soldier_Down.Texture(109,96);

function OnGUI() {

 if (GUI.Button (Rect(0,0,109,96),btnSoldier_Up))
     renderer.material.mainTexture = btnSoldier_Down;
 

}

According to the help document linked below, it looks like this should work. But it's not. Any suggestions? http://unity3d.com/support/documentation/ScriptReference/Texture2D.Texture2D.html http://unity3d.com/support/documentation/Components/class-Texture2D.html

Unfortunately most of the help areas I found on searching were for C#, Im programming in Javascript so it's been a bit confusing for me. Thanks in advance!

Comment
Add comment
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

3 Replies

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

Answer by Teriander · Jan 18, 2012 at 08:40 AM

Thanks for replying Danny, I kind of wanted to avoid using GUIStyle because I tried uses it last week for other purposes but had problems. I think if I went back to it, it could solve my button issues but the old problems would return. I feel it could be easier to find a simple button mouseover effect.

I've made some changes to my old code and I think Im really close. But not sure what Im missing. If you can look at this and tell me what you think that would be great.

//Get Component Variables var btnSoldier_Up : GUITexture; var btnSoldier_Down : GUITexture;

function Start () {

 // Get Component to display Unit Purchases
 btnSoldier_Up = GameObject.Find ("GUI_Soldier_Up").GetComponent (GUITexture);
 btnSoldier_Down = GameObject.Find ("GUI_Soldier_Down").GetComponent (GUITexture);
 
 

}

function OnGUI() {

 //Create Buttons for Unit Purchases.
 if (GUI.Button (Rect(0,0,109,96),btnSoldier_Up)) {
     btnSoldier_Down.enabled = true;
 }else {
 btnSoldier_Down.enabled = false;
 }
 

}

The code seems to be accepted but I get this error message:

Assets/Script_SceneManager.js(71,24): BCE0023: No appropriate version of 'UnityEngine.GUI.Button' for the argument list '(UnityEngine.Rect, UnityEngine.GUITexture)' was found.

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 dannyskim · Jan 18, 2012 at 08:42 AM 0
Share

The error is stating that you're simply using a GUITexture as the texture property of your GUI.Button. You need to change btSoldier_Up to btnSoldier_Up.texture in your GUI.Button if statement

avatar image
1

Answer by dannyskim · Jan 18, 2012 at 08:22 AM

It's pretty confusing on what you're actually trying to accomplish here. I think this is because you have certain GUI elements confused on what they actually are, and how to access the properties of them.

If you're trying to change the texture of a

 GUI.Button

this is not changed through the renderer.material.mainTexture, seeing as how no GUI element or components that are part of the GUI class actually have a renderer. In fact, a

 GUITexture

does not have a renderer attached to it either. It seems like you understand how to access the texture of a GUITexture though, because of your variable declarations with GUI_Soldier_Up and GUI_Soldier_down.

I can also see that you applied a texture to the GUI.Button, but I am not aware of anyway to access that property without using a GUIStyle. If you try to make the GUI.Button a variable and try to access any of its properties like so:

 var myButton = GUI.Button( new Rect( 0, 0, 0, 0 ), myTexture );

you'll see that no properties are available to be changed.

So using a GUIStyle, you can do this:

 var myStyle : GUIStyle;

 if (GUI.Button (Rect(0,0,109,96), "", myStyle ))
     // Do Something here

When you declare the GUIStyle, in the inspector you'll see a whole bunch of settings that you can change for different button states. So On Normal, you can set a Texture, On Active, you can set a texture, etc.

These can be accessed through the properties of the GUIStyle that you created:

 myStyle.onActive.background = myTexture2D;
 myStyle.onNormal.background = myTexture2D_1;

The title of your post is misleading, which states that you want to change the texture property of a GUITexture, which is not a GUI.Button. If this is so, then you can do:

 var myGuiElement : GUITexture;
 var myTexture2D : Texture2D;

 myGuiElement.texture = myTexture2D;

but it seems like you already know how to do this.

One other thing, is I suggest you not use dynamic typing. One, it slows down your game in practically every case, and two, it makes your code harder to read and harder to keep track of. Strictly casting a type to your variables will save you a lot of trouble in the long run and leads to cleaner coding habits, imho. Also, if you plan on deploying to a mobile platform, then iOS doesn't allow dynamic typing for one, and two, using practically any of Unity's GUI implementations is not suggested because of resource hogging by said system. It's only good for menus with no gameplay action, and even then I wouldn't suggest using it.

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
avatar image
0

Answer by Teriander · Jan 18, 2012 at 10:14 AM

Thank you! The button is working without errors now, but the image is not displaying. Im going to have to play around a bit and see why it can't find the graphic. But I think this brings me closer to the solution! Appreciate your support!!

If you happen to notice off hand why the graphic wouldn't display Im all ears :) Till then I'll do some research. Thanks again!

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 starecho · Feb 17, 2012 at 05:10 PM 0
Share

$$anonymous$$aybe you can try

myStyle.active.background = myTexture2D; myStyle.normal.background = myTexture2D_1;

I just tried, the image is displayed.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI.Button Texture Swap? 3 Answers

Creating a component from a button click 1 Answer

Change button icon for each without multiple GUISkins 1 Answer

Null reference exception[SOLVED] 1 Answer

Deactivating a power up script after a certain amount of time 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