示例#1
0
        public void Rename(ProjectItemMeta meta, string name)
        {
            //Implementation could be removed in your own IProjectManager implementation

            if (PersistentData != null && PersistentData.Length != 0)
            {
                if (PersistentData.Length > 1)
                {
                    Dictionary <long, PersistentData> dataDictionary = PersistentData.ToDictionary(d => d.InstanceId);

                    PersistentData persistentData;
                    if (dataDictionary.TryGetValue(meta.Descriptor.InstanceId, out persistentData))
                    {
                        PersistentObjects.PersistentObject persistentObject = persistentData.AsPersistentObject;
                        persistentObject.name = name;
                        if (meta.Descriptor.Components != null)
                        {
                            for (int i = 0; i < meta.Descriptor.Components.Length; ++i)
                            {
                                if (dataDictionary.TryGetValue(meta.Descriptor.Components[i].InstanceId, out persistentData))
                                {
                                    persistentObject      = persistentData.AsPersistentObject;
                                    persistentObject.name = name;
                                }
                            }
                        }
                    }
                }
                else
                {
                    PersistentObjects.PersistentObject persistentObject = PersistentData[0].AsPersistentObject;
                    persistentObject.name = name;
                }
            }
        }
示例#2
0
        public override object WriteTo(object obj, Dictionary <long, UnityObject> objects)
        {
            obj = base.WriteTo(obj, objects);

            if (baseObjectData != null)
            {
                //prevent name to be overriden with name stored m_baseObjectData.name
                PersistentObjects.PersistentObject persistentObj = baseObjectData.AsPersistentObject;
                if (persistentObj != null)
                {
                    persistentObj.name = name;
                }
                baseObjectData.WriteTo(obj, objects);
            }

            Type type = obj.GetType();

            if (!type.IsScript())
            {
                throw new ArgumentException(string.Format("obj of type {0} is not subclass of {1}", type, typeof(MonoBehaviour)), "obj");
            }

            do
            {
                FieldInfo[] fields = type.GetSerializableFields();
                for (int i = 0; i < fields.Length; ++i)
                {
                    FieldInfo field = fields[i];
                    string    name  = field.Name;
                    if (!this.fields.ContainsKey(field.Name))
                    {
                        FormerlySerializedAsAttribute formelySerializedAs = field.GetCustomAttributes(typeof(FormerlySerializedAsAttribute), false).FirstOrDefault() as FormerlySerializedAsAttribute;
                        if (formelySerializedAs == null)
                        {
                            continue;
                        }
                        name = formelySerializedAs.oldName;
                        if (!this.fields.ContainsKey(name))
                        {
                            continue;
                        }
                    }

                    bool isArray   = field.FieldType.IsArray;
                    Type fieldType = field.FieldType;
                    if (isArray)
                    {
                        fieldType = fieldType.GetElementType();
                    }

                    DataContract value = this.fields[name];
                    if (fieldType.IsSubclassOf(typeof(UnityObject)) || fieldType == typeof(UnityObject))
                    {
                        if (isArray)
                        {
                            if (value.AsPrimitive == null)
                            {
                                continue;
                            }

                            if (!(value.AsPrimitive.ValueBase is long[]))
                            {
                                continue;
                            }

                            long[] instanceIds = (long[])value.AsPrimitive.ValueBase;
                            Array  objectsFoundByInstanceId = Array.CreateInstance(fieldType, instanceIds.Length);
                            for (int j = 0; j < instanceIds.Length; ++j)
                            {
                                object o = objects.Get(instanceIds[j]);
                                objectsFoundByInstanceId.SetValue(o, j);
                            }
                            field.SetValue(obj, objectsFoundByInstanceId);
                        }
                        else
                        {
                            if (value.AsPrimitive == null)
                            {
                                continue;
                            }
                            if (!(value.AsPrimitive.ValueBase is long))
                            {
                                continue;
                            }

                            long instanceId = (long)value.AsPrimitive.ValueBase;
                            if (!objects.ContainsKey(instanceId))
                            {
                                continue;
                            }

                            object objectFoundByInstanceId = objects[instanceId];
                            try
                            {
                                field.SetValue(obj, objectFoundByInstanceId);
                            }
                            catch
                            {
                                Debug.LogError(instanceId);
                                throw;
                            }
                        }
                    }
                    else
                    {
                        if (value == null)
                        {
                            if (field.FieldType.IsValueType())
                            {
                                continue;
                            }
                            field.SetValue(obj, value);
                        }
                        else
                        {
                            if (field.FieldType.IsSubclassOf(typeof(UnityEventBase)))
                            {
                                if (value.AsUnityEvent == null)
                                {
                                    continue;
                                }
                                PersistentUnityEventBase persistentUnityEvent = value.AsUnityEvent;
                                UnityEventBase           unityEvent           = (UnityEventBase)Activator.CreateInstance(field.FieldType);
                                persistentUnityEvent.WriteTo(unityEvent, objects);
                                field.SetValue(obj, unityEvent);
                            }
                            else
                            {
                                if (!field.FieldType.IsEnum())
                                {
                                    object fieldValue;
                                    if (field.FieldType.IsPrimitive() || field.FieldType.IsArray())
                                    {
                                        PrimitiveContract primitive = value.AsPrimitive;
                                        if (primitive == null ||
                                            primitive.ValueBase == null && field.FieldType.IsValueType() ||
                                            primitive.ValueBase != null && !field.FieldType.IsAssignableFrom(primitive.ValueBase.GetType()))
                                        {
                                            continue;
                                        }
                                        fieldValue = primitive.ValueBase;
                                    }
                                    else
                                    {
                                        fieldValue = value.Data;
                                    }

                                    field.SetValue(obj, fieldValue);
                                    if (fieldValue is IEnumerable)
                                    {
                                        IEnumerable enumerable = (IEnumerable)fieldValue;
                                        foreach (object o in enumerable)
                                        {
                                            if (o is IRTSerializable)
                                            {
                                                IRTSerializable rtSerializable = (IRTSerializable)o;
                                                rtSerializable.Deserialize(objects);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (fieldValue is IRTSerializable)
                                        {
                                            IRTSerializable rtSerializable = (IRTSerializable)fieldValue;
                                            rtSerializable.Deserialize(objects);
                                        }
                                    }
                                }
                                else// if (field.FieldType.IsEnum)
                                {
                                    PrimitiveContract primitive = value.AsPrimitive;
                                    if (primitive == null ||
                                        primitive.ValueBase == null && field.FieldType.IsValueType() ||
                                        primitive.ValueBase != null && primitive.ValueBase.GetType() != typeof(uint))
                                    {
                                        continue;
                                    }

                                    var val = Enum.ToObject(field.FieldType, primitive.ValueBase);
                                    field.SetValue(obj, val);
                                }
                            }
                        }
                    }
                }
                type = type.BaseType();
            }while (type.IsScript());

            return(obj);
        }