Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Two Buttons sharing a function

Discussion in 'UGUI & TextMesh Pro' started by NavpointSoftware, Nov 24, 2018.

  1. NavpointSoftware

    NavpointSoftware

    Joined:
    Apr 30, 2011
    Posts:
    102
    Hi All,

    I’m pretty sure this is a simple solution just I can’t see it.

    I’ve attached an image showing my problem.

    But basically, I have a single .png image with alpha and with both areas I want to act as buttons within.

    The problem is the button function stretches across the middle ‘deadzone’ so if the user accidentally clicks in the middle it will select those 2 areas.

    If I was to split the single .png into 2 images I could have 2 separate buttons.

    What I want is that the user could then press either button and BOTH buttons would for example change colour together.

    How would I go about doing this?

    Ive also adjusted my other image parts to layer correctly in the hierarchy and though this helps is still not satisfactorily.

    Kind Regards,

    John
     

    Attached Files:

  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    This is tricky.
    As far as I know, the UGUI doesn't support multi touch by default - but even if: the functions would be called separately.
    So I guess the best solution is to mimic the button behavior but doing all the input validations by hand.
     
  3. NavpointSoftware

    NavpointSoftware

    Joined:
    Apr 30, 2011
    Posts:
    102
    I Managed a work around -

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class ButtonPairs : MonoBehaviour {
    6.  
    7.     [SerializeField] GameObject gameController;
    8.     [SerializeField] Button  btnFirst;
    9.     [SerializeField] Button btnSecond;
    10.  
    11.     bool btnFirstPressed = false;
    12.     bool btnSecondPressed = false;
    13.  
    14.     public void ButtonFirstPressed()
    15.     {
    16.  
    17.         btnFirstPressed = true;
    18.  
    19.         gameController.GetComponent<GameInterface>().InstrumentButtonClicked();
    20.  
    21.         StartCoroutine(WhichButtonPressed());
    22.  
    23.     }
    24.  
    25.     public void ButtonSecondPressed()
    26.     {
    27.         btnSecondPressed = true;
    28.  
    29.         gameController.GetComponent<GameInterface>().InstrumentButtonClicked();
    30.  
    31.        StartCoroutine(WhichButtonPressed());
    32.      
    33.     }
    34.  
    35.  
    36.     public IEnumerator WhichButtonPressed()
    37.     {
    38.        
    39.         yield return new WaitForSeconds(0.02f);
    40.  
    41.         if (btnSecondPressed == true)
    42.         {
    43.             if (btnSecond.image.color == Color.green)
    44.             {
    45.  
    46.                 btnFirst.interactable = false;
    47.                 btnFirst.GetComponent<Image>().color = Color.green;
    48.             }
    49.             else if (btnSecond.image.color == Color.red)
    50.             {
    51.                 btnFirst.interactable = false;
    52.                 btnFirst.GetComponent<Image>().color = Color.red;
    53.             }
    54.             else if (btnSecond.image.color == Color.yellow)
    55.             {
    56.                 btnFirst.interactable = false;
    57.                 btnFirst.GetComponent<Image>().color = Color.yellow;
    58.             }
    59.         }
    60.         else if (btnFirstPressed == true)
    61.         {
    62.             if (btnFirst.image.color == Color.green)
    63.             {
    64.  
    65.                 btnSecond.interactable = false;
    66.                 btnSecond.GetComponent<Image>().color = Color.green;
    67.             }
    68.             else if (btnFirst.image.color == Color.red)
    69.             {
    70.                 btnSecond.interactable = false;
    71.                 btnSecond.GetComponent<Image>().color = Color.red;
    72.             }
    73.             else if (btnFirst.image.color == Color.yellow)
    74.             {
    75.                 btnSecond.interactable = false;
    76.                 btnSecond.GetComponent<Image>().color = Color.yellow;
    77.             }
    78.         }
    79.     }
    80.  
    81.  
    82.  
    83.  
    84. }
    85.  
    Im new to programming so if there is any ways I can optimise or improve this code let me know :)

    Kind Regards,
    John