keijiro / KlakHap

HAP video player plugin for Unity
Other
341 stars 27 forks source link

Dynamically switch videos #35

Closed jBachalo closed 4 years ago

jBachalo commented 4 years ago

Hi Thanks for providing this excellent resource to the community. I have 2 looping videos that I simply want to switch between at runtime Simply calling the Open method alone with the new video file isn't sufficient to do this.

These are the changes I made to the Open method. Will I have any garbage collection issues?

`public void Open(string filePath, PathMode pathMode = PathMode.StreamingAssets)
    {
        if (_demuxer != null)
        {
            Debug.LogError("Stream has already been opened.");
            //return;
             _demuxer.Dispose();// new line
            _demuxer = null; // new line
        }

        _filePath = filePath;
        _pathMode = pathMode;

        OpenInternal();
    }`
keijiro commented 4 years ago

Will I have any garbage collection issues?

Yes. Other internal objects (_stream, _decoder, _texture, _updater) will be leaked.

I'd recommend destroying the HapPlayer component and recreate a new instance to open another file.

keijiro commented 4 years ago

I'm closing this issue now. Please feel free to reopen it for further problems.

jBachalo commented 4 years ago

Thanks. Just thinking this would be a very useful feature. And could I simply do the following?

`public void Open(string filePath, PathMode pathMode = PathMode.StreamingAssets)
    {
        if (_demuxer != null)
        {
            Debug.LogError("Stream has already been opened.");
            //_stream, _decoder, _texture, _updater

            _stream.Dispose();
            _stream = null;

             _decoder.Dispose();
            _decoder = null;

            UnityEngine.Object.Destroy( _texture );
            _texture = null;

             _updater.Dispose();
            _updater = null;

            _demuxer.Dispose();// new line
        _demuxer = null; // new line
            //return;
        }

        _filePath = filePath;
        _pathMode = pathMode;

        OpenInternal();
    }`
keijiro commented 4 years ago

I think your modification is on the right track this time. Yes, it might be a useful feature, it needs extensive testing though.

jBachalo commented 4 years ago

Thanks. Unfortunately even with above still memory leak(s)! Based on your suggestion am creating 2nd component on same GameObject as HapPlayer. Testing and will post results.

     `using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
    using UnityEngine.Playables;

    namespace Klak.Hap{

public class DestroyHAP : MonoBehaviour
{
    HapPlayer _hap;
    public RenderTexture rt;

 public void DestroyComponent()
  {
    if(GetComponent<HapPlayer>() != null){
    Destroy(GetComponent<HapPlayer>());
            }
  }

public HapPlayer CreateComponent()
{
   _hap = gameObject.AddComponent<HapPlayer>();
        _hap.targetTexture= rt;
        _hap.targetRenderer= GetComponent<MeshRenderer>();
        return _hap;

}
}

}`