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 Zilby · Oct 03, 2018 at 12:54 AM · uicontrollernavigationcontrols

360 Degree UI Controller Automatic Navigation

So I've been working on integrating controllers into my game, and thus have needed my UI to be navigable by controllers. To do this, I've been using Unity's UI Selectable class and its automatic navigation (with some exceptions where I use explicit). This however seems increasingly insufficient, especially for UI that gets dynamically filled in, such as my awards screen as shown here:

. alt text .

I was wondering if anyone's found solutions to simply get the UI element that the controller axis is pointing at (which would enable more than 4 directional UI selection). It seems like there should be a really obvious answer to this but I haven't been able to find one anywhere, and it's bumming me out x__x

awards.png (147.9 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
0
Best Answer

Answer by Zilby · Oct 03, 2018 at 03:15 AM

Ended up writing a script for it overriding Selectable by modifying a default Selectable script. It uses some of my own input manager RInputManager to determine if the UI is currently using controller input, but aside from that it can be used basically anywhere. (Can also have it override Button to use it with those):

.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System.Collections;

 /// <summary>
 /// OmniDirectionalUI allows for UI to receive directional input from a controller in any direction. 
 /// Written by Alex Zilbersher.
 /// </summary>
 public abstract class OmniDirectionalUI : Selectable
 {
     private static bool justMoved = false;
     private const float MOVE_DELAY = 0.2F;
 
 
     protected override void Start()
     {
         base.Start();
         Navigation n = new Navigation();
         n.mode = Navigation.Mode.Explicit;
         navigation = n;
         interactable = true;
     }
 
     public virtual void Update()
     {
         if (RInputManager.ControllerInput && currentSelectionState == SelectionState.Highlighted)
         {
             Vector3 dir = new Vector3(RInputManager.Horizontal(RInputManager.P1), RInputManager.Vertical(RInputManager.P1), 0.0f);
             
             if (dir.magnitude > 0.1f && !justMoved)
             {
                 Selectable s = FindSelectable360(dir);
                 if (s != null)
                 {
                     EventSystem.current.SetSelectedGameObject(s.gameObject);
                     StartCoroutine(Moved());
                 }
             }
         }
     }
 
     protected IEnumerator Moved()
     {
         justMoved = true;
         yield return new WaitForSecondsRealtime(MOVE_DELAY);
         justMoved = false;
     }
 
     // Find the next selectable object in the specified world-space direction.
 public Selectable FindSelectable360(Vector3 dir)
 {
     dir = dir.normalized;
     Vector3 pos = (Vector3)(transform as RectTransform).rect.center; 
     pos = transform.TransformPoint(pos);
     float minDist = Mathf.Infinity;
     Selectable bestPick = null;

     for (int i = 0; i < allSelectables.Count; ++i)
     {
         Selectable sel = allSelectables[i];

         if (sel == this || sel == null)
             continue;

         if (!sel.IsInteractable() || sel.navigation.mode == Navigation.Mode.None)
             continue;

         var selRect = sel.transform as RectTransform;
         Vector3 selCenter = selRect != null ? (Vector3)selRect.rect.center : Vector3.zero;
         selCenter = sel.transform.TransformPoint(selCenter);
         Vector3 myVector = (selCenter - pos);

         float angle = Vector2.Distance(dir, myVector.normalized);

         if (angle > 1)
             continue;

         float dist = Vector2.Distance(pos, selCenter);

         float score = angle + (dist / 5);

         if (score < minDist)
         {
             minDist = score;
             bestPick = sel;
         }
     }
     return bestPick;
 }
 }
 
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

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

How do I have button navigation work for only one of my players? 1 Answer

Unity Standard Assets 0 Answers

Unity Android Change Color of Navigation bar 0 Answers

How do I add input for two different local players? 1 Answer

Move through selected buttons one by one 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