温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Unity3D简单的音乐播放器【代码】

发布时间:2020-07-12 19:44:19 来源:网络 阅读:1344 作者:xiaoxuanyunmeng 栏目:游戏开发
import System.IO;
import System.Collections;
import System.Text;
var www : WWW;
var file:ArrayList;
var scrollPosition :Vector2= Vector2.zero;
var scrollVelocity:float  = 0f;
var timeTouchPhaseEnded:float  = 0f;
var inertiaDuration:float = 0.5f;
var lastDeltaPos:Vector2 ;
var skin:GUISkin;
?
function Start () {
file=new ArrayList();
he("C:\\","*.mp3",file);
} <!--DVFMTSC-->
?
var play:Texture;
var pause:Texture;
var next:Texture;
var last:Texture;
var sound:Texture;
?
function OnGUI(){
GUI.skin=skin;
var y:int=0;
scrollPosition = GUI.BeginScrollView( Rect(0, 0, Screen.width, Screen.height), scrollPosition,  Rect(0, 0,Screen.width, file.Count*60), false, false);
//Unity3D:www.unitymanual.com
for(var f:String in file){
//把系统编码转成utf-8,因为android上是utf-8
var utf:Encoding =Encoding.UTF8;//utf-8
var gb:Encoding =Encoding.Default;//系统的编码
var temp:byte[] = gb.GetBytes(f);
var temp1:byte[] = Encoding.Convert(gb, utf, temp);
var f1:String=utf.GetString(temp1);
?
//用lastindexof和substring方法获取文件名(不包括路径)
var p:int=f1.LastIndexOf('\\',f1.Length-1); //必须是'\\',如果是‘\’会报错的。如果是手机要写‘/’
var f2:String=f1.Substring(p+1,f1.Length-1-p);
var b=GUI.Button(Rect(0,y,Screen.width,60),"   "+f2);
?
if(b){
www=new WWW("file://"+f);    //记住这里面的“//”别写成“\\”就行哈;
audio.clip=www.audioClip;
}
y+=60;
}
GUI.EndScrollView();
?
}
?
?
function Update (){
if(Input.touchCount > 0){
if(Input.GetTouch(0).phase == TouchPhase.Moved){
scrollPosition.y += Input.GetTouch(0).deltaPosition.y;
lastDeltaPos = Input.GetTouch(0).deltaPosition;
}
else if (Input.GetTouch(0).phase == TouchPhase.Ended){
print ("End:"+lastDeltaPos.y+"|"+Input.GetTouch(0).deltaTime);
if(Mathf.Abs(lastDeltaPos.y)> 20.0f){
scrollVelocity = lastDeltaPos.y * 0.5/ Input.GetTouch(0).deltaTime;
print(scrollVelocity);
}
timeTouchPhaseEnded = Time.time;
}
?
}
?
else{
if(scrollVelocity != 0.0f){
var t:float = (Time.time - timeTouchPhaseEnded)/inertiaDuration;
var frameVelocity:float = Mathf.Lerp(scrollVelocity, 0, t);
scrollPosition.y += frameVelocity * Time.deltaTime;
if (t >= inertiaDuration)
scrollVelocity = 0;
}
?
}
if(Input.GetKey(KeyCode.Escape)){
Application.Quit();
}
if(!audio.isPlaying)
audio.Play();
}
?
function he(path:String,pattern:String,al:ArrayList){
if(path!=null){
var f1:String[]=Directory.GetFiles(path,pattern);
var d1:String[];
for(var f11:String in f1){
al.Add(f11);
}
try{
d1=Directory.GetDirectories(path);
for(var d11:String in d1){
try{ he(d11,pattern,al);}
catch(Exception){}
}
}catch(Exception){}
?
}
}
?

Unity3D简单的音乐播放器【代码】

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI