重命名序列化字段

As some of you may know, we built most of the new UI System as user scripts. That means we were subject to most of the limitations our users face when developing systems in Unity and extending the editor. Throughout the development we’ve been fixing the issues we came across. Today we would like to highlight one of them.

你们中有些人知道,我们将大多数新的UI系统构建为用户脚本。 这意味着在Unity中开发系统并扩展编辑器时,我们受到用户所面临的大多数限制。 在整个开发过程中,我们一直在解决遇到的问题。 今天,我们要强调其中之一。

One of the problems we faced was how could we rename fields without having our users lose data. During our beta period, we wanted to refactor our code without breaking our beta users projects, so they could continue testing each new version without having to worry about data loss and project breakage. To solve that we introduced the [FormerlySerializedAs] attribute.

我们面临的问题之一是如何在不使用户丢失数据的情况下重命名字段。 在测试期间,我们希望重构代码而不破坏测试用户项目,因此他们可以继续测试每个新版本,而不必担心数据丢失和项目中断。 为了解决这个问题,我们引入了[FormerlySerializedAs]属性。

你能做什么呢? 让我们来看一些用例! (What can you do with it? Let’s look at some use cases for it!)

变量重命名 (Variable renaming)

Let’s say you have the following class:

假设您有以下课程:

1

2
3
4
5
6
using UnityEngine;
class MyClass : MonoBehaviour
{
[SerializeField]
private string m_MyVariable;
}

1

2
3
4
5
6
using UnityEngine ;
class MyClass : MonoBehaviour
{
[ SerializeField ]
private string m_MyVariable ;
}

But you would like to rename m_MyVariable to something else like m_ABetterName, but you don’t want your users to have to re-populate the data for this MonoBehaviour in all of their scenes and/or prefabs. You can now accomplish that like this:

但是您想将m_MyVariable重命名为m_ABetterName之类的其他名称 ,但是您不希望用户必须在其所有场景和/或预制件中重新填充此MonoBehaviour的数据。 您现在可以像这样完成操作:

1

2
3
4
5
6
7
8
using UnityEngine;
using UnityEngine.Serialization;
class MyClass : MonoBehaviour
{
[FormerlySerializedAs("m_MyVariable")]
[SerializeField]
private string m_ABetterName;
}

1

2
3
4
5
6
7
8
using UnityEngine ;
using UnityEngine . Serialization ;
class MyClass : MonoBehaviour
{
[ FormerlySerializedAs ( "m_MyVariable" ) ]
[ SerializeField ]
private string m_ABetterName ;
}

Encapsulating public API

封装公共API

In this case, you have a public field that is part of your API, but would like to encapsulate it in an accessor. So let’s assume we have a class MyClass like this:

在这种情况下,您有一个公共字段,它是您的API的一部分,但希望将其封装在访问器中。 因此,假设我们有一个类似MyClass的类:

1

2
3
4
5
using UnityEngine;
class MyClass : MonoBehaviour
{
public string myValue;
}

1

2
3
4
5
using UnityEngine ;
class MyClass : MonoBehaviour
{
public string myValue ;
}

To encapsulate this value in an accessor without losing any existing data in your assets you can do something like this:

要将这个值封装在访问器中而不丢失资产中任何现有数据,您可以执行以下操作:

1

2
3
4
5
6
7
8
9
10
11
12
13
using UnityEngine;
using UnityEngine.Serialization;
class MyClass : MonoBehaviour
{
[FormerlySerializedAs("myValue")]
[SerializeField]
private string m_Value;
public string myValue
{
get { return m_Value; }
set { m_Value = value; }
}
}

1

2
3
4
5
6
7
8
9
10
11
12
13
using UnityEngine ;
using UnityEngine . Serialization ;
class MyClass : MonoBehaviour
{
[ FormerlySerializedAs ( "myValue" ) ]
[ SerializeField ]
private string m_Value ;
public string myValue
{
get { return m_Value ; }
set { m_Value = value ; }
}
}

Multiple Renames

多个重命名

Renaming fields multiple times is supported, just add the attribute multiple times, one for each name of the previous names of the field:

支持多次重命名字段,只需多次添加属性,字段的先前名称的每个名称一次即可:

1

2
3
4
5
6
7
8
9
using UnityEngine;
using UnityEngine.Serialization;
class MyClass : MonoBehaviour
{
[FormerlySerializedAs("m_MyVariable")]
[FormerlySerializedAs("m_ABetterName")]
[SerializeField]
private string m_EvenBetterName;
}

1

2
3
4
5
6
7
8
9
using UnityEngine ;
using UnityEngine . Serialization ;
class MyClass : MonoBehaviour
{
[ FormerlySerializedAs ( "m_MyVariable" ) ]
[ FormerlySerializedAs ( "m_ABetterName" ) ]
[ SerializeField ]
private string m_EvenBetterName ;
}

When can I remove the attribute?

什么时候可以删除属性?

You can remove the attributes after you have re-saved all of your scenes and assets, after the rename. Of course this implies you have ‘control’ of your users. For some, like the Asset Store publishers for instance, this ‘control’ is impossible. So in this case you will have to keep it as long as you want to make sure people with any version of your code can upgrade to your new code without losing data.

重命名后重新保存所有场景和资产后,可以删除属性。 当然,这意味着您可以“控制”用户。 对于某些人(例如资产商店发布者)来说,这种“控制”是不可能的。 因此,在这种情况下,只要您要确保使用任何版本的代码的人都可以在不丢失数据的情况下升级到新代码,就必须保留它。

Hope you find this new little feature helpful!

希望这个新的小功能对您有所帮助!

翻译自: https://blogs.unity3d.com/2015/02/03/renaming-serialized-fields/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值