Unity PC 后台可以替换WAV格式的音乐,把音乐放在StreamingAssets文件夹下面

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

public class MyMain : MonoBehaviour
{
    public List<AudioClip> mAudioClip = new List<AudioClip>();
    [Header("点击左边的按钮的时候content要滚动到什么位置")]
    public List<float> sliceContentY = new List<float>();
    public Transform all_musicName;
    public Text Name_time;
    private Text newTxt;
    public GameObject LiftAllBtn;

    private float newTxt_X;
    private float newTxt_Y;
    private Text audioTotalTime;


    //==获取每个音乐的总时间
    private int currentHour;
    private int currentMinute;
    private int currentSecond;
    private AudioSource audioSource;
    private FileInfo[] files;

    void Start()
    {

        audioSource = GetComponent<AudioSource>();
        newTxt_X = Name_time.GetComponent<RectTransform>().localPosition.x;
        newTxt_Y = Name_time.GetComponent<RectTransform>().localPosition.y;
      
        for (int i = 0; i < LiftAllBtn.transform.childCount; i++)
        {
            Button btn = LiftAllBtn.transform.GetChild(i).GetComponent<Button>();
            EventTriggerListener.Get(btn.gameObject).onClick = onLiftBtn;
            Image img = btn.transform.GetChild(0).GetComponent<Image>();
            if(i>0)
            img.gameObject.SetActive(false);
        }

        string filePath_folder = Application.streamingAssetsPath + "/MP3";
        DirectoryInfo direction = new DirectoryInfo(filePath_folder);
        files = direction.GetFiles("*WAV", SearchOption.AllDirectories);
        print(files.Length);
        StartCoroutine(IELoadExternalAudioWebRequest());
    }


    private IEnumerator IELoadExternalAudioWebRequest() {
        for (int i = 0; i < files.Length; i++) {

            UnityWebRequest _unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(files[i].ToString(), AudioType.WAV);
            yield return _unityWebRequest.SendWebRequest();

            if (_unityWebRequest.isHttpError || _unityWebRequest.isNetworkError) {
                Debug.Log(_unityWebRequest.error.ToString());
            } else {
                AudioClip _audioClip = DownloadHandlerAudioClip.GetContent(_unityWebRequest);
                mAudioClip.Add(_audioClip);
                mAudioClip[i].name = files[i].Name;
                if (i == files.Length - 1) {
                    //  audioSource.clip = mAudioClip[i];
                    //   audioSource.Play();

                    print(i + "___i");
                    init();

                }

            }
        }
    }

 

    void onLiftBtn(GameObject sender)
    {
        GameObject _content = all_musicName.parent.gameObject;
        for (int i = 0; i < LiftAllBtn.transform.childCount; i++)
        {
            Image img = LiftAllBtn.transform.GetChild(i).GetChild(0).GetComponent<Image>();
            if (sender.name== LiftAllBtn.transform.GetChild(i).name)
            {
                _content.GetComponent<RectTransform>().localPosition = new Vector2(0, sliceContentY[i]);
                img.gameObject.SetActive(true);
            }
            else
            {
                img.gameObject.SetActive(false);
            }
        }
    }
    private void init()
    {

        print("inti");
        for (int i = 0; i < mAudioClip.Count; i++)
        {
            float num = mAudioClip[i].length;

           audioSource.clip = mAudioClip[i];

            newTxt = Text.Instantiate(Name_time, all_musicName);
            newTxt.name = "a" + i;
            newTxt.transform.localPosition = new Vector2(newTxt_X, newTxt_Y);
            newTxt_Y -= 63;

            newTxt.GetComponent<Text>().text = mAudioClip[i].name;
            audioTotalTime = newTxt.transform.GetChild(0).GetComponent<Text>();


            currentHour = (int)audioSource.clip.length / 3600;
            currentMinute = (int)(audioSource.clip.length - currentHour * 3600) / 60;
            currentSecond = (int)(audioSource.clip.length - currentHour * 3600 - currentMinute * 60);
         
            audioTotalTime.text = string.Format("{0:D2}:{1:D2}:{2:D2}", currentHour, currentMinute, currentSecond);
            //  audioTotalTime.text = string.Format("{0:D2}:{1:D2}:{2:D2}",
            //  currentHour, currentMinute, currentSecond);

            if (i == mAudioClip.Count-1)
            {
                Destroy(all_musicName.GetChild(0).gameObject);
            }
        }


    }



   
}

 

posted on 2021-11-22 17:18  zqiang0803  阅读(224)  评论(0编辑  收藏  举报

导航