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

Replicate AudioSource Inspector

Discussion in 'Immediate Mode GUI (IMGUI)' started by chlorosoft, Jun 22, 2018.

  1. chlorosoft

    chlorosoft

    Joined:
    Oct 24, 2017
    Posts:
    4
    Hello,

    We are a videogame development studio and also we are developing assets for Asset store.

    We are currently developing a tool to manage the audio. This tool has a settings window, in this windows the developar can configure all audioclip settings (the same settings that you can find in a AudioSource).
    We want to implement the 3D sound settings that are in the AudioSoruce also in the window.

    We have tried:
    - Make an AnimationCurve with multiple curves but we have not found anything.
    - Get the unity editor class of the AudioSource but this uses a lot of internal classes and properties.

    Would there be any way to draw the AudioSource inspector as Unity does in our custom inspector or other solution?

    Thanks
     
  2. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    The Odin inspector asset has something called inline inspectors where you can just add on to the audio inspector...
     
  3. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
  4. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
  5. chlorosoft

    chlorosoft

    Joined:
    Oct 24, 2017
    Posts:
    4
    First I use Editor.CreateEditor to get the AudioSource editor and then I access the Audio3DGUI method that is responsible for drawing the part of the 3D audio. This was achieved thanks to the reflection in the code part:

    Code (CSharp):
    1.  
    2.  
    3.                tmpEditor.serializedObject.Update();
    4.  
    5.                MethodInfo methodAudio = tmpEditor.GetType().GetMethod(
    6.                    "Audio3DGUI", BindingFlags.NonPublic | BindingFlags.Instance
    7.                );
    8.  
    9.                methodAudio.Invoke(tmpEditor, null);
    10.  
    11.                tmpEditor.serializedObject.ApplyModifiedProperties();
    At this moment I have another problem. To apply the curves in AudioSource I use the SetCustomCurve and GetCustomCurve methods (from AudioSoruce class) with the animation curves that I saved in an external class.
    Sometimes during this stage some animations curves are lost and I get an error of missing keys of the animation curve of the final AudiosSource.

    My Unity version is 5.4.2f2.

    Thank you
     
    Last edited: Jul 2, 2018
  6. TenderclawWill

    TenderclawWill

    Joined:
    Jun 14, 2018
    Posts:
    6
    Hate to necro such an old thread but did you ever find a full solution to this. I am similarly trying to recreate this through reflection and am running into a lot of issues. Doubt anyone is still reading this but would be nice
     
  7. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,297
    @TenderclawWill
    If your aim is to draw the entire AudioSource editor you can simply use Editor.OnInspectorGUI. No need to use any reflection.
     
  8. TenderclawWill

    TenderclawWill

    Joined:
    Jun 14, 2018
    Posts:
    6
    My aim is to just draw the 3d fall off graph and nothing else.

    i have it working with reflection like so.


    Code (CSharp):
    1.         private void DrawAudioSource3DSettings() {
    2.             audioSource.minDistance = minDistance.floatValue;
    3.             audioSource.maxDistance = maxDistance.floatValue;
    4.             audioSource.rolloffMode = (AudioRolloffMode) rollOffMode.enumValueIndex;
    5.  
    6.             audioSourceEditor.serializedObject.Update();
    7.  
    8.             MethodInfo methodDrawAudio3D = audioSourceEditor.GetType().GetMethod("Audio3DGUI", BindingFlags.NonPublic | BindingFlags.Instance);
    9.  
    10.             methodDrawAudio3D.Invoke(audioSourceEditor, null);
    11.  
    12.             audioSourceEditor.serializedObject.ApplyModifiedProperties();
    13.  
    14.             minDistance.floatValue = audioSource.minDistance;
    15.             maxDistance.floatValue = audioSource.maxDistance;
    16.             rollOffMode.enumValueIndex = (int)audioSource.rolloffMode;
    17.             doppler.floatValue = audioSource.dopplerLevel;
    18.             spread.floatValue = audioSource.spread;
    19.             customFalloffCurve.animationCurveValue = audioSource.GetCustomCurve(AudioSourceCurveType.CustomRolloff);
    20.         }
    which works pretty well. Where it fails is when i try to use a custom curve and drag around the values i get a null on something in the reflected function, but because its reflection its pretty hard to tell what.

    its a null in a collection (probably a list) in the OnMouseDrag event in the curve editor, but its hard for me to figure out more from there.
     
  9. TenderclawWill

    TenderclawWill

    Joined:
    Jun 14, 2018
    Posts:
    6
    It seems to have fixed itself and i have no idea why. you can close this thread. Maybe i had a bad thing serialized that was breaking it earlier than i thought previously.
    upload_2020-4-20_15-18-32.png

    I wouldn't be surprised if the underlying issue is still there. if i find the actual real fix i can post it here.
     
  10. TenderclawWill

    TenderclawWill

    Joined:
    Jun 14, 2018
    Posts:
    6
  11. TenderclawWill

    TenderclawWill

    Joined:
    Jun 14, 2018
    Posts:
    6
    So just for future people reading, I figured out what was breaking it earlier.

    My backend object im calling the reflection on (audioSource in my code sample above) was previously an instantiated game object flagged to hide in inspector and not save. for whatever reason, when the base game object is flagged to not save, the curve is uneditable, but if you flag it as just hide in inspector and manually clean it up, it works fine (either with an onsave or ondisable hook, onsave probably safer)

    I would really prefer to get this working with the hide in inspector and dont save flag, but this is fine for now as im not sure if that is even possible.
     
    Last edited: Apr 21, 2020
    BeeNvizzio and SisusCo like this.
  12. SangOk1994

    SangOk1994

    Joined:
    Jun 11, 2020
    Posts:
    2