欢迎光临
我们一直在努力

小游戏网站源码,Unity 超级简单摇杆制作

小游戏网站源码,Unity 超级简单摇杆制作实现的相关代码
1:首先建立两个Image,然后将其中一个为父物体,另一个为子物体,并且调整好大小
2:在父物体上写个JoyStick.cs脚本:

```cpp using UnityEngine; using UnityEngine.EventSystems; using System.Collections; public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler { public static float h, v; //传出hv public float maxDis; //最大距离 private recttransform childrecttrans; private Coroutine coroutine = null; void Start() { childRectTrans = transform.GetChild(0) as RectTransform; } public void OnBeginDrag(PointerEventData eventData) { if (coroutine != null) { StopCoroutine(coroutine); coroutine = null; } } public void OnDrag(PointerEventData eventData) { Vector3 outPos; if (RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transform as RectTransform, eventData.position, eventData.pressEventCamera, out outPos)) { childRectTrans.position = outPos; //限制拖拽距离 childRectTrans.anchoredposition = Vector2.ClampMagnitude(childRectTrans.anchoredPosition, maxDis); //或者利用子物体和父物体的距离判断是否超过最大距离,当距离大于等于最大的距离时候, //计算父物体和子物体的向量,然后利用向量*最大距离来限制拖拽距离 //if (Vector2.Distance(childRectTrans.position, this.transform.position) > maxDis) //{ // Vector2 dir = (childRectTrans.position - this.transform.position).normalized; // childRectTrans.anchoredPosition = dir * maxDis; //} GetHV(); } } public void OnEndDrag(PointerEventData eventData) { //当结束拖动,要将物体归0,为了加一点缓冲效果 //(1)可以使用dotween等补间动画插件,会减少很多 //rectTransform.DoAnchoredPos(Vector2.zero,0.5f); //(2)或者使用携程 这里使用携程 if (coroutine == null) coroutine = StartCoroutine(IEToZeroPos(childRectTrans, 0.1f)); } private void GetHV() { h = childRectTrans.anchoredPosition.x / maxDis; v = childRectTrans.anchoredPosition.y / maxDis; } private IEnumerator IEToZeroPos(RectTransform rectTransform, float duartion) { if (duartion == 0f) { yield return null; rectTransform.anchoredPosition = Vector2.zero; GetHV(); coroutine = null; yield break; } Vector2 currentpos = rectTransform.anchoredPosition; float offx = currentpos.x / duartion; float offy = currentpos.y / duartion; while (rectTransform.anchoredPosition != Vector2.zero) { yield return null; rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x - offx * Time.deltaTime, rectTransform.anchoredPosition.y - offy * Time.deltaTime); GetHV(); if (rectTransform.anchoredPosition.sqrMagnitude < 8f) { rectTransform.anchoredPosition = Vector2.zero; GetHV(); coroutine = null; break; } } } } ```

 

另外附上Cube上面的脚本

 

 

加个使用doTween的吧

```cpp using UnityEngine; using UnityEngine.EventSystems; using System.Collections; using DG.Tweening; public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler { public static float h, v; //传出hv public float maxDis; //最大距离 private RectTransform childRectTrans; private Coroutine coroutine = null; void Start() { childRectTrans = transform.GetChild(0) as RectTransform; } public void OnBeginDrag(PointerEventData eventData) { if (coroutine != null) { StopCoroutine(coroutine); coroutine = null; } } public void OnDrag(PointerEventData eventData) { Vector3 outPos; if (RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transform as RectTransform, eventData.position, eventData.pressEventCamera, out outPos)) { childRectTrans.position = outPos; //限制拖拽距离 childRectTrans.anchoredPosition = Vector2.ClampMagnitude(childRectTrans.anchoredPosition, maxDis); //或者利用子物体和父物体的距离判断是否超过最大距离,当距离大于等于最大的距离时候, //计算父物体和子物体的向量,然后利用向量*最大距离来限制拖拽距离 //if (Vector2.Distance(childRectTrans.position, this.transform.position) > maxDis) //{ // Vector2 dir = (childRectTrans.position - this.transform.position).normalized; // childRectTrans.anchoredPosition = dir * maxDis; //} GetHV(); } } public void OnEndDrag(PointerEventData eventData) { //当结束拖动,要将物体归0,为了加一点缓冲效果 //(1)可以使用dotween等补间动画插件,会减少很多 rectTransform.DoAnchoredPos(Vector2.zero,0.5f).OnUpdate(GetHV); } private void GetHV() { h = childRectTrans.anchoredPosition.x / maxDis; v = childRectTrans.anchoredPosition.y / maxDis; } } ```

 

以上就是小游戏网站源码,Unity 超级简单摇杆制作实现的相关代码, 更多内容欢迎关注之后的文章

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《小游戏网站源码,Unity 超级简单摇杆制作》
文章链接:https://www.456zj.com/9289.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址