0
\$\begingroup\$

So I am trying to make a button that will spawn an object field and a float field when clicked. Each object field being unique, so I can add gameobjects to it. I can’t think of a way to do this. This is purely Editor Scripting. Also, if there is an option to delete a row (the object field and the FloatField) that would be amazing

\$\endgroup\$
1
\$\begingroup\$

Here's some quick code I wrote down. Hope this solves the issue you were having.

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SomethingEditor : EditorWindow {

    private static List<Object> objs = new List<Object>();
    private static List<float> floats = new List<float>();
    private static Vector2 v = Vector2.zero;

    [MenuItem("MenuItem/Test")]
    private static void DoIt () { EditorWindow.GetWindow<SomethingEditor>(); }

    void OnGUI () {
        EditorGUILayout.BeginScrollView(v);

        if(GUILayout.Button("Create an object")) {
            floats.Add(default(float));
            objs.Add(default(Object));
        }

        for (var i = 0; i < objs.Count; i++) {
            objs[i] = EditorGUILayout.ObjectField(objs[i], typeof(Object), false);
            floats[i] = EditorGUILayout.FloatField(floats[i]);
        }

        if(GUILayout.Button("Print")) {
            for (var i = 0; i < objs.Count; i++) {
                Debug.Log(objs[i]);
                Debug.Log(floats[i]);
            }
        }
        EditorGUILayout.EndScrollView();
    }
}

You can obviously make changes to make the editor look better, but you can have something like this.

\$\endgroup\$
  • \$\begingroup\$ Any thoughts on how to do the remove part? \$\endgroup\$ – Joe Robert Jul 15 at 22:31
  • \$\begingroup\$ You can remove the last item from the list, or you can pass a specific index, and you can remove the entry at that index. It would be similar code as the first if clause \$\endgroup\$ – Shraa1 Jul 16 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.