Unity实现简单虚拟摇杆

所属分类: 软件编程 / C#教程 阅读数: 129
收藏 0 赞 0 分享

最近一直在倒腾用UGUI做虚拟摇杆,网上普遍的的做法就是使用以下的代码,但是这个有些注意事项,第一点就是Canvas的Render Mode必须是Screen Space Overlay,第二点就是挂载这个脚本的锚点的x,y必须是0.5,如图下:

using UnityEngine;
using UnityEngine.EventSystems;
public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler
{
 Transform point;
 Vector3 startPos;//开始位置
 Vector3 dir;//方向
 float radius = 0;//需要移动的半径
 void Start()
 {
 point = transform.GetChild(0);
 radius = (transform as RectTransform).sizeDelta.x * 0.5f;
 startPos = point.position;
 }
 public void OnDrag(PointerEventData eventData)
 {
 point.position = eventData.position;
 dir = (point.position - startPos).normalized;
 if (Vector3.SqrMagnitude(point.position - startPos) > radius * radius)
 point.position = startPos + dir * radius;
 }
 public void OnEndDrag(PointerEventData eventData)
 {
 point.localPosition = Vector3.zero;
 }
}

如果Canvas的Render Mode是Screen Space Camera,这样的话上面的代码是不能满足要求的,花了一点时间才发现是这个原因,导致上面的代码不适用的,最后把代码重写了一下,终于可以成功了!

public class JoyStick : MonoBehaviour, IDragEvent
{
 private Canvas canvas;
 private RectTransform rectTransform;//坐标
 private static Quaternion amendAngle;
 private static float mRadius = 0,v=0, h=0;
 private static Transform point;
 private static Vector3 initPos;
 private static Vector2 startPos;
 private void Start()
 {
 point = transform.GetChild(0);
 canvas = GameObject.Find("UIRoot").GetComponent<Canvas>();
 rectTransform = transform as RectTransform; //也可以写成this.GetComponent<RectTransform>(),但是不建议;
 mRadius = (transform as RectTransform).sizeDelta.x * 0.5f;
 initPos = point.localPosition;
 h = v = 0;
 }
 
 public void OnBeginDrag(PointerEventData eventData)
 {
 RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, canvas.worldCamera, out startPos);
 startPos = eventData.position - startPos;
 h = v = 0; 
 }
 
 public void OnDrag(PointerEventData eventData)
 {
 point.localPosition = eventData.position - startPos;
 Vector3 dir = (point.localPosition - initPos).normalized;
 v = dir.normalized.x; h = dir.normalized.y;
 if (Vector3.SqrMagnitude(point.localPosition - initPos) > mRadius * mRadius)
 point.localPosition = initPos + dir * mRadius;
 }
 
 public void OnEndDrag(PointerEventData eventData)
 {
 point.localPosition = Vector3.zero;
 h = v = 0; 
 }
}

RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, canvas.worldCamera, out startPos)这个的startPos返回的是点击屏幕的坐标,rectTransform是这个脚本挂载物体上的RectTransform的组件,然后减去eventData.position就知道坐标的偏移值了,看一下代码应该都可以了解意思,这里就不过多的解释了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多