找回密码
 立即注册
查看: 5643|回复: 65

[脚本] Unity播放声音代码

[复制链接]
发表于 2012-12-26 19:11 | 显示全部楼层 |阅读模式
[code=csharp]using UnityEngine;
using System.Collections;

public class Sound : MonoBehaviour
{
        public AudioClip[] sound;
void Start () {

}

  void Update () {

}
       public void SoundPlay(int number)
    {
        audio.clip = sound[number];
        audio.Play();
          }
    public void SoundPlay(AudioClip clip)
    {
        audio.clip = clip;
        audio.Play();
         }
}


-------------------------------------------------------

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{

   
    //宣告music陣列放置音樂檔
    public AudioClip[] music;

    //宣告musicObject
    public GameObject musicObject;

    public GameObject ani;                       
   
  
    void Start()
    {

     

    }

    //吁叫Sound裡SoundPlay()

    void Update()
    {
      
      for(int i=0;i<3;i++)
      {
        musicObject.GetComponent<Sound>().SoundPlay(music);
      }
     }[/code]
发表于 2017-4-18 10:05 | 显示全部楼层
很不错
发表于 2017-4-18 09:57 | 显示全部楼层
楼主是超人
发表于 2017-4-18 09:44 | 显示全部楼层
顶顶多好
发表于 2017-4-18 09:56 | 显示全部楼层
真心顶
发表于 2017-4-18 09:59 | 显示全部楼层
难得一见的好帖
发表于 2017-5-6 15:17 | 显示全部楼层
楼主是超人
发表于 2017-5-6 15:14 | 显示全部楼层
好帖就是要顶
发表于 2017-5-6 15:14 | 显示全部楼层
顶顶多好
发表于 2017-5-6 14:38 | 显示全部楼层
真心顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-3 23:19 , Processed in 0.112900 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表