Unity-AudioSource组件利用UGUI搭建简易音乐播放器

Unity-AudioSource组件利用UGUI搭建简易音乐播放器_第1张图片

实现功能主要4个脚本:音乐源管理资源加载,进度条SliderUI,音乐信息显示UI(prefab)

 

音乐源管理脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System;

public class Sound_demo : MonoBehaviour
{
    AudioSource source;
    RectTransform content;
    SliderUI slider;//进度控制
    Slider volume;//音量控制

    int currentIndex = 0;//当前音频索引
                         // Use this for initialization
    void Start()
    {
        source = this.GetComponent();//获该脚本下的音频源组件
        content = GameObject.Find("Content").GetComponent();//找到Scroll View 下的Content 用来存放预设体
        slider = GameObject.Find("Slider").GetComponent();//获取进度条下的自定义SliderUI:Slider组件
        volume = GameObject.Find("Volume").GetComponent();//获取音量进度条
        InitSoundList();//初始化播放列表
    }

    // Update is called once per frame
    void Update()
    {
        //if (slider!=null&& source.isPlaying) {
        //    slider.value = source.time / Date_demo.Instance.clips[currentIndex].length;
        //}


        if (source == null || source.clip == null) return;
        if (source.isPlaying && SliderUI.State == SliderState.Normal)
        {

            print("当前歌曲下标:" + currentIndex);
            slider.value = source.time / source.clip.length;//进度条实时更新
            //判断歌曲是否播放完毕,开始播放下一首
            if (source.time>=source.clip.length*0.99f) {
                OnPlayNext();
            }
            source.volume = volume.value;//音量实时更新
        }
        //拖拽滑块播放
        if (SliderUI.State == SliderState.Up)
        {
            source.time = slider.value * source.clip.length;
            slider.Reset();
        }
    }
    void InitSoundList()
    {
        //根据加载的歌曲资源将其添加到播放列表中
        for (var i = 0; i < Date_demo.Instance.clips.Length; i++)
        {
            AudioClip clip = Date_demo.Instance.clips[i];
            GameObject Amusic = GameObject.Instantiate(Date_demo.Instance.prefabs, content);


            //动态添加脚本
            SoundItem item = Amusic.AddComponent();
            item.InitItemInfo(clip.name, clip.length, i);
            item.AddListener(ToPlay);
        }
    }

    //点击播放
    private void ToPlay(int obj)
    {
        source.time = 0;
        if (source.isPlaying)
        {
            source.Stop();
        }
        currentIndex = obj;
        source.clip = Date_demo.Instance.clips[obj];
        source.Play();
    }

    //播放暂停按钮
    public void OnPlayPause()
    {

        if (source.isPlaying)
        {
            source.Pause();
            return;
        }
        if (source.clip == null)
        {
            source.clip = Date_demo.Instance.clips[currentIndex];
        }
        source.Play();
    }
    //播放下一曲
    public void OnPlayNext()
    {
        source.time = 0;
        currentIndex++;
        if (currentIndex >= Date_demo.Instance.clips.Length) 
        {
            currentIndex = 0;

        }
        source.clip = Date_demo.Instance.clips[currentIndex];
        source.Play();

    }
    //播放上一曲
    public void OnPlayLast()
    {
        source.time = 0;
        currentIndex--;
        if (currentIndex<0) {
            currentIndex = Date_demo.Instance.clips.Length - 1;
        }
        source.clip = Date_demo.Instance.clips[currentIndex];
        source.Play();
      
    }
}

 

 

资源加载脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Date_demo : MonoBehaviour {

    public static Date_demo Instance;
    [HideInInspector]
    public AudioClip[] clips;
    [HideInInspector]
    public GameObject prefabs;

    private void Awake()
    {
        
        Instance = this;
        //加载Resources/Musics目录下的音频
        clips = Resources.LoadAll("Musics");
        //加载Resources/Amusic目录下的预制体
        prefabs = Resources.Load("Prefabs/Amusic");

    }
    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

 

 

进度条SliderUI:(重写Slider组件)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum SliderState
{
    Normal, Down, Up
}
public class SliderUI :Slider{
    public static SliderState State = SliderState.Normal;//默认状态2,按下时候1,松开时候0
    public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData)
    {
        base.OnPointerDown(eventData);
        State = SliderState.Down;
        print("按下");

    }
    public override void OnPointerUp(UnityEngine.EventSystems.PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        State = SliderState.Up;
        print("松开");
    }
    public void Reset() {
        State = SliderState.Normal;
    }

}

 

 

音乐信息显示UI:(挂载在Prefab上)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//挂载每个音乐信息显示UI
public class SoundItem : MonoBehaviour {

	//歌曲委托,index歌曲索引
    public event System.Action SOUNDPLAY;
    //保存索引
    private int index=-1;
    private Text nameUI;
    private Text timerUI;
    private void Awake()
    {
        nameUI = transform.Find("name").GetComponent();
        timerUI = transform.Find("time").GetComponent();
    }
    void Start () {
        Button but = this.GetComponent

End...

你可能感兴趣的:(UnityC#脚本,Unity)