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 Der_Kevin · Oct 21, 2015 at 02:14 PM · uimultiplayereventsysteminterface

UI: Navigate 2 Players in the Same Menu

Hey! I want to create a simple mario kart like select your player menu for 2 Players which looks like this at the moment MENU the black squares with the numbers 1-12 are button components and the yellow and blue selector graphics are sprites.

my question would be: how can i control/move the yellow selector with the WASD keys and the blue one with the ARROW keys to activate the buttons? (using wasd to highlight the buttons with a different color already works out of the bux, but not moving the selector sprite) and how can i save or output which button Player1 and/or Player 2 is currently selecting? so that i can put something like "Player1: Slot 2" and "Player2: Slot 8"

question-ui-selection.png (54.6 kB)
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
1
Best Answer

Answer by BeauWorlds · Oct 22, 2015 at 08:12 AM

Hi Der_Kevin, I would set the rectTransform.position of your navigator squares to be the same as the position of the square that they are on, I've made you an example of how to do this:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class SlotSelector : MonoBehaviour {
     public RectTransform navigator1;
     int nav1Pos = 0;
     public RectTransform navigator2;
     int nav2Pos = 0;
 
     public RectTransform[] slots = new RectTransform[12];

     public int jumpAmount = 4;

     public Text textShowNav1;
     public Text textShowNav2;

     void Start(){
     MoveNav1(0);
     MoveNav2(0);
     }

     void Update () {
         // move up
         if(Input.GetKeyDown(KeyCode.W)){
             MoveNav1(-jumpAmount);
         }
         if(Input.GetKeyDown(KeyCode.UpArrow)){
             MoveNav2(-jumpAmount);
         }
 
         if(Input.GetKeyDown(KeyCode.A)){
             MoveNav1(-1);
         }
         if(Input.GetKeyDown(KeyCode.LeftArrow)){
             MoveNav2(-1);
         }
 
         if(Input.GetKeyDown(KeyCode.S)){
             MoveNav1(jumpAmount);
         }
         if(Input.GetKeyDown(KeyCode.DownArrow)){
             MoveNav2(jumpAmount);
         }
 
         if(Input.GetKeyDown(KeyCode.D)){
             MoveNav1(1);
         }
         if(Input.GetKeyDown(KeyCode.RightArrow)){
             MoveNav2(1);
         }
     }
 
     void MoveNav1(int change){
         if(change > 0){
             if(nav1Pos+change < slots.Length-1){
                 nav1Pos += change;
             }else{
                 nav1Pos = slots.Length-1;
             }
         }else{
             if(nav1Pos+change >= 0){
                 nav1Pos += change;
             }else{
                 nav1Pos = 0;
             }
         }
         navigator1.position = slots[nav1Pos].position;
                 textShowNav1.text = "Nav 1 is at slot "+ nav1Pos;
     }
 
     void MoveNav2(int change){
         if(change > 0){
             if(nav2Pos+change < slots.Length-1){
                 nav2Pos += change;
             }else{
                 nav2Pos = slots.Length-1;
             }
         }else{
             if(nav2Pos+change >= 0){
                 nav2Pos += change;
             }else{
                 nav2Pos = 0;
             }
         }
         navigator2.position = slots[nav2Pos].position;
                 textShowNav2.text = "Nav 2 is at slot "+ nav2Pos;
     }
 }
 

If you create a .cs script named SlotSelector and paste this inside, then you can attach the script to a game object (any one you like) then you set "navigator1" to be your first navigator UI and "navigator2" to be your second. Next in the Array called "slots" you will see that there is space for 12 components, add your slot UIs to these component slots (remembering that arrays start at 0, so slot 1 will go to element 0, slot 2 will go to element 1 etc...) you can increase the length at the top if you wish to have more that 12 slots, or less.

Then set "textShowNav1" to a ui text component and "textShowNav2" to another, and this will tell you their current slot.

Your example photo shows rows of 4, if you wish to use rows of 6, for example, simply change "jumpAmount" in the inspector to 6.

If you have any questions or want me to explain some of the code, please don't hesitate to ask :) the script may look daunting but is actually very simple

Hope this helped, Beau C.

Comment
Add comment · Show 3 · 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 Der_Kevin · Oct 22, 2015 at 06:08 PM 0
Share

Hey! that works perfect! thank you so much! I only found 1 "bug" when iam at slot 10 for example and press down again, the selector goes to slot 12 and not to slot 2 or nowhere. but thats i $$anonymous$$or issue ;)

avatar image Eno-Khaon Der_Kevin · Oct 22, 2015 at 08:23 PM 0
Share

The position can be handled using modulo operators if you want to wrap from top to bottom and can also prevent changing lines from left to right as desired.

As a simple example, you can deter$$anonymous$$e position changes with:

 // left/right
 // From your current position, add or subtract 1 (moveInput as -1 or 1)
 // Example: 4 columns, 3 rows, (4x3 grid)
 // 0  1  2  3
 // 4  5  6  7
 // 8  9  10 11
 // From 7, we move right. That means
 // ((integer)(7 / 4) * 4) + ((7 + 1) % 4)
 // (1 * 4) + (8 % 4)
 // 4 + 0
 newPosition = ((currentPosition / columns) * columns) + ((currentPosition + moveInput) % columns);
 
 // left/right
 // From your current position, add or subtract 1 * columns (moveInput as -1 or 1 multiplier)
 // Example: 4 columns, 3 rows, (4x3 grid)
 // 0  1  2  3
 // 4  5  6  7
 // 8  9  10 11
 // From 10, we move down. That means
 // (10 + (4 * 1)) % (4 * 3)
 // 14 % 12
 // 2
 newPosition = (currentPosition + (columns * moveInput)) % (columns * rows);

This pair of formulas should be usable for any rectangular grid to loop back around both horizontally and vertically.

avatar image malialis · Jun 23, 2016 at 04:31 PM 0
Share

Hello.

Sorry for the silly question. Where do I put the added script

newPosition = ((currentPosition / columns) * columns) + ((currentPosition + moveInput) % columns);

Would I put that at the end of the selection on line 65 ?

I am new to C# and I am also wanting to make a selection menu to load characters.

Thank you in advance.

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

48 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

Related Questions

Canvas-prefab elements are not interactable 2 Answers

MultiplayerEventSystem Same UI BUG 0 Answers

Prevent MultiplayerEventSystem going to another playerRoot 0 Answers

uUI - OnSelect: From Mouse/Pointer or Keyboard/Controller? 0 Answers

Is there a way for the UI event system to read different selected objects through multiple players' input? 0 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