Search Unity

Simple mobile touch buttons

Discussion in 'Scripting' started by Lukaszczyk, Mar 20, 2019.

  1. Lukaszczyk

    Lukaszczyk

    Joined:
    Feb 20, 2019
    Posts:
    2
    Hello All

    From 3 days I am fighting with configure my two buttons to work them on mobile phone. What I need, is simply when you press button left, then do something, when relase- stop doing this. This is my code for one of the buttons:
    Code (CSharp):
    1. public class ButtonB : MonoBehaviour
    2. {
    3.     public bool buttonB = false;
    4.  
    5.     void OnMouseDown()
    6.     {      
    7.         Debug.Log("ENGINE B PRESSED");
    8.         buttonB = true;
    9.     }
    10.  
    11.     void OnMouseUp()
    12.     {      
    13.         Debug.Log("ENGINE B UNPRESSED");
    14.         buttonB = false;
    15.     }
    It works, but I can't press both buttons at a same time. I know that I should use GetTouch, but I am trying from like a 3 days and still don't know how it work! Can anyone help me, please?

    Thanks for any ideas!
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    You solved your issue with saying you need to take touch. Mouse Button is there only once afaik. You can script yourself a fakeTouch that gets generated on mouseclick and release all at the last mouseup, better try to use remote or a real device build.
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Multi touch works fine, but there's known bug with it and unity remote. If you're using unity remote, try to test on device without it. And I suggest you switch to using IPointerUpHandler, IPointerDownHandler interfaces for handling input events because it has a parameter wich contains extended event data.
     
    WheresMommy likes this.
  4. Lukaszczyk

    Lukaszczyk

    Joined:
    Feb 20, 2019
    Posts:
    2
    This worked for me:
    Code (CSharp):
    1. if (Input.touchCount > 0)
    2.         {
    3.             for (int i = 0; i < Input.touchCount; i++)
    4.                 if (Input.GetTouch(i).position.x < ScreenWidth / 2)
    5.                 {
    6.                 if (Input.GetTouch(i).phase == TouchPhase.Began)
    7.                 {                                    
    8.                     Debug.Log("ENGINE A PRESSED");
    9.                         buttonA = true;
    10.                 }
    11.                 else if (Input.GetTouch(i).phase == TouchPhase.Ended)
    12.                 {
    13.                     Debug.Log("ENGINE A unPRESSED");
    14.                         buttonA = false;
    15.                 }
    16.                 }          
    17.         }
    Now I am just thinking is there any way to draw circle on a touched place and remove it when touch end?