Search Unity

Input WebGL

Discussion in 'Scripting' started by Sphelps, Oct 9, 2015.

  1. Sphelps

    Sphelps

    Joined:
    Jun 9, 2013
    Posts:
    243
    It works on everything else but when I make it into WebGL build click does not work

    Any help would be nice Thanks

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class InputManager : MonoBehaviour
    6. {
    7.     public static event Action<Vector2> OnTouchDownEvent;
    8.     public static event Action<Vector2> OnTouchUpEvent;
    9.  
    10.     public static event Action<Vector2> OnMouseDownEvent;
    11.     public static event Action<Vector2> OnMouseUpEvent;
    12.  
    13.     public static event Action OnBackButtonPressedEvent;
    14.  
    15.     void Update()
    16.     {
    17.         #if (UNITY_ANDROID || UNITY_IOS || UNITY_WP8) && !UNITY_EDITOR
    18.         foreach(Touch t in Input.touches)
    19.         {
    20.             switch(t.phase)
    21.             {
    22.             case TouchPhase.Began :
    23.                 if(OnTouchDownEvent != null)
    24.                 {
    25.                     OnTouchDownEvent(t.position);
    26.                 }
    27.                 break;
    28.             case TouchPhase.Stationary :
    29.                 break;
    30.  
    31.             case TouchPhase.Canceled :
    32.             case TouchPhase.Ended :
    33.                 if(OnTouchUpEvent != null)
    34.                 {
    35.                     OnTouchUpEvent(t.position);
    36.                 }
    37.                 break;
    38.             }
    39.         }
    40.         #endif
    41.  
    42.         if(Input.GetKeyDown(KeyCode.Escape))
    43.         {
    44.             if(OnBackButtonPressedEvent != null)
    45.             {
    46.                 OnBackButtonPressedEvent();
    47.             }
    48.         }
    49.     }
    50.  
    51.     void OnGUI()
    52.     {
    53.         #if UNITY_EDITOR || UNITY_METRO || UNITY_STANDALONE || UNITY_WEBPLAYER
    54.         if(Event.current.type == EventType.MouseDown)
    55.         {
    56.             if(OnMouseDownEvent != null)
    57.             {
    58.                 OnMouseDownEvent(Input.mousePosition);
    59.             }
    60.         }
    61.         if(Event.current.type == EventType.MouseUp)
    62.         {
    63.             if(OnMouseUpEvent != null)
    64.             {
    65.                 OnMouseUpEvent(Input.mousePosition);
    66.             }
    67.         }
    68.         #endif
    69.     }
    70. }
    71.  
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If we take out the defines you end up with this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class InputManager : MonoBehaviour
    6. {
    7.     public static event Action<Vector2> OnTouchDownEvent;
    8.     public static event Action<Vector2> OnTouchUpEvent;
    9.  
    10.     public static event Action<Vector2> OnMouseDownEvent;
    11.     public static event Action<Vector2> OnMouseUpEvent;
    12.  
    13.     public static event Action OnBackButtonPressedEvent;
    14.  
    15.     void Update()
    16.     {
    17.         if(Input.GetKeyDown(KeyCode.Escape))
    18.         {
    19.             if(OnBackButtonPressedEvent != null)
    20.             {
    21.                 OnBackButtonPressedEvent();
    22.             }
    23.         }
    24.     }
    25.  
    26.     void OnGUI()
    27.     {
    28.     }
    29. }
    30.  
    So as I read it only your escape key should work on WebGL. Seems to be working as written.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Also, why? There is a perfectly good EventSystem component that will do this all for you.
     
  4. Sphelps

    Sphelps

    Joined:
    Jun 9, 2013
    Posts:
    243
    This was a asset on the store the publisher has says he is going to post a update but that was a month ago
     
  5. Sphelps

    Sphelps

    Joined:
    Jun 9, 2013
    Posts:
    243
    lol nvm I go it!