Entitas  0.35.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
ComponentBlueprint.cs
1 using System;
2 using System.Collections.Generic;
3 
4 namespace Entitas.Serialization.Blueprints {
5 
6  [Serializable]
7  public class ComponentBlueprint {
8 
9  public int index;
10  public string fullTypeName;
11  public SerializableMember[] members;
12 
13  Type _type;
14  Dictionary<string, PublicMemberInfo> _componentMembers;
15 
16  public ComponentBlueprint() {
17  }
18 
19  public ComponentBlueprint(int index, IComponent component) {
20  _type = component.GetType();
21  _componentMembers = null;
22 
23  this.index = index;
24  fullTypeName = _type.FullName;
25 
26  var memberInfos = _type.GetPublicMemberInfos();
27  members = new SerializableMember[memberInfos.Count];
28  for (int i = 0; i < memberInfos.Count; i++) {
29  var info = memberInfos[i];
30  members[i] = new SerializableMember(
31  info.name, info.GetValue(component)
32  );
33  }
34  }
35 
36  public IComponent CreateComponent(Entity entity) {
37  if(_type == null) {
38  _type = fullTypeName.ToType();
39 
40  if(_type == null) {
42  "Type '" + fullTypeName +
43  "' doesn't exist in any assembly!",
44  "Please check the full type name."
45  );
46  }
47 
48  if(!_type.ImplementsInterface<IComponent>()) {
50  "Type '" + fullTypeName +
51  "' doesn't implement IComponent!",
52  typeof(ComponentBlueprint).Name +
53  " only supports IComponent."
54  );
55  }
56  }
57 
58  var component = entity.CreateComponent(index, _type);
59 
60  if(_componentMembers == null) {
61  var memberInfos = _type.GetPublicMemberInfos();
62  _componentMembers = new Dictionary<string, PublicMemberInfo>(
63  memberInfos.Count
64  );
65  for (int i = 0; i < memberInfos.Count; i++) {
66  var info = memberInfos[i];
67  _componentMembers.Add(info.name, info);
68  }
69  }
70 
71  for (int i = 0; i < members.Length; i++) {
72  var member = members[i];
73 
74  PublicMemberInfo memberInfo;
75  if(!_componentMembers.TryGetValue(member.name, out memberInfo)) {
77  "Could not find member '" + member.name +
78  "' in type '" + _type.FullName + "'!",
79  "Only non-static public members are supported."
80  );
81  }
82 
83  memberInfo.SetValue(component, member.value);
84  }
85 
86  return component;
87  }
88  }
89 
91  public ComponentBlueprintException(string message, string hint) :
92  base(message, hint) {
93  }
94  }
95 }
IComponent CreateComponent(int index, Type type)
Definition: Entity.cs:342
Base exception used by Entitas.