欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實現(xiàn)簡單搖桿的制作

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

利用UGUI制作一個簡單搖桿,效果圖

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

ps:將子物體的錨點設置為居中          

2、在父物體上寫個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);
 
            //或者利用子物體和父物體的距離判斷是否超過最大距離,當距離大于等于最大的距離時候,
            //計算父物體和子物體的向量,然后利用向量*最大距離來限制拖拽距離
            //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上面的腳本  

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的吧

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#并行編程之Task同步機制

    C#并行編程之Task同步機制

    這篇文章介紹了C#并行編程之Task同步機制,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • 剖析設計模式編程中C#對于組合模式的運用

    剖析設計模式編程中C#對于組合模式的運用

    這篇文章主要介紹了設計模式編程中C#對于組合模式的運用,理論上來說組合模式包含抽象構件、樹葉構件和樹枝構件三個角色,需要的朋友可以參考下
    2016-02-02
  • 解析C#自定義控件的制作與使用實例的詳解

    解析C#自定義控件的制作與使用實例的詳解

    本篇文章是對C#中自定義控件的制作與使用實例進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • WPF設置窗體可以使用鼠標拖動大小的方法

    WPF設置窗體可以使用鼠標拖動大小的方法

    這篇文章主要介紹了WPF設置窗體可以使用鼠標拖動大小的方法,涉及針對窗口的操作與設置技巧,具有很好的借鑒價值,需要的朋友可以參考下
    2014-11-11
  • C#求n個數(shù)中最大值和最小值的方法

    C#求n個數(shù)中最大值和最小值的方法

    這篇文章主要介紹了C#求n個數(shù)中最大值和最小值的方法,涉及C#中max及min方法的使用技巧,需要的朋友可以參考下
    2015-05-05
  • C#語言中的修飾符匯總

    C#語言中的修飾符匯總

    本文主要介紹的是C#語言中的修飾符,主要從四方面介紹,希望對大家有幫助,一起來看。
    2015-10-10
  • 詳解C#如何優(yōu)雅地終止線程

    詳解C#如何優(yōu)雅地終止線程

    在大多情況下,我們只關心線程的創(chuàng)建與啟動,運行,卻并不關心線程的結束或者終止。今天這篇文章,我們就以一些簡單的小例子,簡述如何有效的停止線程,僅供學習分享使用,如有不足之處,還請指正
    2023-03-03
  • C#.Net ArrayList的使用方法

    C#.Net ArrayList的使用方法

    這篇文章主要介紹了C#.Net ArrayList的使用方法,使用動態(tài)數(shù)組的優(yōu)點是可以根據(jù)用戶需要,有效利用存儲空間,需要的朋友可以參考下
    2015-10-10
  • c# 使用計時器和觀察者模式實現(xiàn)報警推送需求

    c# 使用計時器和觀察者模式實現(xiàn)報警推送需求

    這篇文章主要介紹了c# 使用計時器和觀察者模式實現(xiàn)報警推送需求,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • C#修改及重置電腦密碼DirectoryEntry實現(xiàn)方法

    C#修改及重置電腦密碼DirectoryEntry實現(xiàn)方法

    這篇文章主要介紹了C#修改及重置電腦密碼DirectoryEntry實現(xiàn)方法,實例分析了C#修改及重置電腦密碼的相關技巧,需要的朋友可以參考下
    2015-05-05

最新評論