Unity不序列化int吗?场地 [英] Unity Doesnt Serialize int? field

查看:227
本文介绍了Unity不序列化int吗?场地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在编辑器中更改其属性的类.因此,我将班级System.Serializable设为必需,并公开了我希望能够更改的变量.
像这样:

I have a class i want to change the properties of in the editor. So i made my class System.Serializable and made the variables public that i want to be able to change.
Like so:

[System.Serializable]
public class UIOptionsRing
{
    public float Radius, DistanceBetweenPoints, StartOffset, GapInDegrees;
    public int? GapAfterElementNumer = 3; //this var doesnt show up
    public Vector3 CircleCenter;
    public GameObject CircleElementsContainer;

}

但是我遇到的问题是GapAfterElementNumer没有在所有其他字段中显示在编辑器中.我如何才能使int?也显示出来?

But the problem i am having is that the GapAfterElementNumer is not show up in the editor at all the other fields are. How i can i make it so that int? also shows up?

推荐答案

可空类型未在Unity Editor中进行序列化,因为它的序列化器不支持null. 如果您不打算使用JsonUtility将此类序列化为json,则有一个较小的解决方法. 关键思想是您必须创建自己的可为null的int.

Nullable types are not serialized in Unity Editor because it's serializer doesn't support null. There's a small workaround if you're not going to serialize this class to json using JsonUtility. The key idea is that you have to create your own nullable int. Something like

public class IntNullable 
{
     public int Value;
     public bool HasValue;
 }

就像在.NET中完成一样.然后,您可以为IntNullableUIOptionsRing创建自定义编辑器.在此编辑器中,您可以创建一个int值字段和一个"Set Null"按钮,这将更改HasValue变量的值.而且,您还需要在代码中使用此自定义IntNullable.

Just like it's done inside .NET. Then you can create a Custom Editor for IntNullable or your UIOptionsRing. In this editor you can make a filed for int value and a button "Set Null", which will change the value of HasValue variable. And further you need to work with this custom IntNullable in your code.

这篇关于Unity不序列化int吗?场地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆