服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity实现简单摇杆的制作

Unity实现简单摇杆的制作

2022-12-05 11:26Zero_LJ C#

这篇文章主要为大家详细介绍了Unity实现简单摇杆的制作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

利用UGUI制作一个简单摇杆,效果图

Unity实现简单摇杆的制作

1、首先建立两个Image,然后将其中一个为父物体,另一个为子物体,并且调整好大小:

Unity实现简单摇杆的制作

ps:将子物体的锚点设置为居中          

2、在父物体上写个JoyStick.cs脚本:

Unity实现简单摇杆的制作  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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上面的脚本  

?
1
2
3
4
5
6
7
8
9
10
private void Update()
    {
        Vector3 dir = new Vector3(JoyStick.h, 0, JoyStick.v);
        if (dir.sqrMagnitude > 0)
        {
            transform.Translate(dir * 3f * Time.deltaTime,Space.World);
            Quaternion targatRotate = Quaternion.LookRotation(dir, Vector3.up);
            transform.rotation = Quaternion.Slerp(transform.rotation, targatRotate, 3 * Time.deltaTime);
        }
    }

加个使用doTween的吧

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
    }
  
}

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

原文链接:https://blog.csdn.net/K20132014/article/details/79764419

延伸 · 阅读

精彩推荐
  • C#WPF自定义控件和样式之自定义按钮(Button)

    WPF自定义控件和样式之自定义按钮(Button)

    接触WPF也有两个多月了,有了一定的理论基础和项目经验,现在打算写一个系列,做出来一个WPF的控件库。下面这篇文章主要给大家介绍了关于WPF自定义控...

    小明GG6162022-02-23
  • C#WPF通过线程使用ProcessBar的方法详解

    WPF通过线程使用ProcessBar的方法详解

    这篇文章主要给大家介绍了关于WPF通过线程使用ProcessBar的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WPF具有一定的参考学习价值,...

    风幻影6262022-07-19
  • C#Unity3D Shader实现扫描显示效果(2)

    Unity3D Shader实现扫描显示效果(2)

    这篇文章主要为大家详细介绍了Unity3D Shader实现扫描显示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    星空不语4282022-07-08
  • C#C#实现Base64处理的加密解密,编码解码示例

    C#实现Base64处理的加密解密,编码解码示例

    这篇文章主要介绍了C#实现Base64处理的加密解密,编码解码,结合实例形式分析了基于C#实现的base64编码解码操作相关技巧,需要的朋友可以参考下...

    PointNet5512021-12-18
  • C#Unity 静态变量跨场景操作

    Unity 静态变量跨场景操作

    这篇文章主要介绍了Unity 静态变量跨场景操作方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Yan_Sl7352022-11-13
  • C#C#动态代码生成控件后其他事件不能获取该控件值的解决方法

    C#动态代码生成控件后其他事件不能获取该控件值的解决方法

    这篇文章主要给大家介绍了关于C#动态代码生成控件后其他事件不能获取该控件值的解决方法,文中通过图文介绍的非常详细,对大家的学习或者工作具有...

    Gavinloliy11412022-02-25
  • C#C# 定时器保活机制引起的内存泄露问题解决

    C# 定时器保活机制引起的内存泄露问题解决

    这篇文章主要介绍了C# 定时器保活机制引起的内存泄露问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    丹枫无迹8232022-08-27
  • C#C#实现较为实用的SQLhelper

    C#实现较为实用的SQLhelper

    这篇文章主要为大家详细介绍了C#实现较为实用SQLhelper的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    天空的影子7682021-12-08