Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Elit3d · Jan 11, 2015 at 10:25 PM · c#scrollgamepadrect

ScrollRect Gamepad support?

So currently the scrollrect only works with mouse scrollwheel and mouse drag. Is there actually a way to get it working with gamepad? Eg right analog or something? Looked at documentation, didnt see anything really useful

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
2
Best Answer

Answer by KurtGokhan · Jan 15, 2015 at 03:59 PM

You can implement a new class from Scrollrect and IMoveHandler. Also IPinterClickHandler so you can select the object on pointer click. Full code looks like this. You should attach this to your ui object which is going to be ScrollRect.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class MoveScrollRect : ScrollRect, IMoveHandler, IPointerClickHandler {
     private const float speedMultiplier = 0.1f;
     public float xSpeed = 0;
     public float ySpeed = 0;
 
     private float hPos, vPos;
 
     void IMoveHandler.OnMove(AxisEventData e) {
         xSpeed += e.moveVector.x * (Mathf.Abs(xSpeed) + 0.1f);
         ySpeed += e.moveVector.y * (Mathf.Abs(ySpeed) + 0.1f);
     }
 
     void Update() {
         hPos = horizontalNormalizedPosition + xSpeed * speedMultiplier;
         vPos = verticalNormalizedPosition + ySpeed * speedMultiplier;
 
         xSpeed = Mathf.Lerp(xSpeed, 0, 0.1f);
         ySpeed = Mathf.Lerp(ySpeed, 0, 0.1f);
 
         if(movementType == MovementType.Clamped) {
             hPos = Mathf.Clamp01(hPos);
             vPos = Mathf.Clamp01(vPos);
         }
 
         normalizedPosition = new Vector2(hPos, vPos);
     }
 
     public void OnPointerClick(PointerEventData e) {
         EventSystem.current.SetSelectedGameObject(gameObject);        
     }
 
     public override void OnBeginDrag(PointerEventData eventData) {
         EventSystem.current.SetSelectedGameObject(gameObject);        
         base.OnBeginDrag(eventData);
     }
 }
 

I couldn't find an elegant way to update ScrollRect position. Also as you see I calculate the scrolling speed myself. It can be tweaked to work better. Anyways, if you improve the code, let me know.

Comment
Add comment · Show 8 · 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 Elit3d · Jan 15, 2015 at 07:27 PM 0
Share

alright, looking forward to the updated code :)

avatar image KurtGokhan · Jan 15, 2015 at 07:49 PM 0
Share

Updated the code. Check it out.

avatar image Elit3d · Jan 15, 2015 at 10:09 PM 0
Share

cool so I'm assu$$anonymous$$g this will work for gamepad too? Also, while we are on the subject, is it possible to make it so if gamepad is plugged in, it will scroll to a certain point?

avatar image KurtGokhan · Jan 16, 2015 at 07:17 AM 0
Share

I have tested. It works with gamepad. You can set "normalizedPosition" field to scroll the ScrollRect. It should take a value between 0 and 1 if you want it to stay in frame. Input class has methods that tells if a gamepad is connected or not. You can use that to set the position and also select the rectangle when a joystick is connected.

avatar image Elit3d · Jan 16, 2015 at 12:19 PM 0
Share

I have a problem. The scrolling works fantastically thank you very much now my problem is, if I want to select a button which is further down I cant do that because I scroll to it and then my gamepad actually doesnt want to select any buttons

Show more comments

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

28 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I get (return) all the GUI inside(contained in) a GUI rect ? 2 Answers

In a ScrollRect and a vertical layout group, how do I set child alignment to the left ? 0 Answers

Can't destroy an ui element 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