You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.9 KiB

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR_64 || UNITY_STANDALONE_WIN64
#else
//[RequireComponent(typeof(AVProQuickTimeManager))]
#endif
public class MoviePlayerPool : PoolBase<MoviePlayerPool>
{
public enum QueryStrategy
{
GetUnique,
CreateNew,
}
public Dictionary<string, MoviePlayerBase> movie_collection = new Dictionary<string, MoviePlayerBase>();
Dictionary<string, List<MovieLock>> avpro_movies2 = new Dictionary<string, List<MovieLock>>();
public MoviePlayerBase GetMoviePlayer(DShowClip clip, bool loop = true, QueryStrategy strategy = QueryStrategy.GetUnique)
{
if (clip == null)
return null;
MoviePlayerBase result = null;
switch (strategy)
{
case QueryStrategy.GetUnique:
{
if (movie_collection.ContainsKey(clip.fullPath) == false)
{
MoviePlayerBase m = GetMovie(clip, loop);
if (movie_collection.ContainsKey(clip.fullPath) == false)
movie_collection.Add(clip.fullPath, m);
}
result = movie_collection[clip.fullPath];
break;
}
case QueryStrategy.CreateNew:
{
if (avpro_movies2.ContainsKey(clip.fullPath) == false)
{
avpro_movies2[clip.fullPath] = new List<MovieLock>();
}
List<MovieLock> candidates = avpro_movies2[clip.fullPath];
//print(filename);
foreach (var m in candidates)
{
//print(m.isLocked);
if (m.isLocked == false)
{
m.movie.Loop = loop;
m.isLocked = true;
return m.movie;
}
}
var movie = GetMovie(clip, loop);
avpro_movies2[clip.fullPath].Add(new MovieLock(movie));
result = movie;
break;
}
}
return result;
}
class MovieLock
{
public MoviePlayerBase movie;
public bool isLocked;
public MovieLock(MoviePlayerBase movie)
{
this.movie = movie;
isLocked = true;
}
}
public void releaseMovie(MoviePlayerBase movie)
{
foreach (var l in avpro_movies2)
{
List<MovieLock> candidates = l.Value;
for (int i = 0; i < candidates.Count; i++)
{
MovieLock m = candidates[i];
if (m.movie == movie)
{
m.isLocked = false;
m.movie.Stop();
}
}
}
}
MoviePlayerBase GetMovie(DShowClip videoAsset, bool loop)
{
#if true
MoviePlayerBase movie = null;
var file_info = GetFileInfo(videoAsset.fullPath);
string path = file_info.FullName;
string ext = file_info.Extension;
if ( ext.Equals(".avi"))
{
movie = gameObject.AddComponent<DShowMoviePlayer>();
if (movie.Load(videoAsset) == false)
{
Debug.Log("Failed to load " + videoAsset.fullPath);
Destroy(movie);
}
}
#else
MoviePlayerBase movie = null;
var file_info = GetFileInfo(filename);
string path = file_info.FullName;
string ext = file_info.Extension;
bool try_load_image = false;
Debug.Log("loading file: " + path);
if (ext.Equals(".mov")|| ext.Equals(".avi"))
{
#if UNITY_EDITOR_64 || UNITY_STANDALONE_WIN64
#else
//if(ext.Equals(".mov"))
// movie = gameObject.AddComponent<AVProQuickTimeMoviePlayer>();
//else
#endif
movie = gameObject.AddComponent<DShowMoviePlayer>();
if (movie.Load(path) == false)
{
Destroy(movie);
try_load_image = true;
path = Path.ChangeExtension(path, ".jpg");
filename = Path.ChangeExtension(filename, ".jpg");
}
}
else if (ext.Equals(".jpg") || ext.Equals(".png"))
{
try_load_image = true;
}
if (try_load_image)
{
movie = gameObject.AddComponent<ImageMoviePlayer>();
movie.Load(path);
}
#endif
if (movie != null)
{
movie.Loop = loop;
//movie.Filename = filename;
//movie.DataRoot = dataRoot;
}
else
{
//Debug.LogFormat("[Movie Player Pool] unknown file: {0}", filename);
}
return movie;
}
}