Пример #1
0
    //////////////////////////////////////////////////////////////////////////
    public void iSave(SerializationManager.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 SerializationManager.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);
    }
Пример #2
0
    public void iLoad(SerializationManager.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 GetObjectData(object obj, SerializationInfo info, StreamingContext context)
        {
            if (obj is SerializableObject so)
            {
                // save component list
                so.implGetSerializableComponents(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 SerializationManager.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   = SerializationManager.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 SerializationManager.SerializationInfoWrapper(info);

            foreach (var n in componentList)
            {
                so.implValidateComponent(n, infoWrapper);
            }

            return(so);
        }
 //////////////////////////////////////////////////////////////////////////
 private void implValidateComponent(ComponentPath componentPath, SerializationManager.SerializationInfoWrapper info)
 {
     info.Prefix = componentPath.InfoPrefix;
     implFindGameObjectComponent(componentPath.m_Path, Type.GetType(componentPath.m_Type), out var component);
     (component as SerializationManager.IComponent)?.iLoad(info);
 }
 public void iLoad(SerializationManager.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(SerializationManager.SerializationInfoWrapper info)
 {
     info.AddValue(c_KeyPosition, gameObject.transform.localPosition);
     info.AddValue(c_KeyRotation, gameObject.transform.localRotation);
     info.AddValue(c_KeyScale, gameObject.transform.localScale);
 }