撰写了文章 发布于 2020-05-02 23:00:05
Articy Importer Guide - 06 克隆对象
Usually your data is considered to be single instance, that means no matter where or when you access a particular object, it is always the same and changes persist through each access. Sometimes it is useful to have a copy of an object to change independantly of the origin object. This process of cloning and its usecase is explained in this guide.
本篇包含以下内容:
This topic contains the following sections:
- 克隆对象 Cloning objects
- 进阶克隆 Advanced cloning
- 移除克隆 Remove clones
- 使用ArticyRef克隆 Cloning with ArticyRef
这里要记住一件事:插件会创建C#类,紧密匹配articy:draft中找到的对象。也就是说你可以访问诸如FlowFragments,Entity等类。但更重要的是,在articy中创建和使用对象模板时,这些模板也将成为可以轻松访问的类。
The only thing you need to keep in mind is: The plugin will create C# classes that closely match the objects found in articy draft. Which means, you have access to classes like FlowFragments, Entity etc. But more importantly, when you have created and used object templates in articy, then those will become classes aswell that you can just as easy access.
注意 |
|---|
虽然不必学习和理解这些示例,但是如果您希望一步一步跟着这些示例,请确保使用Maniac Manfred 的unity项目。 While not necessary to learn and understand the examples, if you want to follow the examples side by side, make sure to use the maniac manfred unity project. |
克隆对象
Cloning objects
Up until this point, we only dealt with the same instance of an object in the database. No matter where and when, we always get the same instance of an object, to clarify consider the following example
var obj = ArticyDatabase.GetObject<Character>("Chr_Manfred");
Debug.Log(obj.DisplayName); // 将“Manfred”打印到控制台 prints "Manfred" to the console
// 现在更改它的显示名称
// now we change its display name
obj.DisplayName = "Player Manfred";
// 之后…
// later ...
var obj = ArticyDatabase.GetObject<Character>("Chr_Manfred"); Debug.Log(obj.DisplayName); // 将“Manfred”打印到控制台 prints "Manfred" to the console
var clonedManfred = ArticyDatabase.CloneFrom<Character>("Chr_Manfred", 1); Debug.Log(clonedManfred.DisplayName); // 将“Manfred”打印到控制台 prints "Manfred" to the consoleprints "Manfred" to the console
clonedManfred.DisplayName = "Cloned Manfred";
// 之后…
// later ...
var clone = ArticyDatabase.GetObject<Character>("Chr_Manfred", 1); // 实例id = 1,返回带有实例id的克隆 instance id = 1, return the clone with that instance id
Debug.Log(clone.DisplayName); //将“Cloned Manfred”打印到控制台 prints "Cloned Manfred" to the console
var baseManfred = ArticyDatabase.GetObject<Character>("Chr_Manfred"); // 没有实例ID作为第二个参数?返回基础对象 no instance id as second argument? Return the base object
Debug.Log(baseManfred.DisplayName); // 将“Manfred”打印到控制台 prints "Manfred" to the console
让我们一条接一条地看看该示例:
Lets check this example piece by piece:
var clonedManfred = ArticyDatabase.CloneFrom<Character>("Chr_Manfred", 1);
注意 |
|---|
因为我们的克隆对象是一个拥有和引用对象相同TechnicalName及相同对象Id的精确克隆,我们需要一个新的id作为区别,否则我们会无法使用GetObject()。这个新的id叫做实例ID,它独立于对象id,并且是GetObject()方法的一个可选参数。实际上每个对象都有一个分配好了的InstanceId,但目前为止,我们处理过的对象始终具有实例ID 0,这类对象称为基础对象。 Because our cloned object is an exact clone with the same TechnicalName and object Id as the referenced object, we need a new id to identify it otherwise we could not use GetObject(). This new id is called Instance ID, it is independent of the object id and is an optional argument for a lot of GetObject methods. In fact every object has an assigned InstanceId already, but the objects we have dealt with this far always have the instance id 0 and are called the base objects. |
clonedManfred.DisplayName = "Cloned Manfred";我们可以像平常一样更改克隆对象。
We can change the new cloned object like normal.
var clone = ArticyDatabase.GetObject<Character>("Chr_Manfred", 1); // 实例id = 1,返回带有实例id的克隆 instance id = 1, return the clone with that instance idinstance id = 1, return the clone with that instance id
Debug.Log(clone.DisplayName); // 将“Cloned Manfred”打印到控制台 prints "Cloned Manfred" to the console
var baseManfred = ArticyDatabase.GetObject<Character>("Chr_Manfred"); // 没有实例id作为第二个参数?返回基本对象 no instance id as second argument? Return the base object
Debug.Log(baseManfred.DisplayName); // 将“Manfred”打印到控制台 prints "Manfred" to the console
当我们使用GetObject()而不传入实例id作为第二个参数,我们相当于告诉数据库返回基础对象。在上面的示例中,你能够看到两个GetObject的调用表现了这一概念。
When we use GetObject() without passing in the instance id as the second argument, we tell the database effectively to return the base object. In our example above you can see both GetObject calls showing this concept.
进阶克隆
Advanced cloning
The previously shown CloneFrom() only works when there is no clone under the given id, making sure we only try to clone it once, otherwise it returns null. But it can be useful to clone if the id is not used and return the existing object otherwise:
var clone = ArticyDatabase.CloneOrGetFrom<Character>("Chr_Manfred", 1);
// 使用id克隆一个新的,或者返回现有具有该id的对象
// clone a new with id, or return the existing one with that id
We saw how to clone an object where we supply the instance id and have to make sure to supply one that is not yet used. This can be a hassle to keep track each id by hand, so for that case the plugin has a special clone method called CloneWithConsecutiveInstanceIdFrom()
uint cloneId1;
uint cloneId2;
var clone1 = ArticyDatabase.CloneWithConsecutiveInstanceIdFrom<Character>("Chr_Manfred", out cloneId1);
var clone2 = ArticyDatabase.CloneWithConsecutiveInstanceIdFrom<Character>("Chr_Manfred", out cloneId2);
Debug.Log(cloneId1); // prints 1
Debug.Log(cloneId2); // prints 2
此处你可以看到我们如何将manfred克隆了两次,并让插件分配了一个空的克隆id,将它保存在CloneWithConsecutiveInstanceIdFrom()方法的第二个参数中。
Here you can see how we clone manfred 2 times, and let the plugin assign a free clone id, storing it in the second argument of the CloneWithConsecutiveInstanceIdFrom() method.
移除克隆
Remove clones
Sometimes its necessary to get rid of your clone or clear all clones all together
ArticyDatabase.RemoveClone("Chr_Manfred", 5); // delete the manfred clone with the instance id 5
ArticyDatabase.RemoveAllClones("Chr_Manfred"); // remove all manfred clones, remember instance id 0, the base object will not be removed
使用ArticyRef克隆
Cloning with ArticyRef
The beforementioned type ArticyRef has also built in support for our different cloning methods. Allowing us to change the id from the inspector.
[ArticyRefComplex]
public ArticyRef complexRef;

No matter which mode the designer selects, the reference can be accessed with the same GetObject methods, the ref will automatically handle the cloning and all necessary maintainance.

注意