参考雨松momo 的《Unity3D研究院之IOS自定义游戏摇杆与飞机平滑的移动(十一)》一文
地址:http://www.xuanyusong.com/archives/526
文中的JoyJoystick脚本是Unity自带的比较旧的脚本,我这儿是针对Unity新的脚本进行分析;
Joystick.cs是官方提供的脚本,具体代码如下:
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace UnityStandardAssets.CrossPlatformInput
{
public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public enum AxisOption
{
// Options for which axes to use
Both, // Use both
OnlyHorizontal, // Only horizontal
OnlyVertical // Only vertical
}
public int MovementRange = 100;
public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
Vector3 m_StartPos;
bool m_UseX; // Toggle for using the x axis
bool m_UseY; // Toggle for using the Y axis
CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
void OnEnable()
{
CreateVirtualAxes();
}
void Start()
{
m_StartPos = transform.position;
}
void UpdateVirtualAxes(Vector3 value)
{
var delta = m_StartPos - value;
delta.y = -delta.y;
delta /= MovementRange;
//圆形
if (Vector3.SqrMagnitude(delta)>1)
{
delta = delta.normalized;
}
// Debug.Log(Vector3.SqrMagnitude(delta));
if (m_UseX)
{
m_HorizontalVirtualAxis.Update(-delta.x);
}
if (m_UseY)
{
m_VerticalVirtualAxis.Update(delta.y);
}
}
void CreateVirtualAxes()
{
// set axes to use
m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
// create new axes based on axes to use
if (m_UseX)
{
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
if (m_UseY)
{
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
}
}
public void OnDrag(PointerEventData data)
{
Vector3 newPos = Vector3.zero;
if (m_UseX)
{
int delta = (int)(data.position.x - m_StartPos.x);
delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
newPos.x = delta;
}
if (m_UseY)
{
int delta = (int)(data.position.y - m_StartPos.y);
delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
newPos.y = delta;
}
transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
UpdateVirtualAxes(transform.position);
}
public void OnPointerUp(PointerEventData data)
{
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
public void OnPointerDown(PointerEventData data) { }
void OnDisable()
{
// remove the joysticks from the cross platform input
if (m_UseX)
{
m_HorizontalVirtualAxis.Remove();
}
if (m_UseY)
{
m_VerticalVirtualAxis.Remove();
}
}
}
}
我这里是对原文 Main.js 的改编,我这里用的语言是C#:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.UI;
public class Main : MonoBehaviour {
public Joystick moveJoystick;
public Image plan;//飞机贴图
//飞机在屏幕中的坐标
float x = 0.0f;
float y = 0.0f;
//避免飞机飞出屏幕,分别是X,Y最大坐标 ,最小坐标是0,0
float cross_x = 0;
float cross_y = 0;
float planSpeed = 20.0f;//飞机移动速度
// Use this for initialization
void Start()
{
x = 200.0f;
y = 200.0f;
cross_x = Screen.width - plan.GetComponent<RectTransform>().rect.width * 0.5f;
cross_y = Screen.height - plan.GetComponent<RectTransform>().rect.height*0.5f;
}
// Update is called once per frame
void Update()
{
//得到游戏摇杆的反馈信息 得到是-1到1之间
//官方CrossPlatformInput核心是提出了PC和手机输入的兼容方案
float touchKey_x = CrossPlatformInputManager.GetAxis(moveJoystick.horizontalAxisName);
float touchKey_y = CrossPlatformInputManager.GetAxis(moveJoystick.verticalAxisName);
//摇杆向左
if (touchKey_x<0)
{
x -= planSpeed;
}
else if (touchKey_x>0)
{
x += planSpeed;
}
//摇杆向xia
if (touchKey_y <0)
{
y -= planSpeed;
}
else if (touchKey_y >0)
{
y+= planSpeed;
}
//防止飞机飞出屏幕,出界检测
if (x < 50)
{
x = 50;
}
else if (x > cross_x)
{
x = cross_x;
}
if (y < 150)
{
y = 150;
}
else if (y > cross_y)
{
y = cross_y;
}
plan.GetComponent<RectTransform>().position= new Vector3(x,y,0);
}
}
这里需要注意的几点是:
引用需要增加
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.UI;
另外就是获取游戏摇杆的反馈信息这儿
float touchKey_x = CrossPlatformInputManager.GetAxis(moveJoystick.horizontalAxisName);
float touchKey_y = CrossPlatformInputManager.GetAxis(moveJoystick.verticalAxisName);