コード例 #1
0
    ///////////////////////////////////////////
    /// Init()
    /// Configures the actual audio source and
    /// plays the sound.
    ///////////////////////////////////////////
    public void Init(string strResource, Transform tf, Hashtable i_hashOptional)
    {
        // load the clip
        AudioClip clip = Resources.Load(strResource) as AudioClip;

        if (clip == null)
        {
            Debug.LogError("No such sound clip for resource " + strResource);
            Destroy(gameObject);
            return;
        }

        // create the audio source
        audioSource      = gameObject.AddComponent <AudioSource>();
        audioSource.clip = clip;

        // set the default volume (may be overriden)
        float fDefaultVolume = Constants.GetConstant <float>("DefaultVolume");

        audioSource.volume = fDefaultVolume;

        ID_Audio dataAudio = IDL_Audio.GetData(strResource);

        if (dataAudio != null)
        {
            audioSource.volume = dataAudio.GetVolume();
            audioSource.loop   = dataAudio.ShouldLoop();
        }

        // change pitch if necessary
        if (i_hashOptional.ContainsKey("pitch"))
        {
            float fPitch = (float)i_hashOptional["pitch"];
            audioSource.pitch = fPitch;
        }

        gameObject.transform.parent   = tf;
        gameObject.transform.position = tf.position;
        audioSource.Play();

        // add destroy script -- if the audio doesn't loop
        if (audioSource.loop == false)
        {
            m_fLifetime = clip.length + 0.1f;
            if (i_hashOptional.ContainsKey("Time"))
            {
                m_fLifetime = (float)i_hashOptional["Time"];
            }

            DestroyThis scriptDestroy = gameObject.AddComponent <DestroyThis>();
            scriptDestroy.SetLife(m_fLifetime);
        }
    }