I have an editor script with a button that adds a Script to the gameObject. If multiple objects are selected, the script is added to the first one only. Here’s my script
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor ( typeof(SomeScript) ),CanEditMultipleObjects]
public class SomeScriptEditor : Editor {
public override void OnInspectorGUI(){
EditorGUILayout.LabelField( "Node adder" );
if( GUILayout.Button("Add Node") ) {
SomeScript go = target as SomeScript;
Node node = go.GetComponent<Node>();
if( node == null ) {
go.gameObject.AddComponent("Node");
}
}
}
}
Am I doing it the wrong way? Shouldn’t the editor script run for every separate object?
Uhm, since the ability exists to edit multiple objects at once the Editor class got extended with targets. Sometimes it helps to just browse through the documentaion and looking for something that might be useful ^^
if( GUILayout.Button("Add Node") )
{
foreach(var obj in targets)
{
SomeScript go = obj as SomeScript;
if (go == null)
continue;
Node node = go.GetComponent<Node>();
if( node == null )
{
go.gameObject.AddComponent("Node");
}
}
}