Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 unity_F84B6283DBCBE2CEF602 · Jan 06 at 02:18 PM · eventeventsystem

How to capture onBeginDrag() onEndDrag() events for screen,Capture UI events onDrag for screen not for object

Howdy all, I'm new here :}

I'm trying to capture onBeginDrag() onEndDrag() events from EventSystem but having trouble I found this script, I've attached it on MainCamera but nothing happns. I'm trying to capture these events when user swipes screen on mobile. Here's the script code:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 using System;
 using UnityEngine.Events;
  
 /// <summary>
 /// Container class for Serializable Events in UnityEditor.
 /// </summary>
 public static class Events {
  
         // Additional event-definitions...
  
     [Serializable]
     public class Vector2 : UnityEvent<UnityEngine.Vector2> { }
 }
  
 public class Swipe : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler {
  
     Vector2 _lastPosition = Vector2.zero;
  
     public Events.Vector2 OnSwipeStart = new Events.Vector2();
     public Events.Vector2 OnSwipeEnd = new Events.Vector2();
     public Events.Vector2 OnSwipe = new Events.Vector2();
  
     public void OnBeginDrag(PointerEventData eventData) {
         _lastPosition = eventData.position;
         OnSwipeStart.Invoke(eventData.position);
     }
  
     public void OnEndDrag(PointerEventData eventData) {
         OnSwipeEnd.Invoke(eventData.position);
     }
  
     public void OnDrag(PointerEventData eventData) {
         Vector2 direction = eventData.position - _lastPosition;
         _lastPosition = eventData.position;
  
         OnSwipe.Invoke(direction);
     }
 }

 



,Howdy everybody,

New here :} I'm having trouble captureing onBeginDrag() onEndDrag() UI events for screen. I wan't to implement some logic when user swipes on mobile screen but it doesn't seem to work, I guess I'm not attaching script on right object, here the code:

I attached this script to MainCamera, but nothing happens :/

 using System.Diagnostics;
 using System;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.Events;
 
 public class Swipe : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler {
  
     Vector2 _lastPosition = Vector2.zero;
     
     public UnityEvent OnSwipeStart;
     public UnityEvent OnSwipeEnd;
     public UnityEvent OnSwipe;
 
     private void Start() {
         if (OnSwipeStart != null) {
             //OnSwipeStart.AddListener(Ping);
         }
     }
  
     public void OnBeginDrag(PointerEventData eventData) {
         _lastPosition = eventData.position;
         UnityEngine.Debug.Log("OnBeginDrag " + eventData.position);
         OnSwipeStart.Invoke();
     }
  
     public void OnEndDrag(PointerEventData eventData) {
         UnityEngine.Debug.Log("OnEndDrag " + eventData.position);
         OnSwipeEnd.Invoke();
     }
  
     public void OnDrag(PointerEventData eventData) {
         
         Vector2 direction = eventData.position - _lastPosition;
         UnityEngine.Debug.Log("OnDrag " + direction);
         _lastPosition = eventData.position;
  
         OnSwipe.Invoke();
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

135 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

Related Questions

How to drag gameobjects (other than ui elements) using the event trigger? 2 Answers

Can't fire a Custom Event on UI Button Touch OnClick 1 Answer

Adding custom BaseEventData to UnityEngine.EventSystems NOT Unity.Events 1 Answer

passing function to class 1 Answer

Does Event fire only once during Update()? 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