Search Unity

MinMaxSlider behaving badly when clicking outside range bar

Discussion in 'Scripting' started by colinday, Oct 10, 2012.

  1. colinday

    colinday

    Joined:
    Jan 9, 2012
    Posts:
    25
    I'm having an issue with the MinMaxSlider in a custom editor going to infinity / NaN when I *sometimes* click *outside* of the range bar. It behaves correctly for the most part, but when I switch the inspector to another object and then come back to this one and click outside the range bar, the range bar disappears and if I drag around the min/max values blow out and go to NaN.

    Here's the code for my simple custom editor with the Sound object I'm trying to write the editor for.

    Any ideas? It really feels like a bug in the Min/Max slider to me ... or I'm missing some crucial setup or piece of understanding about custom editors. I'm using Unity 3.5.6 on OSX.

    Code (csharp):
    1.  
    2.  
    3. // ------------------------------------------------------------------------------------------------
    4. public class Sound : MonoBehaviour
    5. {
    6.        
    7.     // the clip to play
    8.     public AudioClip AudioClip;
    9.    
    10.     // volume
    11.     public float Volume = 1.0f;
    12.  
    13.     // pitch
    14.     public float PitchMin = 1.0f;
    15.     public float PitchMax = 1.0f;
    16.            
    17. }
    18.  
    19.  
    20. using AssemblyCSharp;
    21. using UnityEditor;
    22. using UnityEngine;
    23.  
    24. // ------------------------------------------------------------------------------------------------
    25. [CustomEditor(typeof(Sound))]
    26. [CanEditMultipleObjects]
    27. public class SoundEditor : Editor
    28. {
    29.  
    30.     // properties
    31.     private SerializedProperty m_audioClipProp;
    32.     private SerializedProperty m_volumeProp;
    33.     private SerializedProperty m_pitchMinProp;
    34.     private SerializedProperty m_pitchMaxProp;
    35.    
    36.     // --------------------------------------------------------------------------------------------
    37.     void OnEnable()
    38.     {
    39.         m_audioClipProp = serializedObject.FindProperty( "AudioClip" );
    40.         m_volumeProp = serializedObject.FindProperty( "Volume" );
    41.         m_pitchMinProp = serializedObject.FindProperty( "PitchMin" );
    42.         m_pitchMaxProp = serializedObject.FindProperty( "PitchMax" );
    43.     }
    44.        
    45.     // --------------------------------------------------------------------------------------------
    46.     public override void OnInspectorGUI()
    47.     {
    48.            
    49.         // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
    50.         serializedObject.Update();
    51.  
    52.         // audio clip                  
    53.         AudioClip audioClip = m_audioClipProp.objectReferenceValue as AudioClip;
    54.         audioClip = EditorGUILayout.ObjectField( "Audio Clip", audioClip, typeof( AudioClip ), false ) as AudioClip;
    55.         if (GUI.changed)
    56.         {
    57.             m_audioClipProp.objectReferenceValue = audioClip;
    58.         }
    59.                    
    60.         // volume
    61.         EditorGUILayout.Slider( m_volumeProp, 0.0f, 1.0f );
    62.        
    63.         // pitch
    64.         float min = m_pitchMinProp.floatValue;
    65.         float max = m_pitchMaxProp.floatValue;
    66.         GUIContent pitchLabel = new GUIContent( string.Format( "Pitch [Min:{0:F1}, Max:{1:F1}]", min, max ) );
    67.         EditorGUILayout.MinMaxSlider( pitchLabel, ref min, ref max, -3.0f, 3.0f );
    68.         if (GUI.changed)
    69.         {
    70.             m_pitchMinProp.floatValue = min;
    71.             m_pitchMaxProp.floatValue = max;
    72.         }
    73.                
    74.         // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
    75.         serializedObject.ApplyModifiedProperties();
    76.        
    77.     }
    78.    
    79. }
    80.  
    81.