- Home /
Question by
DustyScreen · Feb 13, 2015 at 02:35 PM ·
editorprojectselection
How to obtain selected files in the Project-window?
I’m trying to make an editorscript that detects which files are selected in the Project-window and populate them in an array or list, when creating a GameObject. I have the following code:
[MenuItem("GameObject/Custom/AudioClipShuffler")]
static void CreateAudioClipShuffler()
{
GameObject go = new GameObject("AudioClipShuffler");
AudioClipShuffler audioClipShuffler = go.AddComponent<AudioClipShuffler>();
// Get whatever is selected in the Project-window as an array or list
// Filter down to only an array or list of AudioClips (called audioClips)
audioClipShuffler.SetAudioClips(audioClips); //SetAudioClips takes an array or list
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
Selection.activeObject = go;
}
(inspired by http://docs.unity3d.com/ScriptReference/MenuItem.html)
Can anyone please help me with the two steps I’m missing? Thanks so much in advance!
Comment
Best Answer
Answer by DustyScreen · Feb 16, 2015 at 12:03 PM
Okay - found out myself :) No big deal, really:
List<AudioClip> audioClips = new List<AudioClip>();
foreach (Object o in Selection.objects)
{
if (o.GetType() == typeof(AudioClip))
{
audioClips.Add((AudioClip)o);
}
}
Your answer
Follow this Question
Related Questions
Multiple object Selection in project view: Unity shows wrong type 0 Answers
Repainting while performing asset operations 5 Answers
How to highlight or select an asset in project window from editor script? 2 Answers
Child selection forces parent selection 1 Answer
Editor scripting, select a field 1 Answer