Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

scrollRect.OnDrag when the script is not in that object

Discussion in 'Editor & General Support' started by jjgarcianorway, Mar 24, 2017.

  1. jjgarcianorway

    jjgarcianorway

    Joined:
    Oct 19, 2015
    Posts:
    33
    Hello all,

    I think I'm going as something very basic but as a newbie, I do not know the answer (but I search for it!):

    I have an script in a empty object.

    It has some public variables that I assigned to objects, one is "scrollView" which is a scrollRect.

    Everything works very well but I would like to know how detect when the user is dragging scrollView.

    I know that it needs to be something related to onDrag but, all the examples I found works with the script attached to that object.

    Can I do it from my script?

    Thanks a lot.

    Best regards,


    Juanjo
     
  2. MrCool92

    MrCool92

    Joined:
    Jul 13, 2015
    Posts:
    15
    You can attach IDragHandler interface within UnityEngine.EventSystems to detect drag event.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class DragHandler : MonoBehaviour, IDragHandler
    5. {
    6.     public void OnDrag(PointerEventData eventData)
    7.     {
    8.         //...
    9.     }
    10. }
    https://docs.unity3d.com/ScriptReference/EventSystems.IBeginDragHandler.html
     
  3. jjgarcianorway

    jjgarcianorway

    Joined:
    Oct 19, 2015
    Posts:
    33
    Thanks for your quick reply but I think I'm too newbie to understand...

    my script is named mainMenu there I have my variable:

    Code (CSharp):
    1. public scrollRect scrollView; // (linked with the editor)
    can I do something like scrollView.onDrag?

    I've tried to have mainMenu : Monobehaviour, IDragHandler... but it gives me error XD

    I feel asking something very basic XD
     
    Last edited: Mar 24, 2017
  4. MrCool92

    MrCool92

    Joined:
    Jul 13, 2015
    Posts:
    15
    It gives you error probably because you are missing namespace UnityEngine.EventSystems or OnDrag function.

    You can override ScrollRect component and replace it with existing one, but that's a different approach.

    Code (CSharp):
    1. //ScrollRectExtended.cs
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. public class ScrollRectExtended : ScrollRect
    7. {
    8.     public override void OnDrag(PointerEventData eventData)
    9.     {
    10.         base.OnDrag(eventData);
    11.         //...
    12.     }
    13. }
    I would recommend you to use EventTrigger component. Just place it on same gameObject as ScrollRect and call your function from mainMenu. Once you understand how EventTrigger works (https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html) you can switch to using interfaces instead.
     
    NoizeDaemon likes this.
  5. jjgarcianorway

    jjgarcianorway

    Joined:
    Oct 19, 2015
    Posts:
    33
    Mmmm I think the EventTrigger sounds like a good solution :) But I need to learn how to use it from my script.

    I've tried the other solution you gave me and the error was:

    `mainMenu' does not implement interface member `UnityEngine.EventSystems.IDragHandler.OnDrag(UnityEngine.EventSystems.PointerEventData)'

    Probably because I@m coming from Corona and Lua but this sounds like Chinese to me XD

    My current script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.IO;
    6. using DG.Tweening;
    7. using UnityEngine.EventSystems;
    8. using UnityEngine.SceneManagement;
    9.  
    10. public class mainMenu : MonoBehaviour {
    11.  
    12.     public ScrollRect scrollView;
    13.  
    14. void Start()
    15. {
    16. }
    17.  
    18. }
    Where and how should I do the Trigger? or the onDrag? or both (just with learning purposes :)).

    And... thanks a lot for your patience! Much appreciated!
     
  6. MrCool92

    MrCool92

    Joined:
    Jul 13, 2015
    Posts:
    15
    It's an component.



    Attach it next to ScrollRect and add drag event type.

    and... capitalize your class names!
    mainMenu > MainMenu
     
    ChiliGuaya likes this.
  7. jjgarcianorway

    jjgarcianorway

    Joined:
    Oct 19, 2015
    Posts:
    33
    Hello sir,

    I tested and it works! Like a charm!

    Super thanks!

    And I also renamed my class :) Always learning new things!!!

    Super thanks again!