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][1]) 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.
![alt text][2]
#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();
}
}
}
// ----------------------------
[1]: http://www.devination.com/2015/03/unity-touch-joysticks-canvas-ui.html
[2]: /storage/temp/53089-screen-shot-2015-08-29-at-131919.png
↧