EditorGUILayout 各類功能表現


BeginFadeGroup -設定起始消失區
會與 EndFadeGroup一組
       
void OnGUI()
{
m_ShowExtraFields.target = EditorGUILayout.ToggleLeft("Show extra fields", m_ShowExtraFields.target);

//Extra block that can be toggled on and off.
if (EditorGUILayout.BeginFadeGroup(m_ShowExtraFields.faded))
{
EditorGUI.indentLevel++;
EditorGUILayout.PrefixLabel("Color");
m_Color = EditorGUILayout.ColorField(m_Color);
EditorGUILayout.PrefixLabel("Text");
m_String = EditorGUILayout.TextField(m_String);
EditorGUILayout.PrefixLabel("Number");
m_Number = EditorGUILayout.IntSlider(m_Number, 0, 10);
EditorGUI.indentLevel--;
}
EditorGUILayout.EndFadeGroup();
}
效果
BeginHorizontal -設定一個橫向區域
會與 EndHorizontal一組
       
void OnGUI()
{
Rect r = EditorGUILayout.BeginHorizontal("Button");
if (GUI.Button(r, GUIContent.none))
Debug.Log("Go here");
GUILayout.Label("I'm inside the button");
GUILayout.Label("So am I");
EditorGUILayout.EndHorizontal();
}
效果
BeginHorizontal -設定一個拖曳區域
會與 EndScrollView一組
       
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
scrollPos =
EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(100), GUILayout.Height(100));
GUILayout.Label(t);
EditorGUILayout.EndScrollView();
if (GUILayout.Button("Add More Text", GUILayout.Width(100), GUILayout.Height(100)))
t += " \nAnd this is more text!";
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("Clear"))
t = "";
}
效果
BeginToggleGroup-設定一個Toggle區域
會與 EndToggleGroup一組,
       
void OnGUI()
{
posGroupEnabled = EditorGUILayout.BeginToggleGroup("Align position", posGroupEnabled);
pos[0] = EditorGUILayout.Toggle("x", pos[0]);
pos[1] = EditorGUILayout.Toggle("y", pos[1]);
pos[2] = EditorGUILayout.Toggle("z", pos[2]);
EditorGUILayout.EndToggleGroup();

rotGroupEnabled = EditorGUILayout.BeginToggleGroup("Align rotation", rotGroupEnabled);
rot[0] = EditorGUILayout.Toggle("x", rot[0]);
rot[1] = EditorGUILayout.Toggle("y", rot[1]);
rot[2] = EditorGUILayout.Toggle("z", rot[2]);
EditorGUILayout.EndToggleGroup();

scaleGroupEnabled = EditorGUILayout.BeginToggleGroup("Align scale", scaleGroupEnabled);
scale[0] = EditorGUILayout.Toggle("x", scale[0]);
scale[1] = EditorGUILayout.Toggle("y", scale[1]);
scale[2] = EditorGUILayout.Toggle("z", scale[2]);
EditorGUILayout.EndToggleGroup();

GUILayout.Space(30);
if (GUILayout.Button("Align!"))
Align();
}
效果
BeginVertical-設定一個直向區域
會與 EndVertical 一組,
       
void OnGUI()
{
Rect r = (Rect)EditorGUILayout.BeginVertical("Button");
if (GUI.Button(r, GUIContent.none))
Debug.Log("Go here");
GUILayout.Label("I'm inside the button");
GUILayout.Label("So am I");
EditorGUILayout.EndVertical();
}
效果
ColorField-設定一個顏色輸入區
       
void OnGUI()
{
matColor = EditorGUILayout.ColorField("New Color", matColor);

if (GUILayout.Button("Change!"))
ChangeColors();
}
效果
CurveField-設定一個曲線輸入區域
       
void OnGUI()
{
curveX = EditorGUILayout.CurveField("Animation on X", curveX);
curveY = EditorGUILayout.CurveField("Animation on Y", curveY);
curveZ = EditorGUILayout.CurveField("Animation on Z", curveZ);

if (GUILayout.Button("Generate Curve"))
AddCurveToSelectedGameObject();
}
效果
Box-設定一個盒狀區域
void OnGUI()
{
if (!tex)
{
Debug.LogError("Missing texture, assign a texture in the inspector");
}
GUILayout.Box(tex);
GUILayout.Box("This is an sized label");
}
效果
BeginHorizontal -設定一個按鈕區域
       
void OnGUI()
{
if (!tex)
{
Debug.LogError("No texture found, please assign a texture on the inspector");
}

if (GUILayout.Button(tex))
{
Debug.Log("Clicked the image");
}
if (GUILayout.Button("I am a regular Automatic Layout Button"))
{
Debug.Log("Clicked Button");
}
}
效果
BeginArea-設定一個區域,會與 EndArea一組。
       
void OnGUI()
{
GUILayout.BeginArea(new Rect(10, 10, 100, 100));
GUILayout.Button("Click me");
GUILayout.Button("Or me");
// Ends the area started above
GUILayout.EndArea();
}
效果
FlexibleSpace-設定一個彈性分隔空間區域
       
void OnGUI()
{
// Wrap everything in the designated GUI Area
GUILayout.BeginArea(new Rect(0, 0, 200, 60));
// Begin the singular Horizontal Group
GUILayout.BeginHorizontal();
// Place a Button normally
GUILayout.RepeatButton("A button with\ntwo lines");
// Place a space between the button and the vertical area
// so it fits the whole area
GUILayout.FlexibleSpace();
// Arrange two more Controls vertically beside the Button
GUILayout.BeginVertical();
GUILayout.Box("Value:" + Mathf.Round(sliderValue));
sliderValue = GUILayout.HorizontalSlider(sliderValue, 0.0f, 10f);

// End the Groups and Area
GUILayout.EndVertical();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
效果
各類EditorGUILayout相關表現 看圖左方的字就可以之道是哪一個功能
       
void OnGUI()
{
m_ShowExtraFields.target = EditorGUILayout.ToggleLeft("Show extra fields", m_ShowExtraFields.target);


//Extra block that can be toggled on and off.
if (EditorGUILayout.BeginFadeGroup(m_ShowExtraFields.faded))
{
EditorGUI.indentLevel++;
EditorGUILayout.PrefixLabel("Color");
m_Color = EditorGUILayout.ColorField(m_Color);
EditorGUILayout.PrefixLabel("Text");
m_String = EditorGUILayout.TextField(m_String);
EditorGUILayout.PrefixLabel("Number");
m_Number = EditorGUILayout.IntSlider(m_Number, 0, 10);
EditorGUI.indentLevel--;
}
EditorGUILayout.EndFadeGroup();


EditorGUI.indentLevel++;

EditorGUILayout.PrefixLabel("double");
m_double = EditorGUILayout.DoubleField(m_double);
EditorGUILayout.PrefixLabel("BoundsField");
m_bounds = EditorGUILayout.BoundsField(m_bounds);
EditorGUILayout.PrefixLabel("BoundsIntField");
m_boundsInt = EditorGUILayout.BoundsIntField(m_boundsInt);

GUILayout.Label("CurveField");
curveX = EditorGUILayout.CurveField("Animation on X", curveX);
curveY = EditorGUILayout.CurveField("Animation on Y", curveY);
curveZ = EditorGUILayout.CurveField("Animation on Z", curveZ);

m_double = EditorGUILayout.DelayedDoubleField("DelayedDoubleField",m_double);

m_float = EditorGUILayout.DelayedFloatField("DelayedFloatField",m_float);

m_Number = EditorGUILayout.DelayedIntField("DelayedIntField",m_Number);

m_String = EditorGUILayout.DelayedTextField("DelayedTextField",m_String);

m_double = EditorGUILayout.DoubleField("DoubleField",m_double);

//EditorGUILayout.PrefixLabel("DropdownButton");
//content = EditorGUILayout.DropdownButton(content, FocusType.Passive);

posGroupEnabled = EditorGUILayout.BeginToggleGroup("Align position", posGroupEnabled);
pos[0] = EditorGUILayout.Toggle("x", pos[0]);
pos[1] = EditorGUILayout.Toggle("y", pos[1]);
pos[2] = EditorGUILayout.Toggle("z", pos[2]);
EditorGUILayout.EndToggleGroup();


EditorGUILayout.PrefixLabel("EnumFlagsField Enum選擇");
m_Flags = (ExampleFlagsEnum)EditorGUILayout.EnumFlagsField(m_Flags);

EditorGUILayout.PrefixLabel("EnumPopup Enum選擇");
m_Flags = (ExampleFlagsEnum) EditorGUILayout.EnumPopup(m_Flags);

EditorGUILayout.PrefixLabel("FloatField");
m_float = EditorGUILayout.FloatField(m_float);

EditorGUILayout.PrefixLabel("Foldout 折疊區");
m_bool = EditorGUILayout.Foldout(m_bool, "Select a GameObject");



EditorGUILayout.PrefixLabel("HelpBox 顯示HelpBox"); //顯示HelpBox
EditorGUILayout.HelpBox("HelpBox", MessageType.None);
EditorGUILayout.HelpBox("HelpBox", MessageType.Warning);
EditorGUILayout.HelpBox("HelpBox", MessageType.Info);
EditorGUILayout.HelpBox("HelpBox", MessageType.Error);



EditorGUI.indentLevel--;


}
效果
各類EditorGUILayout相關表現2
       
void OnGUI()
{
op = (OPTIONS)EditorGUILayout.EnumPopup("Primitive to create:", op);
if (GUILayout.Button("Create"))
InstantiatePrimitive(op);

EditorGUILayout.PrefixLabel("其他功能");
m_int = EditorGUILayout.IntField("IntField",m_int);

EditorGUILayout.IntPopup("IntPopup",selectedSize, names, sizes);
EditorGUILayout.PrefixLabel("IntPopup value");
m_int2 = EditorGUILayout.IntField("IntField",m_int2);

m_int = EditorGUILayout.IntSlider("IntSlider", m_int, 0,10);

EditorGUILayout.LabelField("LabelField1", "LabelField2");

LayerFieldInt = EditorGUILayout.LayerField("Layer for Objects:", LayerFieldInt);

LongValue = EditorGUILayout.LongField("LabelField1", LongValue);

EditorGUILayout.MaskField("MaskField", m_int, names);

EditorGUILayout.PrefixLabel("MinMaxSlider");
EditorGUILayout.LabelField("Min Val:", minVal.ToString());
EditorGUILayout.LabelField("Max Val:", maxVal.ToString());
EditorGUILayout.MinMaxSlider("MinMaxSlider",ref minVal, ref maxVal, minLimit, maxLimit);
EditorGUILayout.ObjectField("ObjectField",source, typeof(Object), true);
m_String = EditorGUILayout.PasswordField("PasswordField", m_String);
index = EditorGUILayout.Popup("Popup", index, options);

EditorGUILayout.PrefixLabel("GetControlRect"); //控制Rect
m_rect = EditorGUILayout.GetControlRect(true, 10f);

m_rectpos = EditorGUILayout.RectField("RectField", m_rectpos);
m_RectInt = EditorGUILayout.RectIntField("RectIntField", m_RectInt);

EditorGUILayout.SelectableLabel("SelectableLabel");

m_float = EditorGUILayout.Slider("MinMaxSlider", m_float, minLimit, maxLimit);

EditorGUILayout.PrefixLabel("EditorGUILayout.Space");
EditorGUILayout.Space();

tagStr = EditorGUILayout.TagField("TagField", tagStr); ;

EditorGUILayout.Space();

GUILayout.Label("TextArea");
TextAreaStr = EditorGUILayout.TextArea("TextArea : Obj name ", TextAreaStr);


GUILayout.Label("TextField");
GUILayout.Label("Select an object in the hierarchy view");
if (Selection.activeGameObject)
Selection.activeGameObject.name =
EditorGUILayout.TextField("TextField Object Name: ", Selection.activeGameObject.name);
this.Repaint();

}
效果
各類EditorGUILayout相關表現3
       
public void OnGUI()
{
showPosition = EditorGUILayout.Foldout(showPosition, status);
if (showPosition)
if (Selection.activeTransform)
{
Selection.activeTransform.position =
EditorGUILayout.Vector3Field("Position", Selection.activeTransform.position);
status = Selection.activeTransform.name;
}

if (!Selection.activeTransform)
{
status = "Select a GameObject";
showPosition = false;
}


if (Selection.activeGameObject)
{
selectedTransform = Selection.activeGameObject.transform;

fold = EditorGUILayout.InspectorTitlebar(fold, selectedTransform);
if (fold)
{
selectedTransform.position =
EditorGUILayout.Vector3Field("Position", selectedTransform.position);
EditorGUILayout.Space();
rotationComponents =
EditorGUILayout.Vector4Field("Detailed Rotation",
QuaternionToVector4(selectedTransform.localRotation));
EditorGUILayout.Space();
selectedTransform.localScale =
EditorGUILayout.Vector3Field("Scale", selectedTransform.localScale);
}

}

m_ToggleBool=EditorGUILayout.Toggle("Toggle", m_ToggleBool);
m_ToggleBool = EditorGUILayout.ToggleLeft("ToggleLeft", m_ToggleBool);
m_Vector2FieldV2 = EditorGUILayout.Vector2Field("m_Vector2FieldV2", m_Vector2FieldV2);
m_Vector2Int = EditorGUILayout.Vector2IntField("Vector2IntField", m_Vector2Int);
m_Vector3FieldV3 = EditorGUILayout.Vector3Field("m_Vector3FieldV3", m_Vector3FieldV3);
m_Vector3Int = EditorGUILayout.Vector3IntField("Vector3IntField", m_Vector3Int);
m_Vector4FieldV4 = EditorGUILayout.Vector4Field("Vector3IntField", m_Vector4FieldV4);

}
效果



 特別注記
EditorGUILayout.PropertyField可設定任何其他的要素,如參數、物件、腳本等
用法上需要先有目標腳本
   
public class EditorGUIMyGameObjectScript : MonoBehaviour
{
public int m_MyInt = 75;
public Vector3 m_MyVector = new Vector3(20, 1, 0);
public GameObject m_MyGameObject;
public Lerp m_lerpScritp;

}

之後再作一個Editor專用的腳本去控制要顯示那些要素
 
using UnityEngine;
using UnityEditor;

// Custom Editor using SerializedProperties.
// Automatic handling of multi-object editing, undo, and prefab overrides.
[CustomEditor(typeof(EditorGUIMyGameObjectScript))]
[CanEditMultipleObjects]
public class EditorGUILayoutPropertyField : Editor
{
SerializedProperty m_IntProp;
SerializedProperty m_VectorProp;
SerializedProperty m_GameObjectProp;
SerializedProperty m_lerpScritp;

void OnEnable()
{
// Fetch the objects from the GameObject script to display in the inspector
m_IntProp = serializedObject.FindProperty("m_MyInt");
m_VectorProp = serializedObject.FindProperty("m_MyVector");
m_GameObjectProp = serializedObject.FindProperty("m_MyGameObject");
m_lerpScritp = serializedObject.FindProperty("m_lerpScritp");
}

public override void OnInspectorGUI()
{
//The variables and GameObject from the MyGameObject script are displayed in the Inspector with appropriate labels
EditorGUILayout.PropertyField(m_IntProp, new GUIContent("Int Field"), GUILayout.Height(20));
EditorGUILayout.PropertyField(m_VectorProp, new GUIContent("Vector Object"));
EditorGUILayout.PropertyField(m_GameObjectProp, new GUIContent("Game Object"));
EditorGUILayout.PropertyField(m_lerpScritp, new GUIContent("Lerp Script"));
// Apply changes to the serializedProperty - always do this at the end of OnInspectorGUI.
serializedObject.ApplyModifiedProperties();
}
}

需要特別注意的是
1.  需要啟用 using UnityEditor;
2.  [CustomEditor(typeof(EditorGUIMyGameObjectScript))] type要指定為目標腳本
3.  OnInspectorGUI 這邊需要是 public override ,同時沒設定顯示的部分她就都不會顯示。



留言

熱門文章