load scene after video ended
i have a invertedsphere with my 360 degreevideo on it and when the video end it should change the scene but my brain isnt strong enough to handle this simple problem pls help.
here my both scripts: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video; using UnityEngine.Playables;
public class startthevideomyprecious : MonoBehaviour {
//Raw Image to Show Video Images [Assign from the Editor]
//public RawImage image;
//Video To Play [Assign from the Editor]
public VideoClip videoToPlay;
private VideoPlayer videoPlayer;
private VideoSource videoSource;
//Audio
private AudioSource audioSource;
// Use this for initialization
void Start()
{
Application.runInBackground = true;
StartCoroutine(playVideo());
}
IEnumerator playVideo()
{
//Add VideoPlayer to the GameObject
videoPlayer = gameObject.AddComponent<VideoPlayer> ();
//Add AudioSource
audioSource = gameObject.AddComponent<AudioSource> ();
//Disable Play on Awake for both Video and Audio
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;
//Set video To Play then prepare Audio to prevent Buffering
videoPlayer.clip = videoToPlay;
videoPlayer.Prepare ();
//Wait until video is prepared
while (!videoPlayer.isPrepared) {
Debug.Log ("Preparing Video");
yield return null;
}
Debug.Log ("Done Preparing Video");
//Set Audio Output to AudioSource
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
//Assign the Audio from Video to AudioSource to be played
videoPlayer.EnableAudioTrack (0, true);
videoPlayer.SetTargetAudioSource (0, audioSource);
//Assign the Texture from Video to RawImage to be displayed
//image.texture = videoPlayer.texture;
//Play Video
videoPlayer.Play ();
//Play Sound
audioSource.Play ();
}
}
i tried to get a message when the video ends but nothing happend :( using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video;
public class loadsceneaftercideomiagy : MonoBehaviour {
private VideoPlayer videoPlayer;
// Use this for initialization
// Update is called once per frame
void Update () {
videoPlayer = gameObject.AddComponent<VideoPlayer> ();
if (VideoPlayer.loopPointReached) {
Debug.Log ("video end");
}
}
}
Answer by Hellium · Mar 13, 2018 at 10:58 AM
loopPointReached
is not a boolean, but an event. Here is how you have to use it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.SceneManagement;
public class LoadSceneAfterVideoEnded : MonoBehaviour
{
public VideoPlayer VideoPlayer; // Drag & Drop the GameObject holding the VideoPlayer component
public string SceneName ;
void Start()
{
VideoPlayer.loopPointReached += LoadScene;
}
void LoadScene(VideoPlayer vp)
{
SceneManager.LoadScene( SceneName );
}
}
@Hellium ty for the answer ;D it got me this Assets/scripts/loadsceneaftercideomiagy.cs(13,35): error CS0103: The name `LoadScene' does not exist in the current context
and what you mean with // Drag & Drop the GameObject holding the VideoPlayer component drag & drop it where?
or do you meant i must make an event trigger with the gameobject in it and the function StartVideo() ??? But what eventtype
I made a mistake, sorry, the 2nd function must be called LoadScene
.
Once you have attached the LoadSceneAfterVideoEnded
script on a gameObject, select the latter. Then, drag the object holding the VideoPlayer
component and drop it to the VideoPlayer
field in the inspector.
@Hellium he got me that:
Assets/scripts/loadsceneaftercideomiagy.cs(13,15): error CS0123: A method or delegate `LoadSceneAfterVideoEnded.LoadScene()' parameters do not match delegate `UnityEngine.Video.VideoPlayer.EventHandler(UnityEngine.Video.VideoPlayer)' parameters
maybe my scenemanager is to strange its a bit obsolete:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class scenemanager : $$anonymous$$onoBehaviour {
public void ChangeScene(string sceneName)
{
Application.LoadLevel (sceneName);
} }
hmm is this black magic? I cant fix the error. I guess something is spelled wrong in this line
VideoPlayer.loopPointReached += LoadScene;
but every Variation i try didnt work ( for example -> videoPlayer with small letter)
Before i forget my script is called the right way LoadSceneAfterVideoEnded
@Hellium thx for the Help if you still have some idea i gonna watch this site many times need to fix this issue.
It works well with short videos, Actually I am trying to use it with a 2 $$anonymous$$utes video, but it always doesn't work at playback speed 1. If I increase the playback speed it work... make no sense
Answer by Delidragon · Mar 14, 2018 at 09:28 AM
yeah it works thx for your help i searched for ideoPlayer.loopPointReached += LoadScene; and i got this https://answers.unity.com/questions/1361254/i-made-an-intro-video-for-my-game-but-when-this-vi-1.html worked fine thx @crossmedia411 and thx @Hellium
void Start(){
videoPlayer.loopPointReached += EndReached;
}
void EndReached(UnityEngine.Video.VideoPlayer vp)
{
UnityEngine.SceneManagement.SceneManager.LoadScene (“NewScene”);
}
$$anonymous$$y bad, I thought the signature of the callback didn't expected a Video Player as parameter. I've fixed my answer.
Follow this Question
Related Questions
Tetxure swap using HTC controllers 0 Answers
There is a problem with my script... 0 Answers
c# - error CS0103: The name `hit' does not exist in the current context (cardboard switching) 1 Answer
How do I make somthing happen when the Player reaches a certain x, y, z position? 0 Answers
GoogleVR / C# - How to, with the "pointer click": click 1 move a cube and click 2 move back the cube 0 Answers