当前位置:网站首页>As for the pit saved by serialized variables, the data added with indexer cannot be serialized

As for the pit saved by serialized variables, the data added with indexer cannot be serialized

2022-07-27 02:38:00 five hundred and forty-five thousand five hundred and eighty-ni

I'm working on a project recently , Making custom editor changes inspector I encountered a huge pit that was difficult to find , It took nearly a day to solve this problem , In order not to let the latecomers repeat the mistakes, let's say it here .

Catalog

summary


Generally, when you want to do data localization, you may step into the pit at the beginning , After surfing the Internet, baidu learned that only serialized data can be stored locally , But what is serialized data is difficult to find a clear and understandable answer on the Internet , In fact, most people don't need to know these , Know that when you want to serialize a class that does not inherit any parent class, you need to add [Serializable], Then you need to mark which data is dirty , Then save , Finished .

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 , Under the condition that there is no problem with the logic , It's almost like that . stay inspector Any changes made on the panel will be saved , Even if you quit Unity Reopening the data is still .

There are always miscellaneous problems in projects , It fully highlights the uncertainty of fate . After completing the above operations , I still can't save the data after making changes to the data , That is, to quit Unity After reopening the editor, all the data is cleared .

I'm very depressed , Online crazy Baidu , But none of the answers can solve my problem . Before I intend to give up , Think about whether to try it XML perhaps JSON When saving data , I noticed a point , Then I can save some data when modifying , But the key data I want to save is that it cannot be saved , The problem can be dealt with from this point .

The data structure at that time was like this ( Did 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 TList This variable , Only this data can't be saved , Other data in this script can be saved after modification , But why can't it be preserved ? reasoning , I think it's still related to this serialization , But I added [Serializable], There's no reason why you can't serialize . Actually, it's wrong. , It really can't be serialized , At that time, I didn't know what the problem was , But I have seen the problem .

Do this , I need to test my idea , So I ran to the project folder and found this Asset file , It suddenly dawned on me , There's no one in it TList This data , That is, it is not serialized at all !

After a day of torture , I've changed it over and over again, even if I can't see the wrong principle , Also saw the source of the error at a glance .

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

That's the problem , I added an indexer to this data , Data with indexers cannot be serialized !!!

When I removed the indexer, the problem was solved .

    public List<T> TList;

Why is there such a problem ? I'm not sure , I didn't check , But for sure, I stepped on another pit , In fact, the emergence of this problem has long been foreshadowed , When I typed the code before, I found that variables with indexers will not appear on the panel , At that time, I thought that variables with indexers would be hidden , Don't worry much , The day after tomorrow, it fell into the pit ...

summary

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

2. Variables with indexers cannot be serialized

3. Want to know which data is serialized , You can go directly to the project folder to find the corresponding asset Open the file and have a look , If there is no data to find , The big probability is that there is no serialization ( Not on the panel , Unless hidden manually , Otherwise, it is probably not serialized )

Since I have been looking for Baidu for a long time, I can't find the corresponding answer , I think it is necessary to write it out for people in need to see . Although for some people this is common sense , But for my self-study , Knowledge acquisition is fragmented for developers , A pit will sink for several days , I hope more people can share their experiences ( Pain ), Help latecomers finish their games faster and better .

原网站

版权声明
本文为[five hundred and forty-five thousand five hundred and eighty-ni]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207262309330145.html