- Home /
Switching clip in videoPlayer for Unity 5.6.0f3 causes Lag and makes CPU usage 100% for BehaviourUpdate
Making project with SteamVR and unity 5.6.0f3. making a simple switching video mechanism and everytime the videoclip switches within the videoplayer and it plays the new clip. it causes a CPU usage spike and my steamVR switches from the application to the compositor for a split second. . I have also tried stopping the clip before switching and playing again.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
[RequireComponent(typeof(AudioSource))]
public class PlayVideo : MonoBehaviour
{
public VideoClip clip;
private SerialUISummoner canvas;
VideoPlayer video;
int vsyncprevious;
private bool playing = false;
// Use this for initialization
void Start()
{
video = GetComponent<VideoPlayer>();
video.clip = clip;
video.SetTargetAudioSource(0, GetComponent<AudioSource>());
canvas = Object.FindObjectOfType<SerialUISummoner>();
}
// Update is called once per frame
void Update()
{
if (canvas != null)
{
if (canvas.Showing && !playing)
{
playing = true;
print("START");
PlayVid();
}
if (!canvas.Showing && playing)
{
playing = false;
StopVid();
}
}
}
public void PlayVid()
{
video.Play();
}
public void StopVid()
{
video.Stop();
}
public void Change()
{
if (video != null)
{
playing = false;
video.enabled = false;
video.clip = clip;
video.enabled = true;
playing = true;
PlayVid();
}
}
}
Answer by astamarr · Jul 31, 2017 at 09:06 PM
Couldn't solved it, so i use multiple video players and just desactivate the game object renderer of finished one. Sucks for optimisation but only way i found to not go make it seamless.
Answer by andersemil · Aug 25, 2017 at 10:16 AM
It seems to be related to garbage collection when a videoclip is finished playing or replaced. It still happens in 2017.1.0p4 :(