u3d一个GameObject绑定两个AudioSource

u3d 一个GameObject绑定两个AudioSource  ,使他们分别播放,并控制
using UnityEngine;
using System.Collections;

public class PlayMusic : MonoBehaviour 
{
    private AudioSource[] m_ArrayMusic;
    private AudioSource m_music1;
    private AudioSource m_music2;

    // Use this for initialization
    void Start ()
    {
        m_ArrayMusic = gameObject.GetComponents<AudioSource>();
        m_music1 = m_ArrayMusic[0];
        m_music2 = m_ArrayMusic[1];
        
    }
    
    // Update is called once per frame
    void Update () 
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            m_music1.Play();
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            m_music1.Stop();
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            m_music2.Play();
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            m_music2.Stop();
        }

        if (Input.GetKey(KeyCode.Q))
        {
            m_music1.volume -= 0.05f;
        
        }
        if (Input.GetKey(KeyCode.W))
        {
            m_music1.volume += 0.05f;
        }
        if (Input.GetKey(KeyCode.T))
        {
            m_music2.volume -= 0.05f;

        }
        if (Input.GetKey(KeyCode.Y))
        {
            m_music2.volume += 0.05f;
        }


    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。