Unity VR 点击按钮旋转角度

190 篇文章 9 订阅
2 篇文章 0 订阅

效果就是这样的

 

using UnityEngine;
using DG.Tweening;

public class GameItemSpawn : MonoBehaviour
{
    public static GameItemSpawn Instance;
    public  int Index = 0;
    public Material[] m_GameItemMatArr;
    public GameObject go_GameItem;
    public float m_Angle;
    private void Awake()
    {
        Instance = this;
        m_Angle = 360.0f / m_GameItemMatArr.Length;
        for (int i = 0; i < m_GameItemMatArr.Length; i++)
        {
            GameObject go = Instantiate(go_GameItem, transform);
            go.transform.localEulerAngles = new Vector3(0, i * m_Angle, 0);
            go.GetComponentInChildren<MeshRenderer>().material = m_GameItemMatArr[i];
            go.GetComponentInChildren<GameItem>().SetPath(m_GameItemMatArr[i].name);
            go.GetComponentInChildren<GameItem>().index=i;

        }
    }
    public void RetateForward()
    {
        Index+=1;

        if (Index >= m_GameItemMatArr.Length)
        {
            Index = 0;
        }
        transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
    }
    public void RetateBack()
    {
        Index-=1;
        if (Index < 0)
        {
            Index = m_GameItemMatArr.Length - 1;
        }
        transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
    }

}

GameItem 有 Unity VRTK的按钮监听方法 不过要先添加 VRTK_ControllerEvents脚本

绑定在VRTK_Manager脚本上

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using VRTK;
public class GameItem : MonoBehaviour
{
    public string m_VideoName;
    public string GetVideoPath;
    public VideoPlayer m_videoPlayer;
    public int index = 0;
    void Awake()
    {
        m_videoPlayer = GetComponent<VideoPlayer>();
        GameObject.Find("VRControllerRight").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItem_Tpuch;
        GameObject.Find("VRControllerLeft").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItem_Tpuch;
    }
    void Update()
    {
        if (index == GameItemSpawn.Instance.Index)
        {
            GetComponent<MeshCollider>().enabled = true;
            GetComponent<MeshRenderer>().material.color=Color.white;
        }
        else
        {
            GetComponent<MeshCollider>().enabled = false;
            GetComponent<MeshRenderer>().material.color=Color.gray;

        }

    }
    //圆盘按钮抬起
    private void GameItem_Tpuch(object sender, ControllerInteractionEventArgs e)
    {
        m_videoPlayer.Pause();
    }

    public void SetPath(string videoNam)
    {
        m_videoPlayer.url = Application.streamingAssetsPath + "/" + videoNam + ".mp4";
    }
    private void OnTriggerEnter(Collider other)
    {
        if(System.IO.File.Exists(m_videoPlayer.url)==false) return;
        m_videoPlayer.Play();
    }
    private void OnTriggerExit(Collider other)
    {
        m_videoPlayer.Pause();

    }
}

按钮脚本就很简单了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameSelectPanel : MonoBehaviour
{
    private Button btn_Forward;
    private Button btn_Break;
    private Button btn_Start;
    private string[] m_GameItemName;
    private Text txt_Title;
    private void ReadGameItemText()
    {
        TextAsset textAsset = Resources.Load<TextAsset>("游乐项目名字");
        m_GameItemName = textAsset.text.Split('\n');
    }
    private void Awake()
    {
        ReadGameItemText();
        btn_Forward = transform.Find("Next").GetComponent<Button>();
        btn_Forward.onClick.AddListener(() =>
        {
            Debug.Log("btn_Forward");
            GameItemSpawn.Instance.RetateForward();
        });
        btn_Break = transform.Find("Last").GetComponent<Button>();
        btn_Break.onClick.AddListener(() =>
        {
            Debug.Log("btn_Break");
            GameItemSpawn.Instance.RetateBack();
        });
        btn_Start = transform.Find("Btn_Start").GetComponent<Button>();
        btn_Start.onClick.AddListener(Setlec);
        txt_Title = transform.Find("Title").GetComponent<Text>();
    }
    void Update()
    {
        txt_Title.text = m_GameItemName[GameItemSpawn.Instance.Index];
    }
    private void Setlec()
    {
        LodingPanel.Intsance.LoadScene();
    }

}

隐藏手柄和不让改变位置信息的代码是这样的

  if (SceneManager.GetActiveScene().name == "MainScene")
        {
            UnityEngine.XR.InputTracking.disablePositionalTracking = false;
        }
        else
        {
            UnityEngine.XR.InputTracking.disablePositionalTracking = true;
        }

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值