GameObject RestoreGameObject(SerializedGameObject serializedGameObject)
            {
                GameObject gameObject = new GameObject();

                Undo.RegisterCreatedObjectUndo(gameObject, "Create");

                deserializedObjects.Add(gameObject);
                deserializedGameObjects.Add(new DeserializedGameObject(serializedGameObject, gameObject));
                EditorJsonUtility.FromJsonOverwrite(serializedGameObject.serializedData, gameObject);
                RestoreObjectReference(serializedGameObject.savedInstanceIDs, gameObject);

                RestoreComponents(gameObject, serializedGameObject.serializedComponents);
                return(gameObject);
            }
            private void SerializeGameObject(GameObject gameObject)
            {
                var parent = gameObject.transform.parent;
                SerializedGameObject sgo = new SerializedGameObject
                {
                    serializedData   = EditorJsonUtility.ToJson(gameObject, false),
                    savedInstanceIDs = GetInstanceReferenceIDs(gameObject),
                    scenePath        = gameObject.scene.path,
                    hasParent        = parent != null
                };

                sgo.parentID     = sgo.hasParent ? parent.GetInstanceID() : 0;
                sgo.siblingIndex = gameObject.transform.GetSiblingIndex();

                sgo.childCount        = gameObject.transform.childCount;
                sgo.indexOfFirstChild = serializedSelection.serializedGameObjects.Count + 1;

                foreach (var component in gameObject.GetComponents <Component>())
                {
                    if (component == null)
                    {
                        continue;
                    }
                    var serializedComponent = SerializeComponent(component);
                    sgo.serializedComponents.Add(serializedComponent);
                }

                serializedSelection.serializedGameObjects.Add(sgo);

                if (gameObject.isStatic)
                {
                    serializedSelection.foundStatic = true;
                    Debug.LogWarning("PlayModeSaver tried to serialize static GameObject " + gameObject +
                                     ". This is not allowed.");
                }

                foreach (Transform child in gameObject.transform)
                {
                    SerializeGameObject(child.gameObject);
                }
            }
 public DeserializedGameObject(SerializedGameObject serializedGameObject, GameObject gameObject)
 {
     this.serializedGameObject = serializedGameObject;
     this.gameObject           = gameObject;
 }