Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 OWL7seven · May 15, 2014 at 12:51 AM · javascriptguitextureinputmanagerguibutton

guitexture to act as a button on the keyboard or mouse

i ve been looking all over the net and i cant find anything. i ve tried everything and cant get it to work

i want a gui Texture to do say Fire1 or Jump or anything that i have in my InputManager.

i m using gui.buttons and they work fine but i dont want to have to adjust each button on the different resolutions of phones. i find gui textures much easier

this is the gui.button script

 #pragma strict
 var input : InputItem;
 
 var position : Vector2; //position of button
 var dimensions : Vector2; //size of button
 var label : String; //text in button
 var toggle : boolean = false; //is this button a toggle?
 @HideInInspector
 var toggled : boolean = false; //are we currently toggled on?
 var showInStore : boolean = false;
 private var used : boolean = false;
 
 private var touched : boolean = false; //had we already touched the button
 private var touching : boolean = false; //are we currently touching the button
 @HideInInspector
 var curTouch : int = -1; //what touch id is this using?
 var useUpdate : boolean = true;
 
 function Update () {
     if(useUpdate)
         UpdateFunction();
 }
 
 function UpdateInput() {
     if(!useUpdate)
         UpdateFunction();
 }
 
 function UpdateFunction () {     
     //are we touching the button this frame?
     if(Input.touches.Length > 0){
         for(var touch : Touch in Input.touches){ //for each touch
             //Is this touch within our button?
             touching = Within(touch.position, Rect(position.x, position.y, dimensions.x,dimensions.y));
             if(touching){
                 curTouch = touch.fingerId; //save which touch we are using
                 break;
             }
         }
     } else {
         touching = false;
     }
     
     if(toggle){ //Toggle button
         input.got = toggled;
         
         if(touching){
             if(!touched){ //first frame touching the button
                 touched = true;
                 
                 input.up = toggled;
                 toggled = !toggled; //invert the toggle
                 input.down = toggled;
             } else {
                 input.down = false;
                 input.up = false;
             }
         } else {
             input.down = false;
             input.up = false;
             touched = false;
             curTouch = -1;
         }
         
     } else { //Normal Button
         if (touching){ //We are touching
             input.got = true; //the button is down
             input.up = false; //the button is not up
                 
             if(!touched){// we hadn't already touched the button (first frame holding it)
                 input.down = true; //the button was got
                 touched = true; //we have touched    
             } else {
                 input.down = false; //it isn't down because this isn't the first fram holding it
             }
         } else { //We are not touching
             curTouch = -1;
             if (touched) {
                 input.up = true; //if we were holding the button last fram, then up is true because this is the frame it was released
             } else {
                 input.up = false;
             }
                 touched = false;
                     input.got = false;
                 input.down = false;
         }
     }
 }
 
 function OnGUI () {
     if(!DBStoreController.inStore || showInStore)
         GUI.Button(Rect(position.x, position.y, dimensions.x,dimensions.y),label);
 }
 
 function Within (pos : Vector2, bounds : Rect) : boolean {    
     pos.y = Screen.height - pos.y;
     return (pos.x > bounds.x && pos.x < (bounds.x + bounds.width) && pos.y > bounds.y && pos.y < (bounds.y + bounds.height));
 }

i would place the Fire1 gameobject as inputitem and it works i want the same thing for the gui.texture

please help been looking for an answer for days.

Comment
Add comment · Show 3
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 robertbu · May 15, 2014 at 01:21 AM 0
Share

I don't understand your title. To use a GUITexture as a button, you can use GUITexture.HitTest() to text the finger or mouse position to see if it is a hit.

http://docs.unity3d.com/Documentation/ScriptReference/GUIElement.HitTest.html

avatar image OWL7seven · May 15, 2014 at 04:05 AM 0
Share

for example in the editor when pressed play. i would press f to say switch on a flashlight. now on a mobile device there is no keyboard i would want now a gui texture to replace the function on the f key( in other words pressing the gui texture would be the same as pressing the f key)

avatar image robertbu · May 15, 2014 at 04:54 AM 0
Share

So inside your for loop that cycles through the touches, you can have something like:

 if (touch.phase == TouchPhase.Begin && guiTextFlashlight.HitTest(touch.position) {
     // Do whatever to turn on/off the flashlight
 }

Where 'guiTextureFlashlight' is a reference to the GUITexture button that you want to use to control the flashlight.

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Fade multiple GuiTextures in ONE Javascript 1 Answer

(JS) Best way to make it so that a player can only issue a "move click" once? 1 Answer

Pairing and GUITexture with a Timer? 1 Answer

Removing crosshair on save 1 Answer

Changing input sensitivity via scripting (JS) 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