对Unity ScriptableObject自定义Inspector面板扩展

主要遇到一下几个问题

  1. 一开始在ScriptObject中使用了字典,好像unity ScriptObject 不支持字典,每次生成数据后,会
    消失,后面修改为使用List.
  2. 一开始直接获取target对象转化为目标对象进行数据显示,这样会导致Inspector面板上修改后,数据不会保存,使用代码修改数据也是不会保存,后面发现unity编辑器扩展好像是需要修改SerializedProperty的属性的值,才会触发自动保存或者手动保存

解决思路

  • 其他数据正常使用target转化的对象来显示,序列化对象中专门设置一个变量来控制SerializedProperty的值的改变,这样就可以触发unity的自动保存和手动保存.

Inspector扩展代码


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

[CustomEditor( typeof( GameManagerConfig )  )]
public class GameManagerConfigInspector : Editor
{

    private GameManagerConfig config=> target as GameManagerConfig;
    private ChannelManagerConfig curChannelConfig => config.GetChannelManagerConfig( config.curChannel );
    private SerializedProperty changeCount;

    void OnEnable()
    {
        changeCount = serializedObject.FindProperty( "changeCount" );
    }
    public override void OnInspectorGUI()
    {
        EditorGUILayout.BeginHorizontal();
        if ( GUILayout.Button("更新管理类配置信息") )
        {
            config.GenerateGameManagerConfig();
            ApplyModify();
        }
        if ( GUILayout.Button("清除管理类配置信息") )
        {
            config.Clear();
            ApplyModify();
        }
        EditorGUILayout.EndHorizontal();
        if ( config.channelManagerConfigList.Count ==0)
        {
            return;
        }
        EditorGUI.BeginChangeCheck();
        config.curChannel = ( ChannelEnum ) EditorGUILayout.EnumPopup( "当前平台 :" , config.curChannel );
        foreach ( ManagerClassInfo managerClass in curChannelConfig.managerClassList)
        {
            if (  managerClass.implementClassList.Count >0 )
            {
                EditorGUILayout.BeginHorizontal( GUI.skin.label );
                EditorGUILayout.LabelField(  managerClass.sampleMangerName );
                managerClass.useIndex = EditorGUILayout.Popup( managerClass.useIndex , managerClass.sampleImplementClassName);
                EditorGUILayout.EndHorizontal();
            }
        }
        if ( EditorGUI.EndChangeCheck() )
        {
            ApplyModify();
        }
    }

    private void ApplyModify()
    {
        serializedObject.Update();
        changeCount.intValue++;
        serializedObject.ApplyModifiedProperties();
       
    }
    
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值