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 Casss · Dec 13, 2016 at 09:03 AM · c#inputtouchconvertmouseclick

How to convert Touch Input To Mouse Input C# unity?

Help me CONVERT Touch Input To Mouse Input.Please

using UnityEngine;

 public class InputManager : MonoBehaviour
 {
 
     private Vector2 startPos;
     private Vector2 endPos;
     private InputType touchInput = InputType.IDLE;
 
     /// <summary>
     /// TouchListener: Use method in the update loop to listen for touch events.
     /// </summary>
     /// <param name="direction">out parameter for swipe direction</param>
     /// <param name="touchPoint">out parameter for initial point</param>
     /// <param name="input">out parameter for kind of touch</param>
     /// <returns>boolean for whether a valid touch event was fired.</returns>
     public bool TouchListener(out TouchCommand command)
     {
         bool output = false;
 
         if (Input.touchCount > 0)
         {
             Touch t = Input.GetTouch(0);
 
             switch (t.phase)
             {
                 case TouchPhase.Began:
                     {
                         startPos = t.position;
                         break;
                     }
                 case TouchPhase.Ended:
                     {
                         endPos = t.position;
                         Vector2 lengthOfSwipe = endPos - startPos;
                         if (lengthOfSwipe.magnitude > 40)
                         {
                             touchInput = InputType.SWIPE;
                         }
                         else
                         {
                             touchInput = InputType.TOUCH;
                         }
                         break;
                     }
             }
         }
 
         switch (touchInput)
         {
             default:
             case InputType.IDLE:
                 {
                     command = new TouchCommand(Direction.INVALID, Vector2.zero, InputType.IDLE);
                     output = false;
                     break;
                 }
             case InputType.SWIPE:
                 {
                     command = new TouchCommand(DirectionFromVector(endPos - startPos), startPos, InputType.SWIPE);
                     output = true;
                     break;
                 }
             case InputType.TOUCH:
                 {
                     command = new TouchCommand(Direction.INVALID, startPos, InputType.TOUCH);
                     output = true;
                     break;
                 }
         }
         touchInput = InputType.IDLE;
         return output;
     }
 
     /// <summary>
     /// Converts Vector2 into direction for raycast
     /// </summary>
     /// <param name="direction"></param>
     /// <returns></returns>
     private Direction DirectionFromVector(Vector2 direction)
     {
         Direction d = Direction.INVALID;
         if (direction.x > 0 && direction.y > 0)
         {
             d = Direction.FORWARD;
         }
         if (direction.x < 0 && direction.y < 0)
         {
             d = Direction.BACKWARD;
         }
         if (direction.x > 0 && direction.y < 0)
         {
             d = Direction.RIGHT;
         }
         if (direction.x < 0 && direction.y > 0)
         {
             d = Direction.LEFT;
         }
         return d;
     }
 }
 
 /// <summary>
 /// Encapsulates the touch behavior into a command object that is passed into the character classes
 /// </summary>
 public class TouchCommand
 {
     public Direction direction
     {
         get; set;
     }
     public Vector2 touchPoint
     {
         get; set;
     }
     public InputType inputType
     {
         get; set;
     }
 
     /// <summary>
     /// Create the touch command you desire with this constructor.
     /// </summary>
     /// <param name="direction"></param>
     /// <param name="touchPoint"></param>
     /// <param name="touchInput"></param>
     public TouchCommand(Direction direction, Vector2 touchPoint, InputType touchInput)
     {
         this.direction = direction;
         this.touchPoint = touchPoint;
         this.inputType = touchInput;
     }
 }


Comment
Add comment · Show 1
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 saud_ahmed020 · Dec 13, 2016 at 10:47 AM 0
Share

@Casss Just understand this and convert yourself. Its very easy. You can do it ! https://docs.unity3d.com/ScriptReference/Input.Get$$anonymous$$ouseButtonDown.html?_ga=1.267810894.1934806468.1481349293

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Casss · Dec 13, 2016 at 05:50 PM

I converted using UnityEngine;

 public class InputManager : MonoBehaviour
 {
 
     private Vector2 startPos;
     private Vector2 endPos;
     private InputType touchInput = InputType.IDLE;
 
     /// <summary>
     /// TouchListener: Use method in the update loop to listen for touch events.
     /// </summary>
     /// <param name="direction">out parameter for swipe direction</param>
     /// <param name="touchPoint">out parameter for initial point</param>
     /// <param name="input">out parameter for kind of touch</param>
     /// <returns>boolean for whether a valid touch event was fired.</returns>
     public bool TouchListener(out TouchCommand command)
     {
         bool output = false;
 
       
             if(Input.GetMouseButtonDown(0))
             {
                 //save began touch 2d point
                 startPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
             }
         if(Input.GetMouseButtonUp(0))
         {
             //save ended touch 2d point
             endPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
                         
                         Vector2 lengthOfSwipe = endPos - startPos;
                         if (lengthOfSwipe.magnitude > 40)
                         {
                             touchInput = InputType.SWIPE;
                         }
                         else
                         {
                             touchInput = InputType.TOUCH;
                         }
                        
                     }
        
 
         switch (touchInput)
         {
             default:
             case InputType.IDLE:
                 {
                     command = new TouchCommand(Direction.INVALID, Vector2.zero, InputType.IDLE);
                     output = false;
                     break;
                 }
             case InputType.SWIPE:
                 {
                     command = new TouchCommand(DirectionFromVector(endPos - startPos), startPos, InputType.SWIPE);
                     output = true;
                     break;
                 }
             case InputType.TOUCH:
                 {
                     command = new TouchCommand(Direction.INVALID, startPos, InputType.TOUCH);
                     output = true;
                     break;
                 }
         }
         touchInput = InputType.IDLE;
         return output;
     }
 
     /// <summary>
     /// Converts Vector2 into direction for raycast
     /// </summary>
     /// <param name="direction"></param>
     /// <returns></returns>
     private Direction DirectionFromVector(Vector2 direction)
     {
         Direction d = Direction.INVALID;
         if (direction.x > 0 && direction.y > 0)
         {
             d = Direction.FORWARD;
         }
         if (direction.x < 0 && direction.y < 0)
         {
             d = Direction.BACKWARD;
         }
         if (direction.x > 0 && direction.y < 0)
         {
             d = Direction.RIGHT;
         }
         if (direction.x < 0 && direction.y > 0)
         {
             d = Direction.LEFT;
         }
         return d;
     }
 }
 
 /// <summary>
 /// Encapsulates the touch behavior into a command object that is passed into the character classes
 /// </summary>
 public class TouchCommand
 {
     public Direction direction
     {
         get; set;
     }
     public Vector2 touchPoint
     {
         get; set;
     }
     public InputType inputType
     {
         get; set;
     }
 
     /// <summary>
     /// Create the touch command you desire with this constructor.
     /// </summary>
     /// <param name="direction"></param>
     /// <param name="touchPoint"></param>
     /// <param name="touchInput"></param>
     public TouchCommand(Direction direction, Vector2 touchPoint, InputType touchInput)
     {
         this.direction = direction;
         this.touchPoint = touchPoint;
         this.inputType = touchInput;
     }
 }
 
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert Touch input to Mouse input C#? 1 Answer

Multiple Cars not working 1 Answer

PointerClick multiple overlaying UI elements 0 Answers

Convert Mouse Input to Touch Input 0 Answers

OnMouseDown is checked when build for Android 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