1 Want a button that fits in the center of the window
// 获取当前屏幕的矩形大小
Rect screenRect = GUILayoutUtility.GetLastRect();
// 创建一个矩形区域,固定在屏幕顶部
GUILayout.BeginArea(new Rect(screenRect.x + position.width / 2 - 150 / 2, screenRect.y + count * EditorGUIUtility.singleLineHeight + 40, 150, 50), new GUIContent("这是区域"), new GUIStyle());
//在矩形区域内添加其他内容
if (GUILayout.Button("打包!", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)))
{
// Do something when the button is clicked
}
// 结束矩形区域的绘制
GUILayout.EndArea();
2 want to get the height of a component
using UnityEditor;
using UnityEngine;
public class MyEditorWindow : EditorWindow {
private SerializedProperty myProperty;
void OnEnable() {
// 获取要显示的 SerializedProperty 对象
myProperty = serializedObject.FindProperty("myPropertyName");
}
void OnGUI() {
// 使用 EditorGUI.GetPropertyHeight 方法获取组件的高度
float height = EditorGUI.GetPropertyHeight(myProperty, true);
// 在 GUI 中绘制组件
EditorGUILayout.PropertyField(myProperty, GUILayout.Height(height));
}
}
3 components want indentation
++EditorGUI.indentLevel; GUILayout.Label("------"); EditorGUILayout.PropertyField(pointCount,new GUIContent("number of vertices"));
4 Want to redraw the gui
set dirty mark
then save the changes
if (GUI.changed)
{
EditorUtility.SetDirty(target);
this.serializedObject.ApplyModifiedProperties();
}