【Unity】用NGUI实现摇杆功能
2015-03-04 16:00
901 查看
using UnityEngine;
using System.Collections;
public class Joystick : MonoBehaviour
{
private bool Ispress = false;
private Transform button;
public static float h = 0;
public static float v = 0;
void Awake()
{
button = transform.Find("Button");
}
void OnPress(bool isPress) //按下触发函数 isPress就为真
{
this.Ispress = isPress;
if (Ispress == false)
{
button.localPosition = Vector3.zero;
h = 0; //鼠标抬起归零
v = 0; //鼠标抬起归零
}
}
void Update()
{
if (this.Ispress) //当触发的时候
{
//print(UICamera.lastTouchPosition);
Vector2 touchPos = UICamera.lastTouchPosition; //记录上一次的触摸位置
//print(touchPos);
touchPos -= new Vector2(91, 91); //让按钮的原点定于中心店
float distance = Vector2.Distance(Vector2.zero, touchPos); //上次触摸坐标和原点的距离
if (distance > 73) //如果大于的话,就让BUTTON的本地坐标等于上次触发的坐标TOUCHPOS
{
touchPos = touchPos.normalized * 73;
button.localPosition = touchPos;
}
else
{
button.localPosition = touchPos;
// print(touchPos);
}
h = touchPos.x / 73;
v = touchPos.y / 73;
}
// print("h的坐标为:"+h+" v的坐标为:"+v);
}
}
using System.Collections;
public class Joystick : MonoBehaviour
{
private bool Ispress = false;
private Transform button;
public static float h = 0;
public static float v = 0;
void Awake()
{
button = transform.Find("Button");
}
void OnPress(bool isPress) //按下触发函数 isPress就为真
{
this.Ispress = isPress;
if (Ispress == false)
{
button.localPosition = Vector3.zero;
h = 0; //鼠标抬起归零
v = 0; //鼠标抬起归零
}
}
void Update()
{
if (this.Ispress) //当触发的时候
{
//print(UICamera.lastTouchPosition);
Vector2 touchPos = UICamera.lastTouchPosition; //记录上一次的触摸位置
//print(touchPos);
touchPos -= new Vector2(91, 91); //让按钮的原点定于中心店
float distance = Vector2.Distance(Vector2.zero, touchPos); //上次触摸坐标和原点的距离
if (distance > 73) //如果大于的话,就让BUTTON的本地坐标等于上次触发的坐标TOUCHPOS
{
touchPos = touchPos.normalized * 73;
button.localPosition = touchPos;
}
else
{
button.localPosition = touchPos;
// print(touchPos);
}
h = touchPos.x / 73;
v = touchPos.y / 73;
}
// print("h的坐标为:"+h+" v的坐标为:"+v);
}
}
相关文章推荐
- Unity中实现摇杆功能
- [置顶] Unity基于NGUI实现拖拽功能
- Unity Action Game Demo(2)NGUI实现摇杆
- unity-使用NGUI 两个Button实现左右切换功能
- 【Unity&Shader】NGUI实现图集变灰功能
- unity技术之NGUI的功能实现(2)
- Unity手游开发--NGUI实现冷却技能(CD)功能
- 【Unity】代码实现NGUI按钮监听功能
- unity技术之NGUI的功能实现(1)
- Unity,NGUI实现将一排按钮分组,实现选择功能
- Unity3D 虚拟摇杆 NGUI实现方法
- 【unity】ngui事件系统实现参考
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- Unity中使用协程实现倒计时功能
- NGUI实现一个背包功能
- unity项目实现“再按一次退出程序”提示功能
- 【Unity快速实现小功能】NGUI篇——实现点击有链接的文本
- Unity-基于NGUI下的序列帧动画代码实现
- [Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能
- Unity结合Flask实现排行榜功能