最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity實(shí)現(xiàn)簡(jiǎn)單搖桿的制作

 更新時(shí)間:2021年09月16日 15:17:29   作者:Zero_LJ  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單搖桿的制作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

利用UGUI制作一個(gè)簡(jiǎn)單搖桿,效果圖

1、首先建立兩個(gè)Image,然后將其中一個(gè)為父物體,另一個(gè)為子物體,并且調(diào)整好大小:

ps:將子物體的錨點(diǎn)設(shè)置為居中          

2、在父物體上寫個(gè)JoyStick.cs腳本:

  

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);
 
            //或者利用子物體和父物體的距離判斷是否超過(guò)最大距離,當(dāng)距離大于等于最大的距離時(shí)候,
            //計(jì)算父物體和子物體的向量,然后利用向量*最大距離來(lái)限制拖拽距離
            //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)
    {
        //當(dāng)結(jié)束拖動(dòng),要將物體歸0,為了加一點(diǎn)緩沖效果
        //(1)可以使用dotween等補(bǔ)間動(dòng)畫插件,會(huì)減少很多
        //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上面的腳本  

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);
        }
    }

加個(gè)使用doTween的吧

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);
 
            //或者利用子物體和父物體的距離判斷是否超過(guò)最大距離,當(dāng)距離大于等于最大的距離時(shí)候,
            //計(jì)算父物體和子物體的向量,然后利用向量*最大距離來(lái)限制拖拽距離
            //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)
    {
        //當(dāng)結(jié)束拖動(dòng),要將物體歸0,為了加一點(diǎn)緩沖效果
        //(1)可以使用dotween等補(bǔ)間動(dòng)畫插件,會(huì)減少很多
        rectTransform.DoAnchoredPos(Vector2.zero,0.5f).OnUpdate(GetHV);     
    }
    private void GetHV()
    {
        h = childRectTrans.anchoredPosition.x / maxDis;
        v = childRectTrans.anchoredPosition.y / maxDis;
    }
  
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 同時(shí)兼容JS和C#的RSA加密解密算法詳解(對(duì)web提交的數(shù)據(jù)加密傳輸)

    同時(shí)兼容JS和C#的RSA加密解密算法詳解(對(duì)web提交的數(shù)據(jù)加密傳輸)

    這篇文章主要給大家介紹了關(guān)于同時(shí)兼容JS和C#的RSA加密解密算法,通過(guò)該算法可以對(duì)web提交的數(shù)據(jù)進(jìn)行加密傳輸,文中通過(guò)圖文及示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。
    2017-07-07
  • ZooKeeper的安裝及部署教程

    ZooKeeper的安裝及部署教程

    Zookeeper是一個(gè)針對(duì)大型分布式系統(tǒng)的可靠協(xié)調(diào)系統(tǒng),提供的功能包括:配置維護(hù)、名字服務(wù)、分布式同步、組服務(wù)等,這篇文章主要介紹了ZooKeeper的安裝及部署,需要的朋友可以參考下
    2019-06-06
  • C#編程之事務(wù)用法

    C#編程之事務(wù)用法

    這篇文章主要介紹了C#編程之事務(wù)用法,結(jié)合實(shí)例形式對(duì)比分析了C#中事務(wù)提交與回滾的具體實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • 二叉樹(shù)的遍歷算法(詳細(xì)示例分析)

    二叉樹(shù)的遍歷算法(詳細(xì)示例分析)

    以下代碼是對(duì)二叉樹(shù)的遍歷算法進(jìn)行了分析介紹,需要的朋友可以參考下
    2013-05-05
  • C#使用NPOI對(duì)Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出

    C#使用NPOI對(duì)Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出

    這篇文章介紹了C#使用NPOI對(duì)Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • WPF實(shí)現(xiàn)雷達(dá)圖(仿英雄聯(lián)盟)的示例代碼

    WPF實(shí)現(xiàn)雷達(dá)圖(仿英雄聯(lián)盟)的示例代碼

    這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)雷達(dá)圖(仿英雄聯(lián)盟)的繪制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-07-07
  • C#調(diào)用sql2000存儲(chǔ)過(guò)程方法小結(jié)

    C#調(diào)用sql2000存儲(chǔ)過(guò)程方法小結(jié)

    這篇文章主要介紹了C#調(diào)用sql2000存儲(chǔ)過(guò)程的方法,以實(shí)例形式分別對(duì)調(diào)用帶輸入?yún)?shù)及輸出參數(shù)的存儲(chǔ)過(guò)程進(jìn)行了詳細(xì)分析,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • 剖析設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用

    剖析設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用

    這篇文章主要介紹了設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用,理論上來(lái)說(shuō)組合模式包含抽象構(gòu)件、樹(shù)葉構(gòu)件和樹(shù)枝構(gòu)件三個(gè)角色,需要的朋友可以參考下
    2016-02-02
  • 詳解C#編程中一維數(shù)組與多維數(shù)組的使用

    詳解C#編程中一維數(shù)組與多維數(shù)組的使用

    這篇文章主要介紹了詳解C#編程中一維數(shù)組與多維數(shù)組的使用,包括數(shù)組初始化等基礎(chǔ)知識(shí)的講解,需要的朋友可以參考下
    2016-01-01
  • C#采用OpenXml實(shí)現(xiàn)給word文檔添加文字

    C#采用OpenXml實(shí)現(xiàn)給word文檔添加文字

    這篇文章主要介紹了C#采用OpenXml實(shí)現(xiàn)給word文檔添加文字的方法,包括了用法的實(shí)例分析,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-09-09

最新評(píng)論

炉霍县| 辛集市| 同德县| 若尔盖县| 云霄县| 紫阳县| 慈利县| 迭部县| 左云县| 泰顺县| 绥宁县| 郁南县| 赫章县| 临夏市| 米脂县| 阿克苏市| 安陆市| 麻江县| 大理市| 泾川县| 轮台县| 祥云县| 金塔县| 扎赉特旗| 博客| 紫阳县| 开化县| 江永县| 泰宁县| 沽源县| 大关县| 安福县| 宁武县| 志丹县| 乐陵市| 专栏| 梅州市| 赤城县| 松江区| 酒泉市| 石林|