public ChildObjectData(SerializationInfo info, StreamingContext context)
 {
     this.DeserializeInfo    = info;
     this.DeserializeContext = context;
     this.Uid           = new PersistentUid(info.GetString("sp_uid"));
     this.ComponentType = info.GetValue("sp_t", typeof(System.Type)) as System.Type;
 }
        static public PersistentObject GetPrecreatedPersistentObject(PersistentUid uid)
        {
            PersistentObject persistentObject;

            Instance.m_PrecreatedPersistentObjects.TryGetValue(uid, out persistentObject);
            return(persistentObject);
        }
 static public void UnregisterPersistentObject(PersistentUid uid)
 {
     if (Instance)
     {
         Instance.m_PrecreatedPersistentObjects.Remove(uid);
     }
 }
 static public void RegisterPersistentObject(PersistentUid uid, PersistentObject persistentObject)
 {
     if (Instance)
     {
         Instance.m_PrecreatedPersistentObjects.Add(uid, persistentObject);
         Debug.Log($"Registering created object: {persistentObject.name} with uid: {uid}");
     }
 }
Example #5
0
        public void Remove(PersistentObject prefab)
        {
            PersistentUid uid = prefab.PrefabUid;

            if (_prefabs.ContainsKey(uid))
            {
                _prefabs.Remove(uid.ToString());
                Debug.Log($"Prefab {prefab.gameObject.name} removed");
            }


            Prefabs    = new List <PersistentObject>(_prefabs.Values);
            PrefabUids = new List <string>(_prefabs.Keys);
        }
        public void OnDeserialize(SerializationInfo info, StreamingContext context, IAssetBundle assetBundle)
        {
            _persistenceUid = new PersistentUid(info.GetString("uid"));
            _uidSet         = true;

            this.transform.position   = (Vector3)info.GetValue("pos", typeof(Vector3));
            this.transform.rotation   = (Quaternion)info.GetValue("rot", typeof(Quaternion));
            this.transform.localScale = (Vector3)info.GetValue("scale", typeof(Vector3));

            SerializationInfoEnumerator e = info.GetEnumerator();

            while (e.MoveNext())
            {
                Type componentType = TypeUtil.FindType(e.Name, true);
                if (componentType == null)
                {
                    continue;
                }
                Component component = this.GetComponent(componentType);
                if (component == null)
                {
                    continue;
                }

                SerializableComponent serializedComponent = (SerializableComponent)e.Value;

                ComponentSerializationUtility.DeserializeComponent(ref component, serializedComponent.DeserializeInfo);
            }

            int cnt = info.GetInt32("count");

            if (cnt > 0)
            {
                var lst = new List <IPersistentUnityObject>();
                this.GetComponentsInChildren <IPersistentUnityObject>(true, lst);
                for (int i = 0; i < cnt; i++)
                {
                    ChildObjectData data = (ChildObjectData)info.GetValue(i.ToString(), typeof(ChildObjectData));
                    if (data != null && data.ComponentType != null)
                    {
                        IPersistentUnityObject pobj = (from o in lst where o.Uid == data.Uid select o).FirstOrDefault();
                        if (pobj != null)
                        {
                            pobj.OnDeserialize(data.DeserializeInfo, data.DeserializeContext, assetBundle);
                        }
                    }
                }
            }
        }
        void Start()
        {
            // Created from a prefab
            if (_isPrefab && Application.isPlaying)
            {
                if (_uidSet == false)
                {
                    Debug.Log($"SETTING NEW ID FOR {gameObject.name}");
                    _persistenceUid  = PersistentUid.NewUid();
                    _isPrefab        = false;
                    _linkedPrefabUid = _prefabUid;
                }
            }

            PersistenceController.RegisterPersistentObject(Uid, this);
        }
Example #8
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            position = EditorGUI.PrefixLabel(position, label);
            float w  = Mathf.Min(position.width, 60f);
            var   r2 = new Rect(position.xMax - w, position.yMin, w, position.height);
            var   r1 = new Rect(position.xMin, position.yMin, Mathf.Max(position.width - w, 0f), position.height);

            var uidProp = property.FindPropertyRelative("_uid");
            var value   = uidProp.stringValue;

            GUI.enabled = false;
            EditorGUI.SelectableLabel(r1, value, EditorStyles.textField);
            GUI.enabled = true;

            if (GUI.Button(r2, "New Id"))
            {
                value = PersistentUid.NewUid().Value;
                uidProp.stringValue = value.ToString();
            }

            EditorGUI.EndProperty();
        }
Example #9
0
        public override void OnInspectorGUI()
        {
            GUILayout.Label($"Prefab status: {PrefabUtility.GetPrefabInstanceStatus(persistentObject)}");

            EditorGUI.BeginChangeCheck();

            if (isPrefab && prevPrefab == null)
            {
                prevPrefab = GetPrefab();

                if (prevPrefab)
                {
                    PersistenceController.RegisterPrefab(prevPrefab);
                }
            }

            EditorGUILayout.PropertyField(isPrefabProp);

            if (EditorGUI.EndChangeCheck())
            {
                isPrefab = isPrefabProp.boolValue;
                if (isPrefab)
                {
                    PersistentObject prefab = GetPrefab();

                    if (prefab != null)
                    {
                        PersistenceController.RegisterPrefab(prefab);

                        prevPrefab = prefab;
                    }

                    if (prefabUidIdProp.stringValue == "")
                    {
                        prefabUidIdProp.stringValue = PersistentUid.NewUid().Value;
                    }
                }
                else
                {
                    if (prevPrefab)
                    {
                        PersistenceController.UnregisterPrefab(prevPrefab);
                    }

                    if (persistenceUidIdProp.stringValue == "")
                    {
                        persistenceUidIdProp.stringValue = PersistentUid.NewUid().Value;
                    }
                }
            }

            if (isPrefab && prevPrefab == null)
            {
                EditorGUILayout.HelpBox("Is Prefab property was set to true, but no corresponding prefab was found", MessageType.Warning);
            }

            if (isPrefab)
            {
                EditorGUILayout.PropertyField(prefabUidProp);
            }
            else
            {
                EditorGUILayout.PropertyField(persistenceUidProp);

                if (linkedPrefabUidIdProp.stringValue != "")
                {
                    EditorGUILayout.PropertyField(linkedPrefabUidProp);
                }
            }

            EditorGUILayout.PropertyField(componentsToSerializeProp, true);

            serializedObject.ApplyModifiedProperties();

            if (GUILayout.Button("Update prefab map"))
            {
                UpdatePrefabMap();
            }
        }