VR拆装(HTC vive Pro开发)——4、一些脚本的分享(一)

项目主场景的搭建主要参照了VR游乐场这个项目VR游乐园(Unity2018.1.0)_哔哩哔哩_bilibili

你可以参考这个项目的视频去按部就班的实现你需要的功能。在这里我会把我用到的代码分享给你。

1、这是实现UI绕轴前后转的一段代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System.IO;

public class GameItemSpawn : MonoBehaviour
{

    public static GameItemSpawn Instance;
    public Material[] m_GameItemMatArr;  // 材质球
    public GameObject go_GameItem;
    public int Index = 0;
    public float m_Angle;  // 角度

    // 初始化生成GameItem,有多少个取决于有多少个游乐项目,就是看材质球数量
    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>().SetVideoName(m_GameItemMatArr[i].name);
            go.GetComponentInChildren<GameItem>().Index = i;
        }
    }

    // 向前转
    public void RotateForward()
    {
        Index++;
        if (Index >= m_GameItemMatArr.Length)
        {
            Index = 0;
        }
        transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
    }


    // 向后转
    public void RotateBack()
    {
        Index--;
        if (Index < 0)
        {
            Index = m_GameItemMatArr.Length - 1;
        }
        transform.DORotate(new Vector3(0, -Index * m_Angle, 0), 0.3f);
    }
}

2、这是实现显示当前UI内容名称的代码

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

public class GameItemSelectPanel : MonoBehaviour
{
    private Button btn_Forward;
    private Button btn_Back;
    private Button btn_Select;
    private Text txt_Title;
    private string[] m_GameItemNameArr;
   
    private void Awake()
    {       
        Init();
        TextAsset textAsset = Resources.Load<TextAsset>("项目名字");
        m_GameItemNameArr = textAsset.text.Split('\n');     
    }
    private void Update()
    {
        txt_Title.text = m_GameItemNameArr[GameItemSpawn.Instance.Index];
    }
    private void ReadGameItemNameText()
    {
        TextAsset textAsset = Resources.Load<TextAsset>("项目名字");
        m_GameItemNameArr = textAsset.text.Split('\n');
    }

    private void Init()
    {
        txt_Title = transform.Find("txt_Title").GetComponent<Text>();
        btn_Forward = transform.Find("btn_Forward").GetComponent<Button>();
        btn_Forward.onClick.AddListener(() =>
        {
            GameItemSpawn.Instance.RotateForward();
        });
        btn_Back = transform.Find("btn_Back").GetComponent<Button>();
        btn_Back.onClick.AddListener(() =>
        {
            GameItemSpawn.Instance.RotateBack();
        });
        btn_Select = transform.Find("btn_Select").GetComponent<Button>();
    }
}

3、这是实现播放当前UI对应视频资源的代码 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using VRTK;
public class GameItem : MonoBehaviour
{
    public int Index;
    private VideoPlayer m_VideoPlayer;

    private void Awake()
    {
        m_VideoPlayer = GetComponent<VideoPlayer>();
        
    }
   
    /// <summary>
    /// 圆盘按钮抬起
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    //private void GameItem_TouchpadReleased(object sender, ControllerInteractionEventArgs e)
    //{
    //    m_VideoPlayer.Pause();
    //}
    /// <summary>
    /// 设置视频名称
    /// </summary>
    /// <param name="videoName"></param>
    public void SetVideoName(string videoName)
    {
        m_VideoPlayer.url = GetVideoPath(videoName);
    }
    /// <summary>
    /// 获取视频路径
    /// </summary>
    /// <param name="videoName"></param>
    /// <returns></returns>
    private string GetVideoPath(string videoName)
    {
        return Application.dataPath + "/StreamingAssets/" + videoName + ".mp4";
    }
    private void OnTriggerEnter(Collider other)
    {
        //代表文件不存在
        //if (File.Exists(m_VideoPlayer.url) == false) return;
        m_VideoPlayer.Play();
    }
    private void OnTriggerExit(Collider other)
    {
        m_VideoPlayer.Pause();

    }
}

4、这是实现为项目增添切换场景也不会被销毁的背景音频的代码

using UnityEngine;
using System.Collections;

public class audio : MonoBehaviour
{
    static audio _instance;
    // Use this for initialization
    void Start()
    {

    }
    public static audio instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<audio>();
                DontDestroyOnLoad(_instance.gameObject);
            }
            return _instance;
        }
    }

    void Awake()
    {

        //此脚本永不消毁,并且每次进入初始场景时进行判断,若存在重复的则销毁
        if (_instance == null)
        {
            _instance = this;
            DontDestroyOnLoad(this);
        }
        else if (this != _instance)
        {
            Destroy(gameObject);
        }

    }
   
}

 

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
HTC Vive是一种虚拟现实头显设备,为用户提供身临其境的游戏体验。如果你想入门HTC Vive游戏开发,以下是一些基本的步骤和建议。 首先,了解虚拟现实技术并熟悉HTC Vive设备的功能和特性。熟悉VR开发的基本原理,例如头部追踪、手部追踪和房间追踪等。你可以阅读HTC Vive的文档和教程,或参加相关的线上教学课程。 其次,学习游戏开发的基础知识和技巧。掌握Unity或Unreal Engine等虚拟现实游戏引擎的使用,这些引擎提供了开发虚拟现实游戏所需的工具和功能。你可以通过教程、示例项目和社区论坛来学习和掌握这些引擎。 接下来,开始制定你的游戏概念和设计。考虑你想要创建的游戏类型和玩法,以及如何充分利用HTC Vive的特性来提供更好的游戏体验。你可以画草图、编写设计文档和制作原型来帮助你更好地理解和实现你的游戏想法。 然后,开始编写和开发你的游戏。利用你学到的知识和工具,创建虚拟现实场景、模型和动画,并添加交互性和游戏逻辑。在这个过程中,不断测试和调试你的游戏,以确保它在HTC Vive设备上正常运行并提供良好的用户体验。 最后,发布和推广你的游戏。将你的游戏打包并提交到HTC Vive的官方市场或其他游戏分发平台上。同时,在社交媒体和相关的游戏开发社区中宣传和推广你的游戏,以吸引更多的玩家。 总结起来,入门HTC Vive游戏开发需要你对虚拟现实技术和HTC Vive设备有一定的了解,掌握游戏开发的基础知识和技巧,制定游戏概念和设计,实际编写和开发游戏,并最终发布和推广你的作品。通过不断学习和实践,你将能够创建出令人惊叹的HTC Vive虚拟现实游戏作品。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是施托克儿啊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值