Regarding the pitfalls of saving serialized variables, data with indexers cannot be serialized.

I was working on a project recently. When I was changing the inspector in a custom editor, I encountered a huge pitfall that was difficult to detect. It took nearly a day to solve the problem. In order to prevent latecomers from making the same mistake, I will talk about it here.

Table of contents

Summarize


Generally, when you want to localize data, you may encounter some pitfalls at first. After surfing Baidu, you realize that only serialized data can be stored locally. However, it is difficult to find a clear and easy-to-understand answer on the Internet about what serialized data is. , in fact, most people don’t need to know this. They know that when a class that does not inherit any parent class wants to be serialized, you need to add [Serializable] on top , and then you need to mark which data is dirty, then save it, and you’re done. .

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

[Serializable]
public class T
{
    ;
}
    public void SaveData()
    {
        EditorUtility.SetDirty(this);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

Generally speaking, this is enough to ensure that there is no problem with the logic. Any modifications made on the inspector panel will be saved, and the data will still be there even if you exit Unity and reopen it.

There are always various miscellaneous problems arising in the projects that can be done, which fully highlights the uncertainty of fate. After completing the above operations, I made modifications to the data and still could not save the data. That is, after exiting the Unity editor and reopening it, all the data was cleared.

I was very depressed. I went to Baidu like crazy, but none of the answers could solve my problem. When I was about to give up and think about whether to try using XML or JSON to save data, I noticed a point. That is, I can save some data when I modify it, but the data I want to save is the most important. If it just cannot be saved, then the problem can be solved from this point.

The data structure at that time was like this (with some processing):

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

[Serializable]
public class Data
{
    public Hashtable hashtable;
    [ShowOnly] public string AName;
    [ShowOnly] public string BName;
    private List<T> _TList = new List<T>();
    public List<T> TList
    {
        get{return _TList;}
        set{_TList = value;}
    }

    public Data(string AName, string BName)
    {
        this.AName = AName;
        this.BName = BName;
    }


    [Serializable]
    public class T
    {
        public int A;
        public int B;
        public int C;
        public int D;
    }
}

The data I want to focus on is the TList variable. Only this data cannot be saved. Other data in this script can be saved after modification, but why can't it be saved? After thinking about it, I think it has something to do with serialization, but I added [Serializable] , so there is no reason why it can't be serialized. In fact, it was wrong. It really couldn't be serialized. I didn't know what the problem was at the time, but I could already see the problem.

To achieve this step, I needed to verify my idea, so I ran to the project folder and found the Asset file. When I looked at it, I suddenly realized that there was no TList data in it, that is, it had not been serialized at all !

After a day of torture, after revising it over and over again, I could see the source of the error at a glance, even if I couldn't see the principle of the error.

    private List<T> _TList = new List<T>();
    public List<T> TList
    {
        get{return _TList;}
        set{_TList = value;}
    }

The problem is here, I added an indexer to this data, and the data with the indexer cannot be serialized! ! !

The problem was solved when I removed the indexer.

    public List<T> TList;

Why is there such a problem? I’m not sure, and I didn’t check it, but I’m sure I’ve stepped into another trap. In fact, this problem has been predicted for a long time. When I was typing the code before, I discovered that variables with indexers would not be displayed in the panel. At that time, I thought that the variables with indexers would be hidden, so I didn't worry about it, but I ended up falling into a trap the day after tomorrow. . .

Summarize

1. The serialized data can be saved automatically or through code after modification.

2. Variables with indexers cannot be serialized

3. If you want to know which data is serialized, you can go directly to the project folder to find the corresponding asset file and open it. If there is no data you are looking for, it is most likely not serialized (it is not on the panel unless it is manually hidden. , otherwise it is probably not serialized)

Since I couldn't find the corresponding answer after searching on Baidu for a long time, I feel it is necessary to write it down so that people in need can read it. Although this is common sense to some people, for developers like me who are self-taught and whose acquisition of knowledge is fragmented, I have to fall into a trap for several days. I hope more people who have fallen into this trap will share their experiences. (Painful), helping latecomers complete their games faster and better.

Guess you like

Origin blog.csdn.net/weixin_49779414/article/details/125823167