• 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
Question by Faniha · May 19, 2015 at 02:20 AM · c#2dguiclick

urgent, pls help. Selection through keyinput in Canvas

Right now I'm working on the Start Menu for my 2d game - I'm doing it through canvas. It's working fine if I wanted to use mousepad and click for selection, but my game is gonna be hooked up to two controllers that only take arrow keys, g and f as input.

In game the players(it's a multiplayer) controls three input keys each to move a sprite around (arrowkeys for movement and they need to both click f and g to attack).

Is it in any way possible to use this concept to click on button a in the UI? (as in the player moves a gameobject around and if it's untop a button and they do the FG click, the button is seen as clicked)

Added part of my code so you can see what is used to move the object in game:

 public class simpleMove : MonoBehaviour {
 
     Animator anim;
     
     float timeframe = 0;
     float timeframe1 = 0;
     bool chkinput=false;
     bool chkinput1=false;
     bool checkani=false;
 
     float speed = 5;
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator> ();    
     }
     
     public void MovementHand (){
         //hand movement
         var move = new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0);
         transform.position += move * speed * Time.deltaTime;
         
         //x axis stop
         if (transform.position.x <= -6.5f) {
             transform.position = new Vector2 (-6.5f, transform.position.y);
         } else if (transform.position.x >= 6.5f) {
             transform.position = new Vector2 (6.5f, transform.position.y);
         }
         
         // Y axis stop
         if (transform.position.y <= -5.0f) {
             transform.position = new Vector2 (transform.position.x, -5.0f);
         } else if (transform.position.y >= 5.0f) {
             transform.position = new Vector2 (transform.position.x, 5.0f);
         }
     }
 
     public void Attack (){
         if (Input.GetKeyDown (KeyCode.F)) {    
             chkinput = true;
         }
         if (chkinput) {
             timeframe += 1 * Time.deltaTime; 
             if (Input.GetKeyDown (KeyCode.G)) {
                 checkani=true;
                 timeframe = 0;
                 chkinput = false;
                 //This is where I would like to click
             }
             if (timeframe > 1) {
                 timeframe = 0;
                 chkinput = false;
                 
             }
         }
         if (Input.GetKeyDown (KeyCode.G)) {    
             chkinput1 = true;
         }
         if (chkinput1) {
             timeframe1 += 1 * Time.deltaTime; 
             if (Input.GetKeyDown (KeyCode.F)) {
                 checkani=true;
                 timeframe1 = 0;
                 chkinput1 = false;
                 //This is where I would like to click
 
             }
             if (timeframe1 > 1) {
                 timeframe1 = 0;
                 chkinput1 = false;
             }
         }
     }
     
     public void OnClick(){
         if (checkani) {
             anim.SetTrigger ("attack_the_bug");
             AudioSource sound = gameObject.GetComponent<AudioSource> ();
             //or here...
             sound.Play ();
             checkani = false;
         }
     }
 
     // Update is called once per physics timestep
     void Update () {
         MovementHand ();
         Attack ();
         OnClick ();
     }
 }


hope you guys can help..

Comment

People who like this

0 Show 2
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 HTwist · May 19, 2015 at 02:39 AM 0
Share

I am not sure if it what you want but if you want to simulate a mouse click from code, you could use ExecuteEvents.Execute. http://forum.unity3d.com/threads/force-a-pointer-press-on-a-button-from-code-as-if-it-came-from-a-mouse-touch-event.280131/

That way, when user press F or G, you would simulate a mouse click on the respective button.

Again I am not sure if it is what you want which is why i posted it as a comment. Hope this help!

avatar image Faniha · May 19, 2015 at 03:13 AM 0
Share

from what other people are writing about it, this seems to be what I want :)

Trigger a button's onClick from keyinput. or in my case where I want to trigger it from within a function that only runs if the correct keyinputs happen (in OnClick).

I just cannot figure out how I should go about implementing it.. tried to get it to work, no luck (that may have something to do with an all nighter, but who knows) - do you know how to do it? :S

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by HTwist · May 19, 2015 at 04:05 AM

If you want to simulate a button click when F and G is pressed, I would do something like that in my MonoBehaviour:

 public Button button; //assign the button from the inspector
 
 void Update() {
  if(Input.GetKeyDown(KeyCode.F) && Input.GetKeyDown(KeyCode.G)) {
     ExecuteEvents.Execute(button.gameObject, new BaseEventData(EventSystem.current), ExecuteEvents.submitHandler);    
  }
 }

It should do it although I haven't tested it.

Comment
Faniha

People who like this

1 Show 0 · 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

Answer by sas_88 · May 19, 2015 at 11:01 AM

Hi,

Try with this.

             float h;
             float v;
             
             bool uimove;
             
             public void MovementHand ()
             {
               if(!uimove)
               {
                h=Input.GetAxis ("Horizontal");
                v=Input.GetAxis ("Vertical");
               }
               var move = new Vector3 (h,v, 0);
              transform.position += move * speed * Time.deltaTime;
             }
     
     void OnGUI()
         {    
             uimove=false;
             if (GUI.RepeatButton(new Rect(Screen.width-300, Screen.height-150, 100, 30), "Uparrow"))
             {
                         uimove=true;
                         h=0;
                         v=1.0f;
             }
     
             if (GUI.RepeatButton(new Rect(Screen.width-300, Screen.height-50, 100, 30), "Downarrow"))
             {
                          uimove=true;
                          h=0;
                          v=-1.0f;
             }
     
             if (GUI.RepeatButton(new Rect(Screen.width-400, Screen.height-95, 100, 30), "leftarrow"))
             { 
                         uimove=true;
                         h=-1.0f;
                         v=0;
             }
             
             if (GUI.RepeatButton(new Rect(Screen.width-200, Screen.height-95, 100, 30), "Downarrow"))
             {
                         uimove=true;
                         h=1.0f;
                         v=0;
             }
     
         }
             ///// press the Ui button make the value as follows
              uimove=true;
              h=1.0f
              v=1.0f
             ///// Release the Ui button make the value as follows
               uimove=false;
    

Try to implement in this way by assigning value to v&h directly,Input.GetAxis ("Horizontal") & Input.GetAxis ("Vertical") takes maximum value 1 & minimum value -1.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Detect click on two objects that are on top of each other 1 Answer

I got 60 errors while trying to make a build for my android game 0 Answers

Scrolling UI runtime problem. 0 Answers

Clamp To The Screen Edge 0 Answers

GUI trigger help (C#) 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