unity中怎样实现调整视频播放速度的功能?
更好的解决方案出来了\u0026lt;( ̄︶ ̄)\u0026gt;原方案有个卡顿问题,修改过后可以流畅播放了。只不过一开始要等待两三秒。思路:用序列帧打assetbundle资源包(每包200帧,不能多,多了在安卓上内存会爆掉)——\u0026gt;解读第1个资源包到数组1——\u0026gt;解读第2个资源包到数组2——\u0026gt;从数组1的第一张图片开始贴图,每贴一张图,就把数组中前一张图释放掉——\u0026gt;贴完数组1的最后一张图片,贴数组2的第一张图,同时用协程把第3个资源包解读到数组1中——\u0026gt;贴完数组2的最后一张图片,贴数组1的第一张图,同时用协程把第4个资源包解读到数组2中——\u0026gt;一直循环这个过程。改速度也是通过改timeScale来实现。有几个要点:1.每个ab包不能大,一旦大了在安卓上容易内存泄漏。2.图片资源本身不能太大,不然在安卓上读图片到数组的速度比贴图片到面片的速度慢,会导致前一个ab包还没完全读完数据到数组中,又要读下一个ab包了,然后出现ab包被占用的问题。3.资源包的位置一定要放对。因为用协程的时候不能用try catch,所以协程的部分出错的话会直接闪退。而读资源的这部分正是用协程实现的,所以如果资源的位置不对,在安卓上就会直接闪退。测试结果:PC端一秒250帧跑起来毫无压力。安卓端只能保证一秒50帧的速度,再快就报错了。继续寻找更好的方案,你们有好主意的话告诉我哟~(o゜▽゜)o☆简化版的测试代码如下(删除部分):using UnityEngine;using System.Collections;public class AssetBundleLoadBefore : MonoBehaviour {\t// 假如有4个包,每个包200帧。设置2个数组来循环。\tpublic GameObject plane;//背景面片\tTexture tx1;// 贴图\tTexture tx2;// 贴图\tAssetBundle assetBundle;// 资源包\tint bag=0;// 现在加载的第几个包\tstring bags="";// 现在加载的包名\tint load=0;// 现在加载第几个物体\tstring loads="";// 现在加载的物体名\tint each=200;// 每包多少帧\tint last=200;// 最后一个包多少帧\tint tote=800;// 视频总帧数\tint number=4;// 文件夹里面assetboundle包的个数\tstring path="";// 资源路径\tint arrayFrame=0;// 贴数组里的第几张图\tint arrayNumLoad=1;// 加载第几个数组里的图\tint arrayNumRenderer=1;// 贴第几个数组里的图\tbool ifchooseed=false;// 是否选好加载方式\tbool ifplay=false;// 是否播放\tstring MSG="";// 文字提示\tbool iffirstset=false;\tvoid Awake()\t{\t\t#if UNITY_ANDROID\t\teach=200;\t\tlast=200;\t\ttote=800;\t\tpath="/mnt/sdcard/AssetBundle/";\t\t#elif UNITY_STANDALONE_WIN || UNITY_EDITOR\t\teach=1000;\t\tlast=1000;\t\ttote=4000;\t\tpath=Application.dataPath + "/AssetBundle/" + "000002" + "/";\t\t#endif\t}\tvoid FixedUpdate()\t{\t\t\tif(iffirstset==false)\t\t\t{\t\t\t\tLoadAssetBundle1(ref tx1);\t\t\t\tLoadAssetBundle1(ref tx2);\t\t\t\tRendererTexture1(tx1);\t\t\t}\t\t\tif(ifplay==true)\t\t\t{\t\t\t\tif(arrayNumRenderer==1)// 如果贴第一个数组\t\t\t\t{\t\t\t\t\tVieoPlay2(1,tx1);\t\t\t\t}\t\t\t\telse// 贴第二个数组\t\t\t\t{\t\t\t\t\tVieoPlay2(2,tx2);\t\t\t\t}\t\t\t}\t}\tvoid OnGUI()\t{\t\tGUI.skin.button.fontSize=50;\t\tGUI.skin.label.fontSize=50;\t\tif(ifchooseed==false)\t\t{\t\t\tif(GUILayout.Button("assetbundle coroutines"))// 用协程提前加载ab包\t\t\t{\t\t\t\tway=2;\t\t\t\tifchooseed=true;\t\t\t\tDebug.Log("assetbundle coroutines:"+Time.realtimeSinceStartup);\t\t\t}\t\t\tif(GUILayout.Button("exit"))\t\t\t{\t\t\t\tApplication.Quit();\t\t\t}\t\t}\t\telse\t\t{\t\t\tif(Time.timeScale\u0026lt;5f)\t\t\t{\t\t\t\tif(GUILayout.Button("add speed"))\t\t\t\t{\t\t\t\t\tTime.timeScale+=0.1f;\t\t\t\t}\t\t\t}\t\t\tif(Time.timeScale\u0026gt;0.2f)\t\t\t{\t\t\t\tif(GUILayout.Button("subtract speed"))\t\t\t\t{\t\t\t\t\tTime.timeScale-=0.1f;\t\t\t\t}\t\t\t}\t\t\tif(GUILayout.Button("play"))\t\t\t{\t\t\t\tifplay=true;\t\t\t}\t\t\tif(GUILayout.Button("stop"))\t\t\t{\t\t\t\tifplay=false;\t\t\t}\t\t\tif(GUILayout.Button("exit"))\t\t\t{\t\t\t\tApplication.Quit();\t\t\t}\t\t}\t\tGUILayout.Label(MSG);\t\t\t}\t\t\tvoid VieoPlay2(int arraynum,Texture tx)\t{\t\tif(arrayFrame\u0026lt;tx.Length)\t\t{\t\t\tRendererTexture2(arraynum);\t\t}\t\telse\t\t{\t\t\tif(arraynum==2)\t\t\t{\t\t\t\tarrayNumRenderer=1;\t\t\t\tarrayNumLoad=2;\t\t\t}\t\t\telse\t\t\t{\t\t\t\tarrayNumRenderer=2;\t\t\t\tarrayNumLoad=1;\t\t\t}\t\t\tarrayFrame=0;\t\t\tStartCoroutine(LoadAssetBundle22(arrayNumLoad));\t\t}\t\t\t}\t\tIEnumerator LoadAssetBundle22(int arraynum)\t{\t\tyield return true;\t\tiffirstset=true;\t\tint count=0;\t\tif(bag\u0026gt;=number)\t\t{\t\t\tbag=0;\t\t\tload=0;\t\t\tcount=each;\t\t}\t\telse if(bag==number-1)\t\t{\t\t\tcount=last;\t\t}\t\telse\t\t{\t\t\tcount=each;\t\t}\t\tbags=\t\t\t#if UNITY_ANDROID\t\t\t"Android"+\tbag.ToString("d2")+".assetbundle";\t\t#elif UNITY_STANDALONE_WIN || UNITY_EDITOR\t\tbag.ToString("d2")+".assetbundle";\t\t#else\t\tbag.ToString("d2")+".assetbundle";\t\t#endif\t\tif(assetBundle==null)\t\t{\t\t\tassetBundle=AssetBundle.CreateFromFile(path+ bags);\t\t}\t\t\t\tif(arraynum==1)\t\t{\t\t\ttx1=new Texture;\t\t\tfor(int i=0;i\u0026lt;count;i++)\t\t\t{\t\t\t\tloads=load.ToString("d6");\t\t\t\ttx1=assetBundle.Load(loads) as Texture;\t\t\t\tyield return load;\t\t\t\tload++;\t\t\t}\t\t}\t\telse\t\t{\t\t\ttx2=new Texture;\t\t\tfor(int i=0;i\u0026lt;count;i++)\t\t\t{\t\t\t\tloads=load.ToString("d6");\t\t\t\ttx2=assetBundle.Load(loads) as Texture;\t\t\t\tyield return load;\t\t\t\tload++;\t\t\t}\t\t}\t\tbag ++;\t\tCleanAssetBundle1();\t\t\t}\t\t\t/// \u0026lt;summary\u0026gt;\t/// 销毁内存图片\t/// \u0026lt;/summary\u0026gt;\t/// \u0026lt;returns\u0026gt;The load texture.\u0026lt;/returns\u0026gt;\t/// \u0026lt;param name="tx"\u0026gt;内存图片\u0026lt;/param\u0026gt;\tIEnumerator UnLoadTexture2(Texture tx)\t{\t\t//\t\tyield return new WaitForSeconds(0.001f);\t\tyield return true;\t\tResources.UnloadAsset(tx);\t}\t\t/// \u0026lt;summary\u0026gt;\t/// 贴一张图,然后从内存销毁前一张图\t/// \u0026lt;/summary\u0026gt;\t/// \u0026lt;param name="tx"\u0026gt;贴图\u0026lt;/param\u0026gt;\tvoid RendererTexture2(int arraynumber)\t{\t\ttry {\t\t\tif(arraynumber==1)\t\t\t{\t\t\t\tplane.renderer.material.mainTexture=tx1;\t\t\t\tif(arrayFrame\u0026gt;0)\t\t\t\t{\t\t\t\t\tStartCoroutine(UnLoadTexture2(tx1));\t\t\t\t}\t\t\t}\t\t\telse\t\t\t{\t\t\t\tplane.renderer.material.mainTexture=tx2;\t\t\t\tif(arrayFrame\u0026gt;0)\t\t\t\t{\t\t\t\t\tStartCoroutine(UnLoadTexture2(tx2));\t\t\t\t}\t\t\t}\t\t\t\t\t\tarrayFrame++;\t\t} catch (System.Exception ex) {\t\t}\t\t\t}\t\tvoid LoadAssetBundle1(ref Texture tx)\t{\t\tint count=0;\t\tif(bag\u0026gt;=number)\t\t{\t\t\tbag=0;\t\t\tload=0;\t\t\tcount=each;\t\t}\t\telse if(bag==number-1)\t\t{\t\t\tcount=last;\t\t}\t\telse\t\t{\t\t\tcount=each;\t\t}\t\ttx=new Texture;\t\tbags=\t\t\t#if UNITY_ANDROID\t\t\t"Android"+\tbag.ToString("d2")+".assetbundle";\t\t#elif UNITY_STANDALONE_WIN || UNITY_EDITOR\t\tbag.ToString("d2")+".assetbundle";\t\t#endif\t\tassetBundle=AssetBundle.CreateFromFile(path+ bags);\t\tfor(int i=0;i\u0026lt;count;i++)\t\t{\t\t\tloads=load.ToString("d6");\t\t\ttx=assetBundle.Load(loads) as Texture;\t\t\tload++;\t\t\tiffirstset=true;\t\t}\t\tbag ++;\t\tCleanAssetBundle1();\t}\t\tvoid RendererTexture1(Texture tx)\t{\t\ttry {\t\t\tplane.renderer.material.mainTexture=tx;\t\t\tarrayFrame++;\t\t} catch (System.Exception ex) {\t\t}\t}\tvoid CleanAssetBundle1()\t{\t\tassetBundle.Unload(false);\t\tResources.UnloadUnusedAssets();\t}}
推荐阅读
- 聪明人养花,这3种“花”怎样也要养一盆,每年能省不少医药费
- 北京22家市属医院均开展安检基本实现重点区域安检措施全覆盖
- 长江流域渔民退捕“上岸”实现扩产新致富
- 实现“甜蜜计划”,这对中哈跨国夫妻好甜
- 北京地铁11号线西段三座车站提前实现主体结构封顶
- 互联网怎样解决“家政服务上门速度慢”的问题
- 怎样看待从1月8号起,QQ钱包开始提现收费
- 银行it人怎样转型
- 汽车|冬天怎样让车内温度快速升高?座椅加热的最佳使用方式二,外循环的作用总结
- 怎样进入通信行业
