Unity5.6版本添加了一个新的组件VideoPlayer,这个组件是用来播放视频,这个解决了之前用movietexture播放视频,只能在PC端使用的问题,因为刚出来,可能还有一些问题,今天我要说的是怎样将视频显示在UGUI上,我用了两种方法,一种是在RawImage显示,一种是在Image上显示,这两个有什么区别,一个就是直接放Texture就可以,一个是要把Texture转成Sprite,刚开始的思路是将Texture强转为Texture2D,但是Unity运行的时候会报错,后来想到了用RenderTexture来获取Texture2D,在转成Sprite。好了,下面就是代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; [RequireComponent(typeof(VideoPlayer))] public class UIVideoImage : MonoBehaviour { private RenderTexture movie; private Image image; private RawImage rawImage; private VideoPlayer player; public UIMode UGUI; [Serializable] public class VideoPlayerFinishedEvent : UnityEvent { } // Event delegates finished. [FormerlySerializedAs("onVideoPlayerFinished")] [SerializeField] private VideoPlayerFinishedEvent m_onVideoPlayerFinished = new VideoPlayerFinishedEvent(); public VideoPlayerFinishedEvent videoPlayerFinished { get { return m_onVideoPlayerFinished; } set { m_onVideoPlayerFinished = value; } } public enum UIMode { None = 0, Image = 1, RawImage = 2 } // Use this for initialization void Start() { movie = new RenderTexture(Screen.width, Screen.height, 24); player = GetComponent<VideoPlayer>(); player.sendFrameReadyEvents = true; if (UGUI == UIMode.Image) { image = GetComponent<Image>(); player.renderMode = VideoRenderMode.RenderTexture; player.targetTexture = movie; } else if (UGUI == UIMode.RawImage) { rawImage = GetComponent<RawImage>(); player.renderMode = VideoRenderMode.APIOnly; } videoPlayer.loopPointReached += OnVideoLoopOrPlayFinished; } // Update is called once per frame void Update() { if (UGUI == UIMode.Image) { //在Image上播放视频 if (player.targetTexture == null) return; int width = player.targetTexture.width; int height = player.targetTexture.height; Texture2D t = new Texture2D(width, height, TextureFormat.ARGB32, false); RenderTexture.active = player.targetTexture; t.ReadPixels(new Rect(0, 0, width, height), 0, 0); t.Apply(); image.sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f)) as Sprite; } if (UGUI == UIMode.RawImage) { //在RawImage上播放视频 if (player.texture == null) return; rawImage.texture = player.texture; } } // 播放完成或者循环完成事件 public void OnVideoLoopOrPlayFinished(VideoPlayer vp) { if (videoPlayerFinished != null) { videoPlayerFinished.Invoke(); } } } |
代码就这些,其实也没多复杂。只要将上面的代码拖到UGUI的Image或者RawImage上选择相应的模式就能进行视频播放了。文章源自大腿Plus-https://www.zhaoshijun.com/archives/584
文章源自大腿Plus-https://www.zhaoshijun.com/archives/584
目前来说这个组件还是很好用的,但是在某些情况下,会有莫名其妙的问题。等遇到了再说吧。文章源自大腿Plus-https://www.zhaoshijun.com/archives/584
文章源自大腿Plus-https://www.zhaoshijun.com/archives/584 文章源自大腿Plus-https://www.zhaoshijun.com/archives/584
我的微信
微信扫一扫
shijun_z
我的QQ
QQ扫一扫
846207670
1F
大腿,666
B1
@ Jimmy 谢谢支持
2F
腿哥666
3F
大长腿,刚刚用了你这个方法,打包到手机上不能播放,貌似是找不到资源,直接一篇空白
B1
@ 杰之诺风 你电脑装QuickTime Player播放器了吗,没有的话装一个,把视频重新导进去
4F
怎么在安卓手机上播放直接黑屏啊
B1
@ 一半是记忆。一半是继续 可能和Unity版本有关系
5F
能进行暂停和播放么
B1
@ 此郎非彼狼 可以,不过要自己写暂停播放的方法