Search code examples
c#unity-game-engineunity3d-editorunity-editor

Why in editorwindow I don't see anymore the save/load buttons and there is no button to select prefab at the EditorGUI.ObjectField?


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

public class SaveTransformsInfo : EditorWindow
{
    public GameObject source;

    [MenuItem("Tools/Save Transforms")]
    private static void CreateReplaceWithPrefab()
    {
        const int width = 340;
        const int height = 420;

        var x = (Screen.currentResolution.width - width) / 2;
        var y = (Screen.currentResolution.height - height) / 2;

        GetWindow<SaveTransformsInfo>().position = new Rect(x, y, width, height);
    }

    private void OnGUI()
    {
        //EditorGUILayout.BeginHorizontal();
        source = (GameObject)EditorGUI.ObjectField( new Rect(10, 30, position.width - 1, 20),
            "Select Prefab", source, typeof(GameObject), true);
        //EditorGUILayout.EndHorizontal();

        if (Selection.transforms.Length > 0)
        {
            if (GUILayout.Button("Save Transforms"))
            {
                TransformSaver.SaveTransform(Selection.transforms);
            }

            if (GUILayout.Button("Load Transforms"))
            {
                TransformSaver.LoadTransform(true, source, false, "");
            }
        }
    }
}

Screenshot :

Mess

The save/load buttons not show.

The ObjectField the text description and the field too far from each other.

I can't select a prefab or a gameobject. There should be a small dot near the field pressing on it should open a window with all prefabs/gameobjects.

How can I organize all that ?

Doors


Solution

  • There should be a small dot near the field

    On the right edge of your picture you can still see the "dot" you are missing. It is simply too far on the right!


    That is caused by you using EditorGUI.ObjectField with a fixed Rect for its position and size that simply doesn't fit into the window size. Mostly it is caused by

    position.width - 1
    

    this - 1 gives you 1 pixel .. which is the 1 pixel of the dot you can see in your Screenshot. You could use

    position.width - 10
    

    so you can see the full dot. (I would then also recommend to not use 20 for the field height but rather EditorGUIUtility.singleLineHeight).

    so like

    source = (GameObject)EditorGUI.ObjectField(new Rect(10, 30, position.width - 10, EditorGUIUtility.singleLineHeight), "Select Prefab", source, typeof(GameObject), true);
    

    However this will still behave strange once the width gets actually smaller then the x position of the field. Also note that you can't mix EditorGUI and GUILayout .. your buttons would be displayed on top of the ObjectField. So you would also need to use GUI.Button with accordingly calculated Rects .. I wouldn't recommend it.


    Instead you should rather simply use the EditorGUILayout variant which uses the automatic Inspector Layout

    source = (GameObject)EditorGUILayout.ObjectField("Select Prefab", source, typeof(GameObject), true);
    

    enter image description here

    enter image description here


    The ObjectField the text description and the field too far from each other.

    The distance between the automatic label and the field could be changed by using EditorGUIUtility.labelWidth and set a shorter width (you'll either have to calculate it or find the best one via trial and error) e.g.

    EditorGUIUtility.labelWidth = 80;
    source = (GameObject)EditorGUILayout.ObjectField("Select Prefab", source, typeof(GameObject), true);
    

    enter image description here

    Note however that this will apply to the entire EditorWindow not only that specific field because usually you want all your fields start aligned.

    Or alternatively if you don't want to affect all fields you can do what apparently you tried already and use BeginHorizontal and EndHorizontal and move the label to a LabelField with fixed width

    EditorGUILayout.BeginHorizontal();
    EditorGUILayout.LabelField("Select Prefab", GUILayout.Width(80));
    source = (GameObject)EditorGUILayout.ObjectField(source, typeof(GameObject), true, GUILayout.ExpandWidth(true));
    EditorGUILayout.EndHorizontal();
    

    Note here, however, that this label is no longer "linked" to the field → it won't turn blue when the field is selected nor bold when it is changed.


    Finally to

    The save/load buttons not show.

    the code says

    if (Selection.transforms.Length > 0)
    

    so my guess is that you don't have anything selected in the Scene

    see Selection.transforms

    Returns the top level selection, excluding Prefabs.

    This is the most common selection type when working with Scene objects.

    once you have it should work just fine:

    enter image description here