Unity 编辑器扩展(在project settings 界面生成一个设置项,并在项目中调用)

Unity 编辑器扩展(在project settings 界面生成一个设置项,并在项目中调用)

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

///
/// 在编辑器 project settings 界面生成一个设置项
///
static class SettingToolEditor
{
    
    
    [SettingsProvider]
    public static SettingsProvider CreateMyCustomSettingsProvider()
    {
    
    
        string k_MyCustomSettingsPath = "Assets/MainConfig.asset";
        // First parameter is the path in the Settings window.
        // Second parameter is the scope of this setting: it only appears in the Project Settings window.
        var provider = new SettingsProvider("Project/ZOR_XR_Settings", SettingsScope.Project)
        {
    
    
            // By default the last token of the path is used as display name if no label is provided.
            label = "ZOR_XR_Settings",
            // Create the SettingsProvider and initialize its drawing (IMGUI) function in place:
            guiHandler = (searchContext) =>
            {
    
    
                SerializedObject settings = new SerializedObject(AssetDatabase.LoadAssetAtPath<MainConfig>(k_MyCustomSettingsPath));
                EditorGUILayout.PropertyField(settings.FindProperty("m_Number"), new GUIContent("ZOR_XR_Settings Number"));
                EditorGUILayout.PropertyField(settings.FindProperty("m_InteractiveType"), new GUIContent("InteractiveType"));
                settings.ApplyModifiedPropertiesWithoutUndo();
            },// Populate the search keywords to enable smart search filtering and label highlighting:
            keywords = new HashSet<string>(new[] {
    
     "m_Number", "Some String" })
        };

        return provider;
    }


    ///
    /// 这是ScriptableObject 生成一个asset文件
    ///
    [CreateAssetMenu(fileName = "MainConfig", menuName = "ScriptableObjects / MainConfig", order = 1)]
    public class MainConfig : ScriptableObject
    {
    
    
        public InteractiveType m_InteractiveType;
        public int m_Number;
        public static MainConfig GetSerializedObject()
        {
    
    
            return Resources.Load("MainConfig") as MainConfig;
        }

    }
    public enum InteractiveType
    {
    
    
        Interactive_OnlyOnGlasses,
        Interactive_OnPhoneAndOnGlasses
    }

    public class TestDemo : MonoBehaviour
    {
    
    
        private MainConfig mainConfig;
        // Start is called before the first frame update
        private void Awake()
        {
    
    
            if (mainConfig == null)
            {
    
    
                mainConfig = MainConfig.GetSerializedObject();// Resources.Load("MainConfig");
            }
            Debug.Log(mainConfig.m_Number);
            Debug.Log(mainConfig.m_InteractiveType);
        }
    }
}


基于MainConfig这个文件来做的,没有这个文件会报错。
转载于:Unity 编辑器扩展(在project settings 界面生成一个设置项,并在项目中调用)
在新版unity会有报错,修改了他的报错。

猜你喜欢

转载自blog.csdn.net/u012685176/article/details/127792693
今日推荐