【问题标题】:How to make play and pause button on Augmented Reality (Vuforia) video playback in Unity?如何在 Unity 中制作增强现实(Vuforia)视频播放的播放和暂停按钮?
【发布时间】:2020-06-20 01:35:46
【问题描述】:

我的项目有一个问题,当它播放视频时会自动播放。我想如何在 Unity 中播放和暂停视频。我正在使用 Unity 2019.3.5f1 和 Vuforia。有没有关于如何让视频播放AR可以播放和暂停的教程?

我尝试了这段代码,但它不起作用。不知道为什么?

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

public class PlayControl : MonoBehaviour, ITrackableEventHandler
{
    public GameObject videoplayer, Playvideo_button, Pausevideo_button, Stopvideo_button;

    protected TrackableBehaviour mTrackableBehaviour;
    public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");

            if (mTrackableBehaviour.TrackableName == "Video_marker")
            {
                videoplayer.GetComponent<VideoPlayer>().Play();
            }
            OnTrackingFound();
        }
        else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
                  newStatus == TrackableBehaviour.Status.NO_POSE)
        {
            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
            videoplayer.GetComponent<VideoPlayer>().Stop();
            OnTrackingLost();
        }
        else
        {
            // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
            // Vuforia is starting, but tracking has not been lost or found yet
            // Call OnTrackingLost() to hide the augmentations
            OnTrackingLost();
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if(mTrackableBehaviour)
            {
              mTrackableBehaviour.RegisterTrackableEventHandler(this);
            }
    }

    protected virtual void OnTrackingFound()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);

        // Enable rendering:
        foreach (var component in rendererComponents)
            component.enabled = true;

        // Enable colliders:
        foreach (var component in colliderComponents)
            component.enabled = true;

        // Enable canvas':
        foreach (var component in canvasComponents)
            component.enabled = true;
    }


    protected virtual void OnTrackingLost()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);

        // Disable rendering:
        foreach (var component in rendererComponents)
            component.enabled = false;

        // Disable colliders:
        foreach (var component in colliderComponents)
            component.enabled = false;

        // Disable canvas':
        foreach (var component in canvasComponents)
            component.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {

                //case 1
                if (hit.collider.tag == "Playvideo")
                {
                    videoplayer.GetComponent<VideoPlayer>().Play();
                    Playvideo_button.SetActive(false);
                    Pausevideo_button.SetActive(true);
                    Stopvideo_button.SetActive(true);
                }

                //case 2
                if (hit.collider.tag == "Stopvideo")
                {
                    videoplayer.GetComponent<VideoPlayer>().Stop();
                    Playvideo_button.SetActive(true);
                    Pausevideo_button.SetActive(true);
                    Stopvideo_button.SetActive(false);
                }

                //case 3
                if (hit.collider.tag == "Pausevideo")
                {
                    videoplayer.GetComponent<VideoPlayer>().Pause();
                    Playvideo_button.SetActive(true);
                    Pausevideo_button.SetActive(false);
                    Stopvideo_button.SetActive(true);
                }
            }
        }
    }
}

【问题讨论】:

  • 请分享您的代码
  • 完成编辑添加代码
  • 您遇到的错误是什么?

标签: unity3d augmented-reality vuforia


【解决方案1】:

目前还不清楚这个脚本在您的项目中的哪个位置运行。确保您的点击位于正确位置的最佳方法是将脚本添加到按钮中,我看到您没有这样做。您是否确保您的 Raycast 到达正确的按钮?您可以为您的Physics.Raycast 定义一个maxDistance,这可能对您有很大帮助。同样在您的“if”语句中,我会先检查是否存在冲突,然后检查标签,如下所示:

    if (Physics.Raycast(ray, out hit, 900.0f))
        {
            if (hit.transform != null)
                {
                        if (GameObject.ReferenceEquals(hit.transform.gameObject, playButtonGameObject))
                            {
                                videoplayer.GetComponent<VideoPlayer>().Play();
                                Playvideo_button.SetActive(false);
                                Pausevideo_button.SetActive(true);
                                Stopvideo_button.SetActive(true);
                            }
                        else if (GameObject.ReferenceEquals(hit.transform.gameObject, stopButtonGameObject))
                            {
                                videoplayer.GetComponent<VideoPlayer>().Stop();
                                Playvideo_button.SetActive(true);
                                Pausevideo_button.SetActive(true);
                                Stopvideo_button.SetActive(false);
                            }
                        else if (GameObject.ReferenceEquals(hit.transform.gameObject, pauseButtonGameObject))
                            {
                                videoplayer.GetComponent<VideoPlayer>().Pause();
                                Playvideo_button.SetActive(true);
                                Pausevideo_button.SetActive(false);
                                Stopvideo_button.SetActive(true);
                            }
                }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多