0

我正在尝试将数据序列化到我的类中/从我的类中派生MonoBehaviour,这些类不能从客户端代码创建(例如,使用new关键字),而是必须由 Unity3D 特定的方法创建,GameObject.AddComponent<T>(). 如何使用该YamlDotNet框架为我的类填充值,而不必为每个类创建一个适配器?是否有某种我可以配置的内置适配器,这样 YamlDotNet 不会实例化它试图序列化的类?

一个典型的文件可能包含项目的映射,例如,

%YAML 1.1
%TAG !invt! _PathwaysEngine.Inventory.
%TAG !intf! _PathwaysEngine.Adventure.
---

Backpack_01: !invt!Item+yml
  mass: 2
  desc:
    nouns: /^bag|(back)?pack|sack|container$/
    description: |
      Your backpack is only slightly worn, and...
    rand_descriptions:
    - "It's flaps twirl in the breeze."
    - "You stare at it. You feel enriched."

MagLite_LR05: !invt!Lamp+yml
  cost: 56
  mass: 2
  time: 5760
  desc:
    nouns: /^light|flashlight|maglite|lr_05$/
    description: |
      On the side of this flashlight is a label...

      (Type "light" to turn it on and off.)

...

标签是我的项目的完全指定的类名,例如,PathwaysEngine.Inventory.Lamp+ymlPathwaysEngine我用于我的游戏引擎代码的命名空间,Inventory处理项目和诸如此类的东西,并且Lamp+yml是编译器如何表示嵌套类,yml在里面LampLamp+yml可能看起来像这样:

public partial class Lamp : Item, IWearable {
    public new class yml : Item.yml {
        public float time {get;set;}
        public void Deserialize(Lamp o) {
            base.Deserialize((Item) o);
            o.time = time;
        }
    }
}

我调用从Deserialize()派生的所有对象,即,一旦游戏中存在类。在其他地方,我已经创建了一个非常复杂的填充类型对象,然后获取一个真实的运行时类的实例并用值填充它。必须有一种更清洁的方法来做到这一点,对吧?ThingAwake()MonoBehaviourDictionarySomeclass+ymlDeserializeSomeclass

我怎样才能:

  1. 告诉Deserializer我我的课是什么?有关上述问题的良好解决方案,请参见第二次编辑

  2. 在不尝试创建我的MonoBehaviour派生类的情况下获取数据?

编辑:我已经解决了这个问题,并且找到了处理自定义数据的好方法(在我尝试从我的数据中解析正则表达式的特殊情况下,并且不将它们视为字符串,因此,un- castable to regex) 是IYamlTypeConverter为该特定字符串使用 a 。但是,将 YamlDotNet 与 Unity3D 一起使用MonoBehaviour仍然是一个问题。

另一个编辑:上面的例子使用了一种非常丑陋的方式来确定类型。就我而言,最好的办法是先用反序列化器注册标签,例如,

var pre = "tag:yaml.org,2002:";
var tags = new Dictionary<string,Type> {
    { "regex", typeof(Regex) },
    { "date", typeof(DateTime) },
    { "item", typeof(Item) }};
foreach (var tag in tags)
    deserializer.RegisterTagMapping(
        pre+tag.Key, tag.Value);

然后,我使用文件!!tag中的符号*.yml,例如,

%YAML 1.1
---

Special Item: !!item
  nouns: /thing|item|object/
  someBoolean: true

Start Date: !!date 2015-12-17

some regex: !!regex /matches\s+whatever/

...
4

1 回答 1

1

您可以将自定义实现传递给类IObjectFactory的构造函数Deserializer。每次反序列化器需要创建一个对象的实例时,它都会使用IObjectFactory来创建它。

请注意,您的工厂将负责创建反序列化的每种类型的实例。实现它的最简单方法是在 周围创建一个装饰器DefaultObjectFactory,例如:

class UnityObjectFactory : IObjectFactory
{
    private readonly DefaultObjectFactory DefaultFactory =
        new DefaultObjectFactory();

    public object Create(Type type)
    {
        // You can use specific types manually
        if (type == typeof(MyCustomType))
        {
             return GameObject.AddComponent<MyCustomType>();
        }
        // Or use a marker interface
        else if (typeof(IMyMarkerInterface).IsAssignableFrom(type))
        {
             return typeof(GameObject)
                 .GetMethod("AddComponent")
                 .MakeGenericMethod(type)
                 .Invoke();
        }
        // Delegate unknown types to the default factory
        else
        {
            return DefaultFactory(type);
        }
    }
}
于 2015-12-10T15:03:05.697 回答