Advertisement
Cookie042

Microphone Device Drawer unity

Apr 7th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1.  
  2.     [System.Serializable]
  3.     public struct MicrophoneDevice
  4.     {
  5.         public int id;
  6.         public string device;
  7.     }
  8.  
  9. #if UNITY_EDITOR
  10.     [CustomPropertyDrawer(typeof(MicrophoneDevice))]
  11.     public class MicrophoneDeviceDrawer : PropertyDrawer
  12.     {
  13.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  14.         {
  15.             var micDevProp = property.FindPropertyRelative("device");
  16.             var micIdProp = property.FindPropertyRelative("id");
  17.  
  18.             int selectedIndex = 0;
  19.             for (int i = 0; i < Microphone.devices.Length; i++)
  20.             {
  21.                 if (micDevProp.stringValue == Microphone.devices[i])
  22.                 {
  23.                     selectedIndex = i;
  24.                     break;
  25.                 }
  26.             }
  27.  
  28.             EditorGUI.BeginChangeCheck();
  29.             selectedIndex = EditorGUI.Popup(position, selectedIndex, Microphone.devices);
  30.             if (EditorGUI.EndChangeCheck())
  31.             {
  32.                 micDevProp.stringValue = Microphone.devices[selectedIndex];
  33.                 micIdProp.intValue = selectedIndex;
  34.             }
  35.  
  36.             property.serializedObject.ApplyModifiedProperties();
  37.         }
  38.  
  39.         public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  40.         {
  41.             return EditorGUIUtility.singleLineHeight;
  42.         }
  43.     }
  44.  
  45. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement