Drag and Drop into Custom Inspector ObjectField not Working
Hello, I'm creating a custom inspector for my audio player/visualizer for an assignment at uni and have got this far. Now I'm having an issue with dragging and dropping an AudioClip into the clip into the Custom Inspector AudioClip objectfield.
I think it's something to do with it not updating the am.audioClips[count].clip in the for loop. I can drop the AudioClips into the default inspector below and it updates the custom inspector but not vice versa. You can select the object in the custom inspector but can't delete it by pressing delete on keyboard.
I'm sure it's fairly simple but any help would be greatly appreciated!
using UnityEngine;
using UnityEditor;
// Reference to the script that this custom inspector script affects.
[CustomEditor(typeof(AudioManager))]
[CanEditMultipleObjects]
public class CustomAudioTrackInspector : Editor
{
AudioManager am;
// Launches when script is fired.
void OnEnable()
{
am = (AudioManager)target;
}
public override void OnInspectorGUI()
{
// Label at top of the AudioManager.
GUILayout.Label("This script handles the audio functions, i.e. Play, \nPause and Stop. Audio clips are also added here.");
GUILayout.Label("Total Audio Clips: " + am.audioClips.Count);
if (GUILayout.Button("Add AudioClip"))
AddAudioClip();
for(int count = 0; count < am.audioClips.Count; count++)
{
GUILayout.BeginHorizontal();
if (GUILayout.Button("Remove AudioClip"))
RemAudioClip(count);
//am.audioClips[count].clip = Selection.activeObject as Object;
am.audioClips[count].name = GUILayout.TextField(am.audioClips[count].name, GUILayout.Width(100));
// Displays correctly and accepts audioclips but still says none after dropping a clip in.
EditorGUILayout.ObjectField(am.audioClips[count].clip, typeof(AudioClip), true);
GUILayout.EndHorizontal();
}
GUILayout.Space(20);
base.OnInspectorGUI();
}
// In-Editor function to add an AudioClip to the audioClips list.
void AddAudioClip()
{
am.audioClips.Add(new Audio());
}
// In-Editor function to remove an AudioClip from the audioClips list.
void RemAudioClip(int index)
{
am.audioClips.RemoveAt(index);
}
}
If i put this instead am.audioClips[count].clip = EditorGUILayout.ObjectField(am.audioClips[count].clip, typeof(AudioClip), true);
which seems logical i get an error saying it cant convert type 'UnityEngine.Object' to 'UnityEngine.Audioclip'.
Answer by justDeek · Dec 22, 2017 at 09:54 PM
You have to cast the whole EditorGUILayout.ObjectField(); for it to specify as that object type.
In your case:
am.audioClips[count].clip = (AudioClip)EditorGUILayout.ObjectField(am.audioClips[count].clip, typeof(AudioClip), true);
Perfect, just what I was looking for! Useful knowledge for the future, thank you very much.
Your answer
Follow this Question
Related Questions
Please help! Audio not working in another class.. 0 Answers
play sound from an array 2 Answers
How to make drag-drop detecting Editor script? 0 Answers
Arrays - picking a random object 0 Answers