UnityVR游乐园笔记#002_主场景

跟着课程做的一个项目(VR游乐园)

课程地址

1. 主场景的搭建

1-1. 场景背景

在这里插入图片描述

1-2. 头盔和手柄

摄像机要调整到10000

在这里插入图片描述

2. 创建UI界面(World Space)

在这里插入图片描述

在这里插入图片描述

3. 实现轮播图

创建 ImageItem 预制体

在这里插入图片描述

用代码生成 ImageItem

挂载在空对象

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

public class ImageItemCreaty : MonoBehaviour
{
    
    
    public static ImageItemCreaty instance;
    [HideInInspector]
    public int i;
    public Material[] materials;
    public GameObject imageItem;
    private float angle;
    

    private void Awake()
    {
    
    
        instance = this;
    }

    private void Start()
    {
    
    
        angle = 360.0f / materials.Length;
        for (int i = 0; i < materials.Length; i++)
        {
    
    
            GameObject go = Instantiate(imageItem, transform);
            go.transform.localEulerAngles = new Vector3(0, i * angle, 0);
            go.transform.Find("Quad").GetComponent<MeshRenderer>().material = materials[i];
        }
    }

    // 向右转
    public void RotateRight()
    {
    
    
        i = ++i % materials.Length;
        transform.DORotate(new Vector3(0, -i * angle, 0), 0.3f);
    }

    // 向左转
    public void RotateLeft()
    {
    
    
        i--;
        if(i < 0)
        {
    
    
            i = materials.Length - 1;
        }    
        transform.DORotate(new Vector3(0, -i * angle, 0), 0.3f);
    }

}

UI代码

挂载在 Canvas 上

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

public class UIMainScene : MonoBehaviour
{
    
    
    private Button rightButton;
    private Button leftButton;
    private Button selectButton;
    private Text titleText;
    private string[] names;

    private void Awake()
    {
    
    
        titleText = transform.Find("TitleText").GetComponent<Text>();
        rightButton = transform.Find("RightButton").GetComponent<Button>();
        leftButton = transform.Find("LeftButton").GetComponent<Button>();
        selectButton = transform.Find("SelectButton").GetComponent<Button>();
    }

    private void Start()
    {
    
    
    	// 获取全部名字
        TextAsset textAsset = Resources.Load<TextAsset>("Text/Name");
        names = textAsset.text.Split('\n');
        
        rightButton.onClick.AddListener(ImageItemCreaty.instance.RotateRight);
        leftButton.onClick.AddListener(ImageItemCreaty.instance.RotateLeft);
    }

    private void Update()
    {
    
    
        titleText.text = names[ImageItemCreaty.instance.i];
    }

}

轮播图效果

在这里插入图片描述

4. 实现轮播图的视频播放

给中间的 Quad 添加 VideoPlayer 组件, 并添加代码 Quad 脚本,然后在 ImageItemCreaty 添加 go.transform.Find("Quad").GetComponent<Quad>().SetVideoName(materials[i].name);
来调用 Quad 脚本里面的方法

在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Video;

public class Quad : MonoBehaviour
{
    
    
    public static Quad instance;
    private VideoPlayer vp;

    private void Awake()
    {
    
    
        instance = this;
        vp = GetComponent<VideoPlayer>();
    }

    public void SetVideoName(string name)
    {
    
    
        vp.url = GetVideoName(name);
    }

    private string GetVideoName(string name)
    {
    
    
        return Application.streamingAssetsPath + "/" + name + ".mp4";
    }

    public void PlayVideo()
    {
    
    
        if (!File.Exists(vp.url)) return;
        vp.Play();
    }

    public void PauseVideo()
    {
    
    
        vp.Stop();
    }
}

在切换轮播图的时候添加以下代码

//关闭全部在播放的视频
        for (int j = 0; j < transform.childCount; j++)
        {
    
    
            transform.GetChild(j).GetChild(0).GetComponent<Quad>().PauseVideo();
        }

        transform.GetChild(i).GetChild(0).GetComponent<Quad>().PlayVideo();

最后效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/My_csdnQMDX/article/details/126621974