Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Add a script component when adding an AudioSource to GameObject

Discussion in 'Editor & General Support' started by luisch125, Feb 18, 2020.

  1. luisch125

    luisch125

    Joined:
    Jul 10, 2015
    Posts:
    35
    Hi. Well, I don't know if that's easy/possible. I just want to add a certain script to any GameObject when I add an AudioSource component.

    Is that possible?

    Is maybe easier to add a menu option to the editor that creates a GameObject with both the AudioSource and the required script? In that case, how should I do that (I know that I can require a component when I create a class, so that's probably the way to go...)?

    Thanks!
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    do manually, easier
     
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,760
    To add custom menu items,
    Create a script and place it into a folder called "Editor" somewhere in your Assets folder.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public static class ToolsMenu
    6. {
    7.     [MenuItem ("Tools/Add Audio Source")]
    8.     public static void AddAudioSource(){
    9.  
    10.         var go = new GameObject("MyGameObject"); //Create new GameObject
    11.         go.AddComponent<AudioSource>(); //Add an AudioSource component
    12.         go.AddComponent<MyScript>();//Add you script component
    13.     }
    14. }
    15.  
     
  4. luisch125

    luisch125

    Joined:
    Jul 10, 2015
    Posts:
    35
    Thanks, I think that's the way to go. Do you know if it could be done the other way?: when adding an AudioSource component automatically add my script. What I want is that everyone on the project can configure the audio gameobjects using my script but without the worry of manually adding it.
     
  5. luisch125

    luisch125

    Joined:
    Jul 10, 2015
    Posts:
    35
    Yeah, thanks for nothing!
     
  6. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,295
    If you want to 100% ensure that your script is found on all GameObjects with the AudioSource component you could subscribe to EditorApplication.hierarchyChanged and check all GameObjects with the AudioSource component manually whenever the scene changes.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [InitializeOnLoad]
    5. public static class AudioSourceHelperAdder
    6. {
    7.     static AudioSourceHelperAdder()
    8.     {
    9.         EditorApplication.hierarchyChanged += OnHierarchyChanged;
    10.     }
    11.  
    12.     private static void OnHierarchyChanged()
    13.     {
    14.         foreach(var audioSource in Object.FindObjectsOfType<AudioSource>())
    15.         {
    16.             AudioSourceHelper found;
    17.             if(!audioSource.TryGetComponent(out found))
    18.             {
    19.                 audioSource.gameObject.AddComponent<AudioSourceHelper>();
    20.             }
    21.         }
    22.     }
    23. }
    This approach does introduce a little bit of overhead every time you change something in your scene. Probably won't be noticeable unless you have huge scenes.


    Another reasonably reliable way to automate adding the component would be to create a custom Editor for the AudioSource component and ensure that your script is found on the target GameObject during OnEnable. This method would work as long as any GameObjects with the AudioSource component are viewed in the inspector at least once, which is pretty safe thing to assume.


    If the component doesn't need to be added immediately in edit mode but just needs to exist in play mode and in builds, you could also use the PostProcessSceneAttribute to add the component.
     
  7. luisch125

    luisch125

    Joined:
    Jul 10, 2015
    Posts:
    35
    Thanks for your nice answer! I see that I've been "underusing" the unity editor :) My point is that I'd like my script to be added whenever the AudioSource is added to a GameObject so we don't forget to select a few things in that script.

    The script needs to be added before runtime because there are a couple things that need to set manually. In fact, the script changes some options on the AudioSource, that's why I need it to be added as soon as the AudioSource is added to the GameObject. And that's why I think that the menu option is a good solution.

    In any case, I see a lot of potential on your answer for other uses.

    Thanks a lot!
     
    SisusCo likes this.