1 using UnityEngine; 2 using System.Collections; 3 4 public class TestLage : MonoBehaviour { 5 public MovieManage Movie;//定义一个播放器组件 6 public UITexture uiTexture; 7 public GameObject ga; 8 private string rootPath = "StreamingAssets/MovieSamples/";//存放音乐的地址 9 // Use this for initialization 10 void Start () { 11 } 12 // Update is called once per frame 13 void Update () { 14 } 15 /// <summary> 16 /// 回调方法 17 /// </summary> 18 /// <param name="str"></param> 19 public void MyAction(string str) 20 { 21 Debug.Log(str); 22 } 23 void OnGUI() 24 { 25 if (GUILayout.Button("PLAY")) 26 { 27 if (ga==null) 28 { 29 Creat(); 30 } 31 Movie.setAction(MyAction); 32 Movie.PlayMovie("HandOfFate"); 33 } 34 if (GUILayout.Button("PAUSE")) 35 { 36 if(ga!=null) 37 { 38 Movie.PauseMovie(); 39 } 40 } 41 if (GUILayout.Button("STOP")) 42 { 43 if (ga != null) 44 { 45 Movie.StopMovie(); 46 } 47 } 48 if (GUILayout.Button("Destroy")) 49 { 50 if (ga != null) 51 { 52 DestroyMovie(); 53 } 54 } 55 } 56 57 public void DestroyMovie() 58 { 59 if (ga!= null) 60 { 61 Movie.Texture1.Stop(); 62 Destroy(ga); 63 } 64 } 65 66 public void Creat() 67 { 68 ga = NGUITools.AddChild(this.gameObject); 69 ga.name = "ga"; 70 ga.AddComponent<MovieManage>(); 71 Movie =ga.GetComponent("MovieManage") as MovieManage;//获取播放器组件 72 UITexture tex = NGUITools.AddChild<UITexture>(ga.gameObject); 73 } 74 }
电影管理代码
1 using UnityEngine; 2 using System.Collections; 3 using System; 4 /// <summary> 5 /// 说明: 6 /// 方法:1 MovieManage.PlayMovie("MovieName");MovieName是电影名,必须在resources文件夹下面,rootPath是相对resources。 7 /// 2 MovieManage.Texture1.Play();播放电影 8 /// 3 Movie.Texture1.Pause();暂停电影 9 /// 4 Movie.Texture1.Stop();停止播放电影 10 /// </summary> 11 public class MovieManage : MonoBehaviour { 12 private string rootPath = "StreamingAssets/MovieSamples/";//存放音乐的地址 13 public MovieTexture Texture1; 14 public UITexture uiTexture; 15 private Action<string> action; 16 public bool checkFlag = false; 17 18 public bool SPFlag = true; 19 20 public void setAction(Action<string> action) 21 { 22 this.action = action; 23 } 24 // Use this for initialization 25 void Start () { 26 } 27 // Update is called once per frame 28 void Update () { 29 if (checkFlag) 30 { 31 uiTexture.GetComponent<UIWidget>().width = Screen.width; 32 uiTexture.GetComponent<UIWidget>().height = Screen.height; 33 if (SPFlag) 34 { 35 if (!Texture1.isPlaying) 36 { 37 action("MovieTest测试"); 38 checkFlag = false; 39 Texture1.Stop(); 40 Destroy(gameObject); 41 } 42 43 } 44 } 45 } 46 public void PlayMovie(string movieName) 47 { 48 uiTexture = (UITexture)this.transform.FindChild("Texture").GetComponent<UITexture>(); 49 //在这里资源加载,movie是视频的文件名不需要后缀名 50 Texture1 = (MovieTexture)Resources.Load(rootPath + movieName, typeof(MovieTexture)); 51 uiTexture.mainTexture= Texture1; 52 Texture1.Play(); 53 checkFlag = true; 54 SPFlag = true; 55 } 56 public void StopMovie() 57 { 58 SPFlag = false; 59 Texture1.Stop(); 60 } 61 public void PauseMovie() 62 { 63 Texture1.Pause(); 64 SPFlag = false; 65 } 66 67 }
核心代码:
ga = NGUITools.AddChild(this.gameObject);//自动生成gameObject物件
ga.name = "ga";//修改名字
ga.AddComponent<MovieManage>();//添加脚步组件
Movie =ga.GetComponent("MovieManage") as MovieManage;//获取播放器组件
UITexture tex = NGUITools.AddChild<UITexture>(ga.gameObject);//给ga添加UITexture组件。
时间: 2024-11-05 10:02:58