Search Unity

UnityEngine.EventSystems.. Trying to figure it out .js

Discussion in 'Scripting' started by Griffo, Aug 29, 2015.

  1. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Hi, I'm just starting to use the new UI GUI and are converting my scripts to use the new UI.

    I've converted a C# script off the internet to .js (form HERE) but it does not do what I'd expected .. The event classes are not being called .. I've got a EventSystem in my scene.

    The C# script works if I replace my .js with it.

    I have no errors showing in the console.

    Can someone point me in the right direction please, thanks.

    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine;
    3. import UnityEngine.EventSystems;
    4. import UnityStandardAssets.CrossPlatformInput;
    5. public class Joystick extends MonoBehaviour
    6. {
    7.      var MovementRange : int = 100;
    8.    
    9.      enum AxisOption
    10.      {                                        // Options for which axes to use                                                    
    11.          Both,                                // Use both
    12.          OnlyHorizontal,                        // Only horizontal
    13.          OnlyVertical                        // Only vertical
    14.      }
    15.      var axesToUse : AxisOption = AxisOption.Both;        // The options for the axes that the still will use
    16.      var horizontalAxisName : String = "Horizontal";        // The name given to the horizontal axis for the cross platform input
    17.      var verticalAxisName : String = "Vertical";            // The name given to the vertical axis for the cross platform input
    18.    
    19.      private var startPos : Vector3;
    20.      private var useX : boolean;                                                            // Toggle for using the x axis
    21.      private var useY : boolean;                                                            // Toggle for using the Y axis
    22.      private var horizontalVirtualAxis : CrossPlatformInputManager.VirtualAxis;            // Reference to the joystick in the cross platform input
    23.      private var verticalVirtualAxis : CrossPlatformInputManager.VirtualAxis;            // Reference to the joystick in the cross platform input
    24.    
    25.      //              ----------------------------
    26.      function Start () {
    27.        
    28.          startPos = transform.position;
    29.          CreateVirtualAxes ();
    30.      }
    31.      //              ----------------------------
    32.      function UpdateVirtualAxes (value: Vector3)
    33.      {
    34.          var delta = startPos - value;
    35.          delta.y = -delta.y;
    36.          delta /= MovementRange;
    37.          if(useX)
    38.              horizontalVirtualAxis.Update (-delta.x);
    39.        
    40.          if(useY)
    41.              verticalVirtualAxis.Update (delta.y);
    42.        
    43.      }
    44.      //              ----------------------------
    45.      function CreateVirtualAxes()
    46.      {
    47.      // set axes to use
    48.          useX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
    49.          useY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
    50.            
    51.      // create new axes based on axes to use
    52.          if (useX)
    53.              horizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
    54.          if (useY)
    55.              verticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    56.      }
    57.      //              ----------------------------
    58.      function OnDrag(data: PointerEventData)
    59.      {
    60.          var newPos : Vector3 = Vector3.zero;
    61.        
    62.          if (useX) {
    63.              var deltaX : float = (data.position.x - startPos.x);
    64.              newPos.x = deltaX;
    65.          }
    66.        
    67.          if (useY)
    68.          {
    69.              var deltaY : float = (data.position.y - startPos.y);
    70.              newPos.y = deltaY;
    71.          }
    72.          transform.position = Vector3.ClampMagnitude( new Vector3(newPos.x , newPos.y , newPos.z), MovementRange) + startPos;
    73.          UpdateVirtualAxes (transform.position);
    74.      }
    75.      //              ----------------------------
    76.      function OnPointerUp(data: PointerEventData)
    77.      {
    78.          transform.position = startPos;
    79.          UpdateVirtualAxes (startPos);
    80.      }
    81.      //              ----------------------------
    82.      function OnPointerDown (data: PointerEventData)
    83.      {
    84.    
    85.      }
    86.      //              ----------------------------
    87.      function OnDisable () {
    88.      // remove the joysticks from the cross platform input
    89.          if (useX)
    90.          {
    91.              horizontalVirtualAxis.Remove();
    92.          }
    93.          if (useY)
    94.          {
    95.              verticalVirtualAxis.Remove();
    96.          }
    97.      }
    98. }
    99. //              ----------------------------
     

    Attached Files:

  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You need to implement the EventSystem namespace interfaces for unity to pick up the new events. I'm not sure how this is done in JavaScript, but it shouldn't be hard to find.
     
  3. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    @BoredMormon Thanks for the reply, believe me when I say I've been through every page of Google to find the answer .. :(
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  5. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    @BoredMormon Thank you, Believe me I have been googling for hours and didn't come across that, thanks.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Googling JavaScript stuff is hard. Apparently it has the same name as some other popular language used for web development. It's like the cursed isle, it can only be found by those who already know where it is.

    Sometimes using UnityScript instead of JavaScript can help in searching.
     
  7. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Thanks, I'll keep that in mind.
     
  8. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    @BoredMormon Can I ask you another question please, lines 53 and 55 seem not to be updating if I add this line

    Code (JavaScript):
    1. print("Horizontal " + CrossPlatformInputManager.GetAxis("Horizontal") + "      Vertical " + CrossPlatformInputManager.GetAxis("Vertical"));
    Between 41 and 42 it outputs 0 - 0 any idea why?

    But if I add this line ..

    Code (JavaScript):
    1. print("Horizontal value is " + -delta.x + "      Vertical value is " + delta.y);
    It outputs as I'd expect -1 to 1
     
  9. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Done it, had to change the CreateVirtualAxes() function to check if already registered, then un-register, then make new instance ..

    Strange ..

    Code (JavaScript):
    1.     function CreateVirtualAxes()
    2.     {
    3.     // set axes to use
    4.         useX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
    5.         useY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
    6.  
    7.         if (useX)
    8.         {
    9.             if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
    10.             {
    11.                 CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
    12.             }
    13.             horizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
    14.             CrossPlatformInputManager.RegisterVirtualAxis(horizontalVirtualAxis);
    15.         }
    16.         if (useY)
    17.         {
    18.             if (CrossPlatformInputManager.AxisExists(verticalAxisName))
    19.             {
    20.                 CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
    21.             }
    22.             verticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    23.             CrossPlatformInputManager.RegisterVirtualAxis(verticalVirtualAxis);
    24.         }
    25.     }