扩展偏好设置面板——PreferenceItem

作者:追风剑情 发布于:2017-11-9 11:11 分类:Unity3d

示例


using UnityEngine;
using UnityEditor;
using System.Collections;

/// <summary>
/// 扩展偏好设置面板
/// </summary>
public class CPreferenceItem {

    private static bool prefsLoaded = false;
    public static bool boolPreference = false;

    public static GameObject gameObjectPreference = null;
    public static bool groupEnabled = false;
    static bool[] pos = new bool[3] { true, true, true };
    static bool fold;

    // 添加一个偏好设置选项卡
    [PreferenceItem("My Preferences")]

    public static void PreferencesGUI()
    {
        // 载入偏好设置
        if (!prefsLoaded)
        {
            boolPreference = EditorPrefs.GetBool("BoolPreferenceKey", false);
            prefsLoaded = true;
        }

        boolPreference = EditorGUILayout.Toggle("Bool Preference", boolPreference);
        EditorGUILayout.Separator();

        gameObjectPreference = EditorGUILayout.ObjectField("ObjectField", gameObjectPreference, typeof(GameObject), true) as GameObject;
        EditorGUILayout.Space();
        EditorGUILayout.TagField("TagField", "MainCamera");
        EditorGUILayout.SelectableLabel("SelectableLabel");
        EditorGUILayout.PasswordField("PasswordField", "123456");
        EditorGUILayout.Popup("Popup", 0, new string[]{"aaa", "bbb"});
        EditorGUILayout.Slider("Slider", 5f, 0f, 10f);

        if (Selection.activeGameObject)
        {
            fold = EditorGUILayout.InspectorTitlebar(fold, Selection.activeGameObject.transform);
            if (fold)
            {
                groupEnabled = EditorGUILayout.BeginToggleGroup("Align position", groupEnabled);
                pos[0] = EditorGUILayout.Toggle("x", pos[0]);
                pos[1] = EditorGUILayout.Toggle("y", pos[1]);
                pos[2] = EditorGUILayout.Toggle("z", pos[2]);
                EditorGUILayout.EndToggleGroup();
            }
        }
        
        // 保存偏好设置
        if (GUI.changed)
            EditorPrefs.SetBool("BoolPreferenceKey", boolPreference);
    }
}


效果(Edit->Preferences...)

1111111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号