- Home /
Align custom sprite custom inspector to the left
I have an object field custom inspector that shows a preview of the object sprite. I can not figure out how to align the preview to the left. It does not have a label, just a blank string, but If I remove that it does not show the preview at all. Inspector code is below
![public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
GridComponent = (DCCGrid)target;
serializedObject.Update();
EditorStyles.foldout.fontStyle = FontStyle.Bold;
ShowCellList = EditorGUILayout.Foldout(ShowCellList, "Cell List");
EditorStyles.foldout.fontStyle = standardEditorFontStyle;
if(ShowCellList)
{
EditorGUI.indentLevel = EditorGUI.indentLevel + 1;
ScrollPosition = EditorGUILayout.BeginScrollView(ScrollPosition, GUILayout.Height(200));
List<GameObject> cellObjectList = new List<GameObject>(GridComponent.CellObjectList);
foreach (GameObject cellObject in cellObjectList)
{
Transform buttonTransform = cellObject.transform.FindChild("Button Image");
Image buttonImage = buttonTransform.GetComponent<Image>();
GridComponent.Editor_IsFoldoutOpen = EditorGUILayout.Foldout(GridComponent.Editor_IsFoldoutOpen, cellObject.name);
if (GridComponent.Editor_IsFoldoutOpen)
{
//EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
buttonImage.sprite = (Sprite)EditorGUILayout.ObjectField("", buttonImage.sprite, typeof(Sprite), allowSceneObjects: false);
EditorGUILayout.EndHorizontal();
Button buttonScript = cellObject.GetComponent<Button>();
SerializedObject serializableButtonScript = new SerializedObject(buttonScript);
EditorGUILayout.BeginHorizontal();
SerializedProperty onClickEvent = serializableButtonScript.FindProperty("m_OnClick");
EditorGUILayout.PropertyField(onClickEvent, true);
EditorGUILayout.EndHorizontal();
}
}
EditorGUILayout.EndScrollView();
EditorGUI.indentLevel = EditorGUI.indentLevel - 1;
}
serializedObject.ApplyModifiedProperties();
EditorGUI.EndChangeCheck();
}
EditorGUI.EndChangeCheck();
}][1]
[1]: /storage/temp/91284-snip.png
Answer by Agustin0987 · May 11, 2020 at 01:07 AM
@NoiseyAgent I know is three years later, but I found a way to achieve this and I wanted to share my code just in case someone needs it in the future.
How I managed to align the EditorGUILayout.ObjectField
to the left, is by using the GUILayout.Width()
options, as follows.
spriteVar = (Sprite)EditorGUILayout.ObjectField("Sprite", spriteVar, typeof(Sprite), false, GUILayout.Width(214));
Using a value of 214 seems to work for me, independently of what the size of the editor window or custom editor has. A value lower than 214 will just make the sprite preview box smaller. A value larger than 214 will move the sprite preview box farther from the label as the value grows.
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Script Editors for Unity 3 Answers
My android app build has more bugs on it than the editor/deveploment version 0 Answers