using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [ExecuteInEditMode] [RequireComponent(typeof(MoviePlayerBase))] public class MovieTextureApply : MonoBehaviour { public enum Target { RawImage, Image, Material } public Target target = Target.Image; public List materials = new List(); public string attributeName; Texture oddVideTexture; Dictionary updater_map = new Dictionary(); //bool inited = false; public bool autoSize = false; Action updater = null; // Use this for initialization void Start () { updater_map = new Dictionary() { { Target.RawImage, UpdateRawImage }, { Target.Material, UpdateMaterial }, { Target.Image, UpdateImage }, }; if(updater_map.ContainsKey(target)) updater = updater_map[target]; //Debug.LogWarning("Implement editor code for better usage."); } // Update is called once per frame void Update () { if (updater != null) updater(); } void UpdateRawImage() { //if(inited == false) { MoviePlayerBase movie = GetComponent(); RawImage img = GetComponent(); if(movie.Texture != oddVideTexture) { img.texture = oddVideTexture = movie.Texture; if (autoSize) GetComponent().sizeDelta = new Vector2(oddVideTexture.width, oddVideTexture.height); //inited = true; } } } public bool showWarnning() { if (GetComponent() == null || target != Target.Image) return false; if (GetComponent().material.name != "Default UI Material") return true; return false; } void UpdateImage() { MoviePlayerBase movie = GetComponent(); Image img = GetComponent(); if (movie.Texture != oddVideTexture && movie.Texture != null) { img.material.mainTexture = oddVideTexture = movie.Texture; if (autoSize) GetComponent().sizeDelta = new Vector2(oddVideTexture.width, oddVideTexture.height); } } void UpdateMaterial() { //if (inited == false) { MoviePlayerBase movie = GetComponent(); if (movie.Texture != oddVideTexture) { oddVideTexture = movie.Texture; foreach (var m in materials) { m.SetTexture(attributeName, movie.Texture); } //inited = true; } } } }