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

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.

#pragma strict
import UnityEngine;
import UnityEngine.EventSystems;
import UnityStandardAssets.CrossPlatformInput;
public class Joystick extends MonoBehaviour
{
     var MovementRange : int = 100;
    
     enum AxisOption
     {                                        // Options for which axes to use                                                    
         Both,                                // Use both
         OnlyHorizontal,                        // Only horizontal
         OnlyVertical                        // Only vertical
     }
     var axesToUse : AxisOption = AxisOption.Both;        // The options for the axes that the still will use
     var horizontalAxisName : String = "Horizontal";        // The name given to the horizontal axis for the cross platform input
     var verticalAxisName : String = "Vertical";            // The name given to the vertical axis for the cross platform input
    
     private var startPos : Vector3;
     private var useX : boolean;                                                            // Toggle for using the x axis
     private var useY : boolean;                                                            // Toggle for using the Y axis
     private var horizontalVirtualAxis : CrossPlatformInputManager.VirtualAxis;            // Reference to the joystick in the cross platform input
     private var verticalVirtualAxis : CrossPlatformInputManager.VirtualAxis;            // Reference to the joystick in the cross platform input
    
     //              ----------------------------
     function Start () {
        
         startPos = transform.position;
         CreateVirtualAxes ();
     }
     //              ----------------------------
     function UpdateVirtualAxes (value: Vector3)
     {
         var delta = startPos - value;
         delta.y = -delta.y;
         delta /= MovementRange;
         if(useX)
             horizontalVirtualAxis.Update (-delta.x);
        
         if(useY)
             verticalVirtualAxis.Update (delta.y);
        
     }
     //              ----------------------------
     function CreateVirtualAxes()
     {
     // set axes to use
         useX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
         useY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
            
     // create new axes based on axes to use
         if (useX)
             horizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
         if (useY)
             verticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
     }
     //              ----------------------------
     function OnDrag(data: PointerEventData)
     {
         var newPos : Vector3 = Vector3.zero;
        
         if (useX) {
             var deltaX : float = (data.position.x - startPos.x);
             newPos.x = deltaX;
         }
        
         if (useY)
         {
             var deltaY : float = (data.position.y - startPos.y);
             newPos.y = deltaY;
         }
         transform.position = Vector3.ClampMagnitude( new Vector3(newPos.x , newPos.y , newPos.z), MovementRange) + startPos;
         UpdateVirtualAxes (transform.position);
     }
     //              ----------------------------
     function OnPointerUp(data: PointerEventData)
     {
         transform.position = startPos;
         UpdateVirtualAxes (startPos);
     }
     //              ----------------------------
     function OnPointerDown (data: PointerEventData)
     {
    
     }
     //              ----------------------------
     function OnDisable () {
     // remove the joysticks from the cross platform input
         if (useX)
         {
             horizontalVirtualAxis.Remove();
         }
         if (useY)
         {
             verticalVirtualAxis.Remove();
         }
     }
}
//              ----------------------------

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.

@Kiwasi Thanks for the reply, believe me when I say I’ve been through every page of Google to find the answer … :frowning:

Turns out implementing an interface in JS as simple as

public class MyClass extends MonoBehaviour implements IPointerClickHandler {}

Some examples of JS interfaces in this page. https://unity3d.com/learn/tutorials/modules/intermediate/scripting/interfaces

@Kiwasi Thank you, Believe me I have been googling for hours and didn’t come across that, thanks.

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.

Thanks, I’ll keep that in mind.

@Kiwasi Can I ask you another question please, lines 53 and 55 seem not to be updating if I add this line

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 …

print("Horizontal value is " + -delta.x + "      Vertical value is " + delta.y);

It outputs as I’d expect -1 to 1

Done it, had to change the CreateVirtualAxes() function to check if already registered, then un-register, then make new instance …

Strange …

    function CreateVirtualAxes()
    {
    // set axes to use
        useX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
        useY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);

        if (useX)
        {
            if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
            {
                CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
            }
            horizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
            CrossPlatformInputManager.RegisterVirtualAxis(horizontalVirtualAxis);
        }
        if (useY)
        {
            if (CrossPlatformInputManager.AxisExists(verticalAxisName))
            {
                CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
            }
            verticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
            CrossPlatformInputManager.RegisterVirtualAxis(verticalVirtualAxis);
        }
    }