0
\$\begingroup\$

I have 26 video files which I have to put in an array and play for different objects. So far I have written this from what I know:

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

public class videomanager : MonoBehaviour {

    public VideoClip[] vids;

    private VideoPlayer vp;

    // Use this for initialization
    void Start () {

        vids = new VideoClip[25];
        vp = gameObject.GetComponent<VideoPlayer> ();
    }

    // Update is called once per frame
    void Update () {

    }
}

I have attached this script on a plane on which I am playing the video.

Before that, I had 26 different planes, one for each different video, but I think it would be better if I could play them on a single plane and change the video according to logic.

I am not sure how I should load them into my player and play them.

\$\endgroup\$
2
\$\begingroup\$

This is a pretty simple application of the clip property and Play() method described in the Unity VideoPlayer docs.

Have you had any difficulty doing something like this?

// This script won't work without a VideoPlayer present,
// so let's ask Unity to enforce that relationship for us.
[RequireComponent(typeof(VideoPlayer))]

// The C# convention for class names is PascalCase.
public class VideoManager : MonoBehaviour {

    // Don't create/size the Array in Start() - that makes an empty
    // array, discarding the clips you assigned in the Inspector.
    public VideoClip[] vids = new VideoClip[26];

    private VideoPlayer vp;

    void Start () {
        vp = gameObject.GetComponent<VideoPlayer> ();
    }

    // Call this method when it's time to play a particular video.
    // Pass a number from 0 to 25 inclusive to choose which video.
    public PlayVideo(int id) {
        // To be safe, let's bounds-check the ID 
        // and throw a descriptive error to catch bugs.
        if(id < 0 || id >= vids.Length) {
            Debug.LogErrorFormat(
               "Cannot play video #{0}. The array contains {1} video(s)",
                                   id,                 vids.Length);
            return;
        }

        // If we get here, we know the ID is safe.
        // So we assign the (id+1)th entry of the vids array as our clip.
        vp.clip = vids[id];

        vp.Play();
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.