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 { public enum QueryStrategy { GetUnique, CreateNew, } public Dictionary movie_collection = new Dictionary(); Dictionary> avpro_movies2 = new Dictionary>(); 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(); } List 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 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(); 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(); //else #endif movie = gameObject.AddComponent(); 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(); 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; } }