Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how to press gui button at the same time??

Discussion in 'Scripting' started by jasmindelacruz, Jul 19, 2014.

  1. jasmindelacruz

    jasmindelacruz

    Joined:
    Jun 25, 2014
    Posts:
    30
    i have 2 gui buttons...and i want to press it at the same time...how???



    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6.  
    7.  
    8. public class Tap : MonoBehaviour
    9. {
    10.     public Texture2D buttonImage = null;
    11.     public GameObject objectToMove;
    12.    
    13.     private float timer;
    14.    
    15.     void Start()
    16.     {
    17.         timer = 0.0f;
    18.     }
    19.    
    20.     void Update()
    21.     {
    22.         timer += Time.deltaTime;
    23.  
    24.     }
    25.    
    26.     private  void OnGUI()
    27.     {
    28.         if (timer > 4.0f)
    29.         {
    30.             if (GUI.Button (new Rect(50,350,300,100),buttonImage))
    31.             {
    32.                 this.transform.position += new Vector3(-1.0f, 0.0f, 0.0f) ;
    33.             }
    34.             if (timer > 4.0f) {
    35.                 if (GUI.Button (new Rect (400, 350, 300, 100), buttonImage)) {
    36.                     this.transform.position += new Vector3 (1, 0, 0);
    37.                 }
    38.             }
    39.         }
    40.     }
    41. }
    42.  
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Do you mean tap two buttons at the same time on a touch device?
     
  3. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I haven't tested the following, but here is the general idea:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Tap : MonoBehaviour
    5. {
    6.     public Texture2D buttonImage = null;
    7.     public GameObject objectToMove;
    8.  
    9.     private float timer;
    10.  
    11.     private readonly Rect firstButtonPosition = new Rect(50,350,300,100);
    12.     private readonly Rect secondButtonPosition = new Rect(400, 350, 300, 100);
    13.  
    14.     void Start()
    15.     {
    16.         timer = 0.0f;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         timer += Time.deltaTime;
    22.  
    23.         int touchCount = Input.touchCount;
    24.         for (int i = 0; i < touchCount; ++i)
    25.         {
    26.             var touch = Input.GetTouch(i);
    27.             if (touch.phase == TouchPhase.Began)
    28.             {
    29.                 // Get touch point and invert Y-axis.
    30.                 var touchPoint = touch.position;
    31.                 touchPoint.y = Screen.height - touchPoint.y;
    32.  
    33.                 if (firstButtonPosition.Contains(touchPoint))
    34.                 {
    35.                     OnFirstButtonTapped();
    36.                 }
    37.                 if (secondButtonPosition.Contains(touchPoint))
    38.                 {
    39.                     OnSecondButtonTapped();
    40.                 }
    41.             }
    42.         }
    43.     }
    44.  
    45.     private void OnGUI()
    46.     {
    47.         // Here we just draw the buttons without checking for touch input!
    48.         if (timer > 4.0f)
    49.         {
    50.             GUI.Button (firstButtonPosition, buttonImage);
    51.             if (timer > 4.0f)
    52.             {
    53.                 GUI.Button (secondButtonPosition, buttonImage);
    54.             }
    55.         }
    56.     }
    57.  
    58.     private void OnFirstButtonTapped()
    59.     {
    60.         // Tapped first button!
    61.         this.transform.position += new Vector3(-1.0f, 0.0f, 0.0f) ;
    62.     }
    63.  
    64.     private void OnSecondButtonTapped()
    65.     {
    66.         // Tapped second button!
    67.         this.transform.position += new Vector3 (1, 0, 0);
    68.     }
    69. }
     
    Last edited: Jul 19, 2014
  4. jasmindelacruz

    jasmindelacruz

    Joined:
    Jun 25, 2014
    Posts:
    30

    yes :)
     
  5. jasmindelacruz

    jasmindelacruz

    Joined:
    Jun 25, 2014
    Posts:
    30
    Assets/Script/Tap.cs(27,55): error CS0117: `UnityEngine.TouchPhase' does not contain a definition for `Begin'
     
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953