摇杆代码(委托+事件)
来源:互联网 发布:微博刷转发软件 编辑:程序博客网 时间:2024/05/17 09:32
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class JoyStick : MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IDragHandler
{
public delegate void JoyStickBegin(Vector2 pos);
public delegate void JoyStickDrag(Vector2 pos);
public delegate void JoyStickEnd();
public event JoyStickBegin OnBeginHandler;
public event JoyStickDrag OnDragHandler;
public event JoyStickEnd OnEndHandler;
public float radius = 50f;
public float resetspeed = 5f;
public bool isTouched = false;
private RectTransform mTran;
private Vector2 startPos;
private Vector2 touchedAxis;
private Vector3 worldPosition;
public Vector2 TouchedAxis
{
get{
if (touchedAxis.magnitude < radius)
{
return touchedAxis.normalized / radius;
}
return touchedAxis.normalized;
}
}
void Start() //获取摇杆初始位置
{
mTran = this.GetComponent<RectTransform> ();
startPos = mTran.anchoredPosition;
}
public void OnPointerDown(PointerEventData data)
{
isTouched = true;
touchedAxis = GetJoyStickAxis (data);
if (this.OnBeginHandler != null)
{
this.OnBeginHandler (TouchedAxis);
}
}
public void OnPointerUp(PointerEventData data)
{
isTouched = false;
mTran.anchoredPosition = startPos;
touchedAxis = Vector2.zero;
if (this.OnEndHandler != null)
{
this.OnEndHandler ();
}
}
public void OnDrag (PointerEventData data)
{
touchedAxis = GetJoyStickAxis (data);
if (this.OnDragHandler != null)
{
this.OnDragHandler (TouchedAxis);
}
}
void Update()
{
if (isTouched && touchedAxis.magnitude >= radius)
{
if (this.OnDragHandler != null)
{
this.OnDragHandler (TouchedAxis);
}
}
if (mTran.anchoredPosition.magnitude > startPos.magnitude)
{
mTran.anchoredPosition -= TouchedAxis * Time.deltaTime * resetspeed;
}
}
private Vector2 GetJoyStickAxis(PointerEventData data)
{ //获取手指位置的世界坐标
if (RectTransformUtility.ScreenPointToWorldPointInRectangle (mTran, data.position, data.pressEventCamera, out worldPosition))
mTran.position = worldPosition; //获取摇杆的偏移量
Vector2 touchAxis = mTran.anchoredPosition-startPos; //摇杆偏移量限制
if(touchAxis.magnitude >= radius)
{
touchAxis = touchAxis.normalized * radius;
mTran.anchoredPosition = touchAxis;
}
return touchAxis;
}
}
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class JoyStick : MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IDragHandler
{
public delegate void JoyStickBegin(Vector2 pos);
public delegate void JoyStickDrag(Vector2 pos);
public delegate void JoyStickEnd();
public event JoyStickBegin OnBeginHandler;
public event JoyStickDrag OnDragHandler;
public event JoyStickEnd OnEndHandler;
public float radius = 50f;
public float resetspeed = 5f;
public bool isTouched = false;
private RectTransform mTran;
private Vector2 startPos;
private Vector2 touchedAxis;
private Vector3 worldPosition;
public Vector2 TouchedAxis
{
get{
if (touchedAxis.magnitude < radius)
{
return touchedAxis.normalized / radius;
}
return touchedAxis.normalized;
}
}
void Start() //获取摇杆初始位置
{
mTran = this.GetComponent<RectTransform> ();
startPos = mTran.anchoredPosition;
}
public void OnPointerDown(PointerEventData data)
{
isTouched = true;
touchedAxis = GetJoyStickAxis (data);
if (this.OnBeginHandler != null)
{
this.OnBeginHandler (TouchedAxis);
}
}
public void OnPointerUp(PointerEventData data)
{
isTouched = false;
mTran.anchoredPosition = startPos;
touchedAxis = Vector2.zero;
if (this.OnEndHandler != null)
{
this.OnEndHandler ();
}
}
public void OnDrag (PointerEventData data)
{
touchedAxis = GetJoyStickAxis (data);
if (this.OnDragHandler != null)
{
this.OnDragHandler (TouchedAxis);
}
}
void Update()
{
if (isTouched && touchedAxis.magnitude >= radius)
{
if (this.OnDragHandler != null)
{
this.OnDragHandler (TouchedAxis);
}
}
if (mTran.anchoredPosition.magnitude > startPos.magnitude)
{
mTran.anchoredPosition -= TouchedAxis * Time.deltaTime * resetspeed;
}
}
private Vector2 GetJoyStickAxis(PointerEventData data)
{ //获取手指位置的世界坐标
if (RectTransformUtility.ScreenPointToWorldPointInRectangle (mTran, data.position, data.pressEventCamera, out worldPosition))
mTran.position = worldPosition; //获取摇杆的偏移量
Vector2 touchAxis = mTran.anchoredPosition-startPos; //摇杆偏移量限制
if(touchAxis.magnitude >= radius)
{
touchAxis = touchAxis.normalized * radius;
mTran.anchoredPosition = touchAxis;
}
return touchAxis;
}
}
0 0
- 摇杆代码(委托+事件)
- 委托 事件 代码理解
- 委托与事件代码详解
- jQuery代码优化:事件委托
- 委托事件与托管代码
- JoystickEvent 摇杆事件
- C# 的委托事件实现(含代码)
- 【unity 代码升华篇】委托、事件全解析(一)
- 【unity 代码升华篇】委托、事件全解析(一)
- 【unity 代码升华篇】委托、事件全解析(一)
- 委托事件(深入)
- unity 事件(委托)
- unity 事件(委托)
- 观察者 (事件委托)
- 委托与事件(一)什么是委托
- 委托和事件(二)--委托详解
- 关于委托和事件的形象代码
- jQuery代码优化:事件委托篇
- Java中数组的扩容
- C++ “error LNK1169: 找到一个或多个多重定义的符号” 解决方法
- 第十六条:复合优先于继承
- 美丽的图片
- NDS(导航数据标准)简介
- 摇杆代码(委托+事件)
- Python中__init__方法注意点
- LNMP安装了哪些软件?安装目录在哪?
- Openwrt使用mtd和sysupgrade刷机、备份恢复系统配置
- Java线程池使用说明
- 基线化是什么意思?
- c++ string转int,int转string
- AngularJs 多语言的使用 angular-translate
- LNMP 1.2缓存加速类扩展(xcache/Redis/memcached/eAccelerator)、imageMagick、ionCube安装教程