public void AfterDeserialize() { // resolve references foreach (var cmp in components) { System.Type type = cmp.GetType(); foreach (var pair in refs) { string fieldName = pair.Key.Substring(1); System.Reflection.FieldInfo fi = type.GetField(fieldName); if (fi == null) { continue; } SerializedComponent resolvedComponent = ResolveComponentFromId(pair.Value); if (resolvedComponent != null) { if (fi.FieldType != resolvedComponent.GetType()) { Debug.LogWarning("deserialize type mismatch", cmp); } else { fi.SetValue(cmp, resolvedComponent); } } else { // TODO resolve gameobject references // this is probably not needed when there are component references } } cmp.AfterDeserialize(); } }
public static T ResolveComponentFromId <T>(int id) where T : SerializedComponent { if (serializedComponents.ContainsKey(id)) { SerializedComponent sc = serializedComponents[id]; if (sc.GetType() != typeof(T)) { Debug.LogWarning("resolvecomponent type mismatch: " + sc.GetType().ToString() + " " + typeof(T).ToString()); return(default(T)); } return((T)sc); } // if( idMap.ContainsKey( id ) && serializedComponents.ContainsKey( idMap[ id ] ) ) // return serializedComponents[ idMap[ id ] ]; Debug.LogWarning("failed to resolve component: " + id); return(null); }