public void iLoad(Serialization.SerializationInfoWrapper info) { // unpack data, remove undeclared components var typeList = info.GetValue <List <ComponentData> >("componentList"); foreach (var componentData in typeList) { var type = Type.GetType(componentData.m_Type); if (type == null) { continue; } var component = gameObject.GetComponent(type); if (component == null) { component = gameObject.AddComponent(type); } foreach (var field in componentData.m_FieldDictionary) { type.GetField(field.Key, BindingFlags.Public | BindingFlags.Instance).SetValue(component, field.Value); } } }
////////////////////////////////////////////////////////////////////////// public void iSave(Serialization.SerializationInfoWrapper info) { // get all components, serialize thous fields var componentDataList = new List <ComponentData>(); var componentList = new List <Component>(); gameObject.GetComponents(componentList); foreach (var serializableComponent in componentList .Where(n => n is Serialization.IComponent == false && n is SerializableObject == false && n is UniqueIDComponent == false)) { var type = serializableComponent.GetType(); var data = new ComponentData() { m_Type = type.FullName }; componentDataList.Add(data); info.Prefix += type.FullName; foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Instance)) { //field.Attributes SerializeField //field.IsNotSerialized data.m_FieldDictionary[field.Name] = field.GetValue(serializableComponent); } info.Prefix = info.Prefix.Remove(info.Prefix.Length - type.FullName.Length); } //componentDataList.Select(n => n.m_Type) info.AddValue("componentList", componentDataList); }
public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) { if (obj is SerializableObject so) { // save component list so._GetSerializableComponents(out var componentPath, out var componentList); info.AddValue("componentList", componentPath); info.AddValue("guid", so.m_UniqueID.ID); info.AddValue("resourcePath", so.m_ResourcePath); // save component data var infoWrapper = new Serialization.SerializationInfoWrapper(info); for (var n = 0; n < componentList.Count; n++) { var path = componentPath[n]; infoWrapper.Prefix = path.InfoPrefix; componentList[n].iSave(infoWrapper); } } }
public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { // find the object by guid if not presented in the scene create one var guid = info.GetString("guid"); var so = Serialization.Instance.GetSerializableObject(guid); if (so == null) { var resourcePath = info.GetString("resourcePath"); if (string.IsNullOrEmpty(resourcePath)) { return(null); } var prefab = Resources.Load(resourcePath); if (prefab == null) { return(null); } so = (Instantiate(prefab) as GameObject)?.GetComponent <SerializableObject>(); } if (so == null) { // create new object return(null); } so.m_UniqueID.ID = guid; // validate components var componentList = info.GetValue("componentList", typeof(List <ComponentPath>)) as List <ComponentPath>; var infoWrapper = new Serialization.SerializationInfoWrapper(info); foreach (var n in componentList) { so._ValidateComponent(n, infoWrapper); } return(so); }
////////////////////////////////////////////////////////////////////////// private void _ValidateComponent(ComponentPath componentPath, Serialization.SerializationInfoWrapper info) { info.Prefix = componentPath.InfoPrefix; _FindGameObjectComponent(componentPath.m_Path, Type.GetType(componentPath.m_Type), out var component); (component as Serialization.IComponent)?.iLoad(info); }
public void iLoad(Serialization.SerializationInfoWrapper info) { gameObject.transform.localPosition = info.GetValue <Vector3>(c_KeyPosition); gameObject.transform.localRotation = info.GetValue <Quaternion>(c_KeyRotation); gameObject.transform.localScale = info.GetValue <Vector3>(c_KeyScale); }
////////////////////////////////////////////////////////////////////////// public void iSave(Serialization.SerializationInfoWrapper info) { info.AddValue(c_KeyPosition, gameObject.transform.localPosition); info.AddValue(c_KeyRotation, gameObject.transform.localRotation); info.AddValue(c_KeyScale, gameObject.transform.localScale); }