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 LukaKotar · Jan 18, 2012 at 01:58 PM · iosbuttonguitexture

How do I set up a GUITexture as a functional button?

I wrote details on what I'm trying to do with notes in the script:

 //This is the script from Unity's FPS tutorial.
 
 function Start () {
     SelectWeapon(0);
 }
 
 function Update () {
         //Works fine with the touch input, but I want my GUITexture do that job
     if (Input.touchCount > 0 && 
       Input.GetTouch(0).phase == TouchPhase.Began)
 
       //I don't need weapon switching
 
     if (Input.GetKeyDown("1")) {
         SelectWeapon(0);
     }    
     else if (Input.GetKeyDown("2")) {
         SelectWeapon(1);
     }    
 }
 
 function SelectWeapon (index : int) {
     for (var i=0;i<transform.childCount;i++)    {
         // Activate the selected weapon
         if (i == index)
             transform.GetChild(i).gameObject.SetActiveRecursively(true);
         // Deactivate all other weapons
         else
             transform.GetChild(i).gameObject.SetActiveRecursively(false);
     }
 }

I'd highly appreciate a reply. I hope you understand my question.

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

4 Replies

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

Answer by Noah-1 · Jan 19, 2012 at 06:36 PM

Here you go bro:

var fireButton : GUITexture;

function Update(){

for (var evt : Touch in Input.touches) {

var HitTest1 = fireButton.HitTest(evt.position);

if (evt.phase == TouchPhase.Began) {

if(HitTest1){

//do something when button is hit

Debug.Log ("ButtonGUI Clicked");

fireButton.enabled = false;

} } } }

Just rememberto set a texture in the inspector ;)

Comment
Add comment · Show 2 · 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 LukaKotar · Jan 19, 2012 at 06:56 PM 0
Share

Thanks! :D Is there a way to activate another script with a GUI? Btw, your GUI works perfect! :D

avatar image Noah-1 · Jan 19, 2012 at 08:22 PM 0
Share

$$anonymous$$mm if you mean to activate a code in a certain moment when you click a button, look up for enable in Unity Script reference, that shoul make it ;)

avatar image
3

Answer by casimps1 · Jan 18, 2012 at 10:45 PM

All you need to do is create a new script and add that script to your GUITexture object. Then add an OnMouseDown( ) method to the script and do whatever you want to do inside that.

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
2

Answer by mehowe7 · Jan 18, 2012 at 03:21 PM

 var btnTexture : Texture;

function OnGUI() {

 if (GUI.Button(Rect(10,10,50,50),btnTexture)){
     Debug.Log("Clicked the button with an image");
 }
Comment
Add comment · Show 2 · 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 Wolfram · Jan 18, 2012 at 03:43 PM 0
Share

He is referring to GUITextures, which are GUIElement objects, and not UnityGUI objects created/handled in OnGUI().

avatar image LukaKotar · Jan 19, 2012 at 04:25 PM 0
Share

Thanks! I didn't think you can add textures to normal Unity GUI Buttons! :D

avatar image
2

Answer by Wolfram · Jan 18, 2012 at 03:43 PM

Use GUILayer.HitTest():

http://unity3d.com/support/documentation/ScriptReference/GUILayer.HitTest.html

Or if you want to implement your own hit test, you can compare the current mouse position on a click with a GUIElement using GUIElement.GetScreenRect().

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

8 People are following this question.

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

Related Questions

Shoot when GUI is pressed? 0 Answers

Only a button can work 1 Answer

[C#] guitexture as button for player move on iOS/android 1 Answer

How do I set up a GUITexture as a button? (Java Script) 2 Answers

How do I place buttons on the screen using GUITexture? 3 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