EditorGUILayout labels are "fixed"...

I’m having a spot of bother with EditorGUILayout. If I create my own inspector it doesn’t format as nicely as the built in inspectors, and the labels get cut off on if the label is long.

Rigidbody is built-in and LootableObject is custom.

My long labels are cut off - take “Animate When Looting” for example, yet in the built-in inspector “Collision Detection” is not.

I’ve been spelunking into the Editor docs, and not yet found joy. How to I “unfix” the label length? I’ve tried a separate “EditorGUILayout.PrefixLabel” and it behaves the same.

The other piece of behavior that I’m finding “different” is how the Custom Inspector recalculates it’s layout when the inspector window is adjusted:

The built-in one breaks between the label and the value, but the custom one keeps the label attached to the value, and it’s the value that’s recalculated…

[edit]

The code involved is:

//	LootableBaseInspector.cs
//	Allows clean and easy editing of lootable items
//	Place in the Editor folder and forget

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

[CustomEditor(typeof(LootableBase))]

public class LootableBaseInspector : Editor {
	
	protected LootableBase thisTarget;
	
	public override void OnInspectorGUI() {
		thisTarget = (LootableBase)target;
		thisTarget.isLootable = EditorGUILayout.Toggle("Is Lootable", thisTarget.isLootable);
		thisTarget.animateWhenLooting = EditorGUILayout.Toggle("Animate When Looting", thisTarget.animateWhenLooting);
		if (thisTarget.animateWhenLooting)
			thisTarget.thisAnimation = EditorGUILayout.ObjectField("Animation", thisTarget.thisAnimation, typeof (Animation)) as Animation;
		thisTarget.destroyWhenEmpty = EditorGUILayout.Toggle("Destroy When Empty", thisTarget.destroyWhenEmpty);
		if (thisTarget.destroyWhenEmpty){
			thisTarget.delayBeforeDestroying = EditorGUILayout.FloatField("Delay Before Destroying",thisTarget.delayBeforeDestroying);
			thisTarget.fadeBeforeDestroying = EditorGUILayout.Toggle("Fade Before Destroying",thisTarget.fadeBeforeDestroying);
			thisTarget.fadeDuration = EditorGUILayout.FloatField("Fade Duration",thisTarget.fadeDuration);
		}
		
	    if (GUI.changed) 
	        EditorUtility.SetDirty(target);
	}
}

	
// ©2011 Adam Buckner (aka: Little Angel) and theantranch.com (mailto: adam@theantranch.com)
// Not for reuse or resale without permission.

EditorGUIUtility.LookLikeControls(150); <— this is the command you are looking for, 150 is pixel width between label and the inspector thing

Ah! Thanks for the clue!

I found that:

EditorGUIUtility.LookLikeInspector();

was the answer.

This:

EditorGUIUtility.LookLikeControls(150.0f);

made a better fixed width label (in the above case fixed to 150…), but the LookLikeInspector() allowed the field to simply behave like an inspector.

I marvel at the fact that you need to TELL an inspector to “look like an inspector”. Is that like saying a duck should look like a duck?

Thanks again for the clue! I missed the EditorGUIUtility branch altogether.

QFT. Unity really seems like a hatch job in a few areas… :expressionless:

LooklikeControls is depreciated.
Try this instead :
EditorGUIUtility.labelWidth = 60f;

4 Likes