Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Siegewolf · Aug 30, 2018 at 08:48 AM · buttonjoystickcontrolsbind

Rebiding of keyboard/mouse/Joystick/Pad controls in the game

After jumping through a lot of hoops, I finally managed to write the code for control bindings... but almost...

From player's point of view, it is standard binding procedure. Inside the options menu/controls panel, there is a button named "jump" and when player clicks on it, game is going to wait for input of a new key. And this works... but only for keyboard keys and mouse buttons. Unless I write a really bizarre piece of code, I am unable to find solution for joystick/pad buttons to be accepted as well.

I am using OnGUI for this:

 void OnGUI
 {
     [...]
     event e = e.current;
     if(e.isKey || e.isMouse)
     {
         [...]
         if(e.isKey)
         {
             if(e.keyCode != KeyCode.Escape)
                 ControlBinds.buttonJump = e.keyCode;
             else
                 ControlBinds.buttonJump = KeyCode.None;
         }
         else if(e.isMouse)
         {
             if(e.button == 0)
                 ControlBinds.buttonJump = KeyCode.Mouse0;
             else if(e.button == 1)
                 ControlBinds.buttonJump = KeyCode.Mouse1;
             else if(e.button == 2)
                 ControlBinds.buttonJump = KeyCode.Mouse2;
         }
         [...]
     }
     [...]
 }

Btw, second if and following else if are yet another ugly piece of code, but it was the only way for me to force mouse button bindings to be accepted. And that only for three standard mouse buttons. Whatever KeyCode.Mouse3/4/5/6/7/8 are, those do not recognize my mouse's 4,5,6,7 buttons. Anyway, I am really missing here something like... e.isJoy ( if(e.isKey || e.isMouse || e.isJoy) ) and e.joyButton ( else if(e.joyButton) ), which is what is preventing me for allowing joystick button binds. It's quite frustrating that e.keyCode sees only keyboard keys, while KeyCode enum has mouse and joystick stuff as well. Is there a sane way around this limitation, perhaps?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Siegewolf · Aug 31, 2018 at 01:01 AM

It seems I was searching for solution in the wrong way.

I managed to find a different, better, way for this. In case anyone interested in my solution, check this video first...

https://www.youtube.com/watch?v=MEHe0Uf3m4I

...and then check this code of mine:

 Public EventSystem eventSystem;
 private GameObject currentButtonControls;
 private float rebindPause = 0.1f;
 void Update()
 {
     if(currentButtonControls != null)
     {
         eventSystem.gameObject.SetActive(false);
         currentButtonControls.GetComponent<Image>().color = new Color32(255, 0, 0, 255);
         currentButtonControls.transform.Find("Text").GetComponent<Text>().text = "Press key...";
         if(Input.anyKeyDown && rebindPause <= 0.0f)
         {
             System.Array values = System.Enum.GetValues(typeof(KeyCode));
             if(currentButtonControls.name == "Jump")
             {
                 if(!Input.GetKeyDown(KeyCode.Escape))
                 {
                     foreach(KeyCode key in values)
                     {
                         if(Input.GetKeyDown(key))
                             ControlBinds.buttonJump = key;
                     }
                 }
                 else
                     ControlBinds.buttonJump = KeyCode.None;
                 currentButtonControls.transform.Find("Text").GetComponent<Text>().text = ControlBinds.buttonJump.ToString();
             }
 
             currentButtonControls.GetComponent<Image>().color = new Color32(255, 255, 255, 255);
             eventSystem.gameObject.SetActive(true);
             eventSystem.SetSelectedGameObject(currentButtonControls);
             rebindPause = 0.1f;
 
             currentButtonControls = null;
         }
         else
             rebindPause -= Time.deltaTime;
     }
 }

ControlBinds is just a static class used at various places throughout the code.

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

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

Find out if any Button on any Gamepad has been pressed and which one 6 Answers

Two Horizontal Movement Axes Behaving Differently 1 Answer

Modifying properties of a buttonName 1 Answer

Why i cant press(use) joystick and gui button at the same time ???? 1 Answer

Simulate a mouse Click? 2 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