当前位置:网站首页>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
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 .
边栏推荐
- The latest JD SMS login + silly girl robot nanny level deployment tutorial (July 24, 2022)
- What is the principle of synchronized lock escalation in multithreading?
- Open the door of programming
- [Li Kou] 1859. Sort sentences
- OSPF总结(思维导图)
- Prometheus operation and maintenance tool promtool (III) debug function
- Go language slow start -- go operator
- LeetCode->二分查找打卡
- Hcip day 6 OSPF static experiment
- 小程序utils
猜你喜欢

Hcip day 1
![[do you know cache - fully understand cache]](/img/65/df29f6f1ff6d0d119da4a3971db0fc.png)
[do you know cache - fully understand cache]

中断、信号、系统调用

What is the principle of synchronized lock escalation in multithreading?

Graduated and entered HW, from test engineer to project manager. Now I earn millions in goose factory every year. My suggestions to you

【你了解Cache吗——全面理解高速缓冲存储器】

Hcip bidirectional republication and routing strategy

Hcip OSPF comprehensive experiment

OSPF summary (mind map)

想要彻底搞的性能优化,得先从底层逻辑开始了解~
随机推荐
Qt中文乱码常量换行符终极解决方案
NAT网络地址转换协议-拓扑实验
关于序列化变量保存的坑,加了索引器的数据不能序列化
uni-app 微信小程序搜索关键字标红显示
[brother Yang takes you to play with the linear table (4) - chain queue]
Prometheus operation and maintenance tool promtool (III) debug function
Find a specific number in an ordered array
The latest multi-threaded & highly concurrent learning materials, interview confidence
After ten years of testing, I want to say to my friends who are still confused: one thing is to do a good job in personal planning
通过ensp让静态路由实现全网可达
How does the whole network display IP ownership?
NAT network address conversion experiment
[draw rectangular coordinate system in C language]
Hcip the next day
cookie增删改查方法
HCIP-第一天
OSPF路由信息协议-拓扑实验
go语言慢速入门——包
Hcip day 4 OSPF routing protocol
创业3年,现在鹅厂,年收入百万+,作为软件测试前辈的一些建议....