当前位置:   article > 正文

unity3d简单的音乐播放器_unity3引擎制作简易音乐播放器

unity3引擎制作简易音乐播放器

这几天用u3d做应用,感觉没效率,尽管它可以用C#写,但是感觉不行,所以我还是早点收手,不做应用了,把这几天做的一个小东西给大家玩玩,音乐播放器~~~

我就直接贴代码了哈,里面有一些注释的,呵呵~~~~但是手机上的编码问题没解决哦~~~有知道的同学,麻烦指点一下,我的qq:397319689(有点笨);

  1. import System.IO;
  2. import System.Collections;
  3. import System.Text;
  4. var www : WWW;
  5. var file:ArrayList;
  6. var scrollPosition :Vector2= Vector2.zero;
  7. var scrollVelocity:float = 0f;
  8. var timeTouchPhaseEnded:float = 0f;
  9. var inertiaDuration:float = 0.5f;
  10. var lastDeltaPos:Vector2 ;
  11. var skin:GUISkin;
  12. function Start () {
  13. file=new ArrayList();
  14. he("C:\\","*.mp3",file);
  15. }
  16. var play:Texture;
  17. var pause:Texture;
  18. var next:Texture;
  19. var last:Texture;
  20. var sound:Texture;
  21. function OnGUI(){
  22. GUI.skin=skin;
  23. var y:int=0;
  24. scrollPosition = GUI.BeginScrollView( Rect(0, 0, Screen.width, Screen.height), scrollPosition, Rect(0, 0,Screen.width, file.Count*60), false, false);
  25. for(var f:String in file){
  26. //把系统编码转成utf-8,因为android上是utf-8
  27. var utf:Encoding =Encoding.UTF8;//utf-8
  28. var gb:Encoding =Encoding.Default;//系统的编码
  29. var temp:byte[] = gb.GetBytes(f);
  30. var temp1:byte[] = Encoding.Convert(gb, utf, temp);
  31. var f1:String=utf.GetString(temp1);
  32. //用lastindexof和substring方法获取文件名(不包括路径)
  33. var p:int=f1.LastIndexOf('\\',f1.Length-1); //必须是'\\',如果是‘\’会报错的。如果是手机要写‘/’
  34. var f2:String=f1.Substring(p+1,f1.Length-1-p);
  35. var b=GUI.Button(Rect(0,y,Screen.width,60)," "+f2);
  36. if(b){
  37. www=new WWW("file://"+f); //记住这里面的“//”别写成“\\”就行哈;
  38. audio.clip=www.audioClip;
  39. }
  40. y+=60;
  41. }
  42. GUI.EndScrollView();
  43. }
  44. function Update (){
  45. if(Input.touchCount > 0){
  46. if(Input.GetTouch(0).phase == TouchPhase.Moved){
  47. scrollPosition.y += Input.GetTouch(0).deltaPosition.y;
  48. lastDeltaPos = Input.GetTouch(0).deltaPosition;
  49. }
  50. else if (Input.GetTouch(0).phase == TouchPhase.Ended){
  51. print ("End:"+lastDeltaPos.y+"|"+Input.GetTouch(0).deltaTime);
  52. if(Mathf.Abs(lastDeltaPos.y)> 20.0f){
  53. scrollVelocity = lastDeltaPos.y * 0.5/ Input.GetTouch(0).deltaTime;
  54. print(scrollVelocity);
  55. }
  56. timeTouchPhaseEnded = Time.time;
  57. }
  58. }
  59. else{
  60. if(scrollVelocity != 0.0f){
  61. var t:float = (Time.time - timeTouchPhaseEnded)/inertiaDuration;
  62. var frameVelocity:float = Mathf.Lerp(scrollVelocity, 0, t);
  63. scrollPosition.y += frameVelocity * Time.deltaTime;
  64. if (t >= inertiaDuration)
  65. scrollVelocity = 0;
  66. }
  67. }
  68. if(Input.GetKey(KeyCode.Escape)){
  69. Application.Quit();
  70. }
  71. if(!audio.isPlaying)
  72. audio.Play();
  73. }
  74. function he(path:String,pattern:String,al:ArrayList){
  75. if(path!=null){
  76. var f1:String[]=Directory.GetFiles(path,pattern);
  77. var d1:String[];
  78. for(var f11:String in f1){
  79. al.Add(f11);
  80. }
  81. try{
  82. d1=Directory.GetDirectories(path);
  83. for(var d11:String in d1){
  84. try{ he(d11,pattern,al);}
  85. catch(Exception){}
  86. }
  87. }catch(Exception){}
  88. }
  89. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/118412
推荐阅读
相关标签
  

闽ICP备14008679号