【问题标题】:Unity3D AudioSource not being attached to Object?Unity3D AudioSource 未附加到对象?
【发布时间】:2017-04-30 23:05:12
【问题描述】:

那么,嘿。我的对象附加了一个音频源组件。我还从here 复制了脚本并将其附加到我的对象上。

 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(AudioSource))]
 public class ExampleClass : MonoBehaviour
 {
     public AudioClip impact;
     AudioSource audio;

     void Start()
     {
         audio = GetComponent<AudioSource>();
     }

     public void play_sound()
     {
         audio.PlayOneShot(impact, 0.7F);
     }
 }

但是当我尝试在我的脚本中使用音频时,我把它剪掉了

有什么问题?

【问题讨论】:

    标签: c# unity3d audio-source


    【解决方案1】:

    您继承自 MonoBehaviour,它继承自 Behaviour,然后从 Component 继承,其中声明了一些变量,例如:

    public Component animation { get; }
    public Component audio { get; }
    public Component camera { get; }
    public Component collider { get; }
    public Component collider2D { get; }
    public Component constantForce { get; }
    public Component guiElement { get; }
    public Component guiText { get; }
    public Component guiTexture { get; }
    public Component hingeJoint { get; }
    public Component light { get; }
    public Component networkView { get; }
    public Component particleEmitter { get; 
    public Component particleSystem { get; }
    public Component renderer { get; }
    public Component rigidbody { get; }
    public Component rigidbody2D { get; }
    

    这些都已弃用,并且不再允许在最新版本的 Unity 中使用。我相信它从 Unity 5 开始就被弃用了。您链接的文档尚未更新。如需更多信息,请参阅this 帖子。

    您所要做的就是将您的音频变量重命名为其他名称,一切正常。

    public AudioClip impact;
    AudioSource myAudio;
    
    void Start()
    {
        myAudio = GetComponent<AudioSource>();
    }
    
    public void play_sound()
    {
        myAudio.PlayOneShot(impact, 0.7F);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-14
      • 2015-08-30
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 2021-02-27
      相关资源
      最近更新 更多