Unity Inspector用脚本修改不触发保存~~2018.3

问题

最近有两个朋友都问了我这个问题,在Editor脚本中,获取target修改里面的数据,没法保存,SetDirty()根本无效,只能通过改改别的地方能保存时候,保存一下。大概代码如下:

//--运行时脚本,里面有AAA属性
using UnityEngine;
public class ShadowMapTest : MonoBehaviour
{
    public int AAA;
}
//--编辑器脚本
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ShadowMapTest))]
public class ShadowMapTestEditor : Editor
{
    ShadowMapTest ShadowMapTest;
    // Start is called before the first frame update
    public override void OnInspectorGUI()
    {
        ShadowMapTest = target as ShadowMapTest;
        if (GUILayout.Button("555"))
        {
             ShadowMapTest.AAA = 555;
        }
        EditorGUILayout.LabelField("显示数据:" + ShadowMapTest.AAA);
        serializedObject.ApplyModifiedProperties();
    }
}

通过如上代码我们做如下实验:
我们初始化一下场景:
在这里插入图片描述
做如下操作:
在这里插入图片描述
在这里插入图片描述
好~问题大概就是这样,上面的问题数据明明发生了改变,却无法触发保存。

解决方案1-使用SerializedProperty

我们使用如下代码

using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ShadowMapTest))]
public class ShadowMapTestEditor : Editor
{
    ShadowMapTest ShadowMapTest;
    SerializedProperty AAA;
    // Start is called before the first frame update
    public override void OnInspectorGUI()
    {
        ShadowMapTest = target as ShadowMapTest;
        AAA = serializedObject.FindProperty("AAA");
        if (GUILayout.Button("555"))
        {
            AAA.intValue = 555;
        }
        EditorGUILayout.LabelField("显示数据:" + ShadowMapTest.AAA);
        serializedObject.ApplyModifiedProperties();
    }
}

是可以触发变更的,因为SerializedProperty 的setter会自动触发change。如果属性是Arry我们可以通过如下方法进行设置。

public void InsertArrayElementAtIndex(int index);//插入元素
public void DeleteArrayElementAtIndex(int index);//删除元素
public bool MoveArrayElement(int srcIndex, int dstIndex);//调整元素位置
public IEnumerator GetEnumerator();//获取迭代器,可以遍历数据

解决方案2-Undo.RecordObject

using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ShadowMapTest))]
public class ShadowMapTestEditor : Editor
{
    ShadowMapTest ShadowMapTest;
    // Start is called before the first frame update
    public override void OnInspectorGUI()
    {
        ShadowMapTest = target as ShadowMapTest;
        if (GUILayout.Button("555"))
        {
            //--再此记录
            Undo.RecordObject(ShadowMapTest, "TTT");
            ShadowMapTest.AAA = 555;
        }
        EditorGUILayout.LabelField("显示数据:" + ShadowMapTest.AAA);
        serializedObject.ApplyModifiedProperties();
    }
}

使用这个方法后也可以记录修改的信息。而且我们不需要使用SerializedProperty查来查去的。

复杂数据结构测试

上面我们都是简单的数据结构,如果我们的数据结构边的复杂一点,如下:

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

public class ShadowMapTest : MonoBehaviour
{
    public int AAA;
    public List<int> A = new List<int>();
    public TestData TestData;
}
[Serializable]
public class TestData
{   
    public List<int> A=new List<int>();
}

经测试,我们使用上述两个方法修改任何一个属性都可以触发保存。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘培玉--大王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值