static void GetReferencesForSerialization(GameObject gameObject, SerializableGameObject sGObject) { if (gameObject == null) { return; } VerifyReferencedGameObject(gameObject, sGObject); Component[] copiedComponents = gameObject.GetComponents <Component>(); if (copiedComponents != null) { for (int c = 0; c < copiedComponents.Length; c++) { VerifyReferencedComponents(copiedComponents[c], sGObject.components[c]); } } if (gameObject.transform.childCount > 0) { for (int s = 0; s < gameObject.transform.childCount; s++) { GetReferencesForSerialization(gameObject.transform.GetChild(s).gameObject, sGObject.children[s]); } } }
static void Copy() { EditorUtility.DisplayProgressBar("Copying GameObjects", "...", 0f); objectReferenceProperties.Clear(); gameObjectsSerializableReferences.Clear(); referencedGameObjects.Clear(); componentsSerializableReferences.Clear(); referencedComponents.Clear(); currentGameObjectId = 0; currentComponentId = 0; GameObject target = Selection.activeGameObject; SerializableGameObject sGObject = GetSerializableGameObject(target); EditorUtility.DisplayProgressBar("Copying GameObjects", "...", 0.5f); GetReferencesForSerialization(target, sGObject); EditorUtility.DisplayProgressBar("Copying GameObjects", "...", 1f); if (sGObject != null) { EditorGUIUtility.systemCopyBuffer = sGObject.GetString(); } EditorUtility.ClearProgressBar(); }
public ComponentCacheDeserializer(SerializableGameObject serialized, GameObject targetObject) { _components = new List <ISerializableComponentBase>(); _targetObject = targetObject; _serializedGameObject = serialized; foreach (Reference reference in serialized.Components) { if (!Deserializer.ContainsObject(reference.ID)) { continue; } _targetComponentCount++; Deserializer.GetSerializedObject <ISerializableComponentBase>(reference.ID, serializableComponent => { ReceiveComponent(serializableComponent); }); } _finishedInitializing = true; ExecuteDeserialization(); }
static void VerifyGameObject(GameObject gameObject, SerializableGameObject sGObject) { for (int i = 0; i < gameObjectsDeserializableReferences.Count; i++) { if (sGObject.gameObjectId == gameObjectsDeserializableReferences[i].gameObjectReferenceId) { Type type = componentsToSetGameObject[i].GetType(); BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly; PropertyInfo[] pinfos = type.GetProperties(flags); foreach (var pinfo in pinfos) { if (pinfo.CanWrite && pinfo.Name == gameObjectPropertyNames[i]) { try { pinfo.SetValue(componentsToSetGameObject[i], gameObject, null); } catch {} } } FieldInfo[] finfos = type.GetFields(flags); foreach (var finfo in finfos) { if (finfo.Name == gameObjectPropertyNames[i]) { try { finfo.SetValue(componentsToSetGameObject[i], gameObject); } catch {} } } } } }
// On Destroy script, save public void OnDestroy() { foreach (GameObject go in gameObjects) { SerializableGameObject s = new SerializableGameObject(go); dataContainer.Add(s); } Persister.Save <GameObjectDataContainer>(filepath, dataContainer); }
public void Load() { SerializableGameObject saveObject = _data.Load(Path.Combine(_path, _fileName)); _gameObject.name = saveObject.Name; _gameObject.transform.position = saveObject.Pos; _gameObject.SetActive(saveObject.IsEnable); Debug.Log(saveObject); }
static void VerifyReferencedGameObject(GameObject gameObject, SerializableGameObject sGObject) { for (int i = 0; i < referencedGameObjects.Count; i++) { if (gameObject == referencedGameObjects[i]) { gameObjectsSerializableReferences[i].gameObjectReferenceId = sGObject.gameObjectId; objectReferenceProperties[i].propertyValue = gameObjectsSerializableReferences[i].GetString(); } } }
public static string GameObjectToJSON(GameObject gobj) { if (gobj == null) { return("{}"); } SerializableGameObject sgobj = new SerializableGameObject(gobj); string retval = JsonConvert.SerializeObject(sgobj, Formatting.Indented); return(retval); }
static bool isDuplicatedComponent(GameObject gameObject, SerializableGameObject sGObject, SerializableComponent component) { int numberOfComponents = 0; foreach (var copiedComponent in sGObject.components) { if (copiedComponent.componentName == component.componentName) { numberOfComponents++; } } return(gameObject.GetComponent(component.componentName) != null && numberOfComponents == 1); }
public void Save() { if (!Directory.Exists(Path.Combine(_path))) { Directory.CreateDirectory(_path); } var balans = new SerializableGameObject { Money = WalletManager.instance.GetBalans(TypeBalans.Money), Crystal = WalletManager.instance.GetBalans(TypeBalans.Crystal) }; _data.Save(balans, Path.Combine(_path, _fileName)); }
public void Save() { if (!Directory.Exists(Path.Combine(_path))) { Directory.CreateDirectory(_path); } var player = new SerializableGameObject { Pos = ServiceLocatorMonoBehaviour.GetService <CharacterController>().gameObject.transform.position, Name = "NEDNAR", IsEnable = true }; _data.Save(player, Path.Combine(_path, FILE_NAME)); }
public void Save() { if (!Directory.Exists(Path.Combine(_path))) { Directory.CreateDirectory(_path); } var saveObject = new SerializableGameObject { Name = _gameObject.name, Pos = _gameObject.transform.position, IsEnable = _gameObject.activeSelf }; _data.Save(saveObject, Path.Combine(_path, _fileName)); }
public void Save() { if (!Directory.Exists(Path.Combine(_path))) { Directory.CreateDirectory(_path); } var player = new SerializableGameObject { Pos = Main.Instance.Player.position, Name = Main.Instance.Player.name, IsEnable = true }; _data.Save(player, Path.Combine(_path, _fileName)); }
static void Paste() { EditorUtility.DisplayProgressBar("Paste GameObjects", "...", 0f); string componentsText = EditorGUIUtility.systemCopyBuffer; //Debug.Log("gil - ComponentAsText::Paste - " + componentsText); gameObjectsDeserializableReferences.Clear(); componentsToSetGameObject.Clear(); gameObjectPropertyNames.Clear(); componentsDeserializableReferences.Clear(); componentsToSetComponent.Clear(); componentPropertyNames.Clear(); SerializableGameObject sGObject = SerializableGameObject.LoadFromText(componentsText); EditorUtility.DisplayProgressBar("Paste GameObjects", "...", 0.3f); if (sGObject != null) { //Debug.Log("gil - ComponentAsText::Paste - " + Selection.gameObjects.Length); foreach (var targetGameObject in Selection.gameObjects) { if (!targetGameObject) { continue; } Debug.Log("gil - ComponentAsText::Paste - GameObject.name = " + targetGameObject.name); PasteSerializableGameObject(targetGameObject, sGObject); EditorUtility.DisplayProgressBar("Paste GameObjects", "...", 0.6f); GetReferencesForDeserialization(targetGameObject, sGObject); EditorUtility.DisplayProgressBar("Paste GameObjects", "...", 1f); } } EditorUtility.ClearProgressBar(); }
static void PasteSerializableGameObject(GameObject gameObject, SerializableGameObject sGObject) { gameObject.name = sGObject.name; foreach (var copiedComponent in sGObject.components) { if (copiedComponent == null) { continue; } Component comp; if (copiedComponent.componentName == "Transform") { comp = (Component)gameObject.transform; } else if (isDuplicatedComponent(gameObject, sGObject, copiedComponent)) { comp = gameObject.GetComponent(copiedComponent.componentName); } else { comp = UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, "Assets/Editor/Tools/CopyComponents/ComponentAsText.cs (134,21)", copiedComponent.componentName); } if (comp == null) { continue; } copiedComponent.ApplySerializedDataTo(comp); } GameObject childGObject; foreach (var sGChild in sGObject.children) { childGObject = new GameObject(); childGObject.transform.parent = gameObject.transform; PasteSerializableGameObject(childGObject, sGChild); } }
static SerializableGameObject GetSerializableGameObject(GameObject gameObject) { if (gameObject == null) { return(null); } Component[] copiedComponents = gameObject.GetComponents <Component>(); if (copiedComponents != null) { SerializableGameObject sGObject = new SerializableGameObject(); sGObject.gameObjectId = currentGameObjectId; sGObject.name = gameObject.name; SerializableComponent sComponent; currentGameObjectId++; for (int c = 0; c < copiedComponents.Length; c++) { sComponent = GetSerializableComponent(copiedComponents[c]); sGObject.components.Add(sComponent); } if (gameObject.transform.childCount > 0) { SerializableGameObject sChildrenGObject; for (int s = 0; s < gameObject.transform.childCount; s++) { sChildrenGObject = GetSerializableGameObject(gameObject.transform.GetChild(s).gameObject); sGObject.children.Add(sChildrenGObject); } } return(sGObject); } return(null); }
static void GetReferencesForDeserialization(GameObject gameObject, SerializableGameObject sGObject) { if (gameObject == null) { return; } //Debug.Log("gil - ComponentAsText::GetReferencesForDeserialization - Testing " + gameObject.name + " with sGObject " + sGObject.name); VerifyGameObject(gameObject, sGObject); Component[] copiedComponents = gameObject.GetComponents <Component>(); if (copiedComponents != null) { List <SerializableComponent> sComponents = sGObject.components; for (int c = 0; c < sComponents.Count; c++) { for (int i = 0; i < copiedComponents.Length; i++) { if (copiedComponents[i] != null && copiedComponents[i].GetType().Name == sComponents[c].componentName) { VerifyComponent(copiedComponents[i], sComponents[c]); copiedComponents[i] = null; break; } } } } if (gameObject.transform.childCount > 0) { for (int s = 0; s < gameObject.transform.childCount; s++) { GetReferencesForDeserialization(gameObject.transform.GetChild(s).gameObject, sGObject.children[s]); } } }
public SerializableGameObject ToSerializableGameObject(GameObject targetObject) { Component[] allComponents; allComponents = targetObject.GetComponents(typeof(Component)); List <SerializableComponent> serializableComponents = new List <SerializableComponent>(); foreach (Component comp in allComponents) { if (comp.GetType() == typeof(Transform)) { continue; } SerializableComponent sComp = new SerializableComponent(comp.GetType(), null, null); List <string> propertiesNames = new List <string>(); List <object> values = new List <object>(); foreach (PropertyInfo pInfo in comp.GetType().GetProperties()) { try { //trying to store the component property data object pValue = pInfo.GetValue(targetObject.GetComponent(comp.GetType())); if (pValue.GetType() == typeof(Vector2)) { pValue = new SerializableVector2((Vector2)pValue); } else if (pValue.GetType() == typeof(Vector3)) { pValue = new SerializableVector3((Vector3)pValue); } else if (pValue.GetType() == typeof(Quaternion)) { pValue = new SerializableQuaternion((Quaternion)pValue); } else if (pValue.GetType() == typeof(Sprite)) { pValue = new SerializableSprite((Sprite)pValue); } else if (pValue.GetType() == typeof(Color)) { pValue = new SerializableColor((Color)pValue); } else if (pValue.GetType() == typeof(Vector4)) { pValue = new SerializableVector4((Vector4)pValue); } else if (pValue.GetType() == typeof(Bounds)) { pValue = new SerializableBounds((Bounds)pValue); } else if (pValue.GetType() == typeof(Matrix4x4)) { pValue = new Serializable4x4Matrix((Matrix4x4)pValue); } else if (pValue.GetType() == typeof(Vector2[])) { Vector2[] data = (Vector2[])pValue; SerializableVector2[] vectors = new SerializableVector2[data.Length]; for (int i = 0; i < data.Length; i++) { vectors[i] = new SerializableVector2(data[i]); } pValue = vectors; } else if (pValue.GetType() == typeof(Vector3[])) { Vector3[] data = (Vector3[])pValue; SerializableVector3[] vectors = new SerializableVector3[data.Length]; for (int i = 0; i < data.Length; i++) { vectors[i] = new SerializableVector3(data[i]); } pValue = vectors; } else if (pValue.GetType() == typeof(RuntimeAnimatorController)) { pValue = new SerializableAnimatorController((RuntimeAnimatorController)pValue); } else if (!(pValue.GetType().IsSerializable) || !(pValue.GetType().IsArray&& pValue.GetType().GetElementType().IsSerializable)) { continue; } values.Add(pValue); propertiesNames.Add(pInfo.Name); } catch (Exception e) { continue; } } sComp.propertiesNames = propertiesNames; foreach (object obj in values) { if (obj.GetType().IsSerializable == false) { values.Remove(obj); propertiesNames.Remove(propertiesNames[values.IndexOf(obj)]); } Debug.Log(".............." + obj.GetType() + ".............."); } sComp.values = values; serializableComponents.Add(sComp); } List <SerializableGameObject> childs = new List <SerializableGameObject>(); foreach (Transform child in targetObject.transform) { childs.Add(ToSerializableGameObject(child.gameObject)); } List <float> position = new List <float>(); position.Add(targetObject.transform.position.x); position.Add(targetObject.transform.position.y); position.Add(targetObject.transform.position.z); List <float> rotation = new List <float>(); rotation.Add(targetObject.transform.eulerAngles.x); rotation.Add(targetObject.transform.eulerAngles.y); rotation.Add(targetObject.transform.eulerAngles.z); List <float> scale = new List <float>(); scale.Add(targetObject.transform.localScale.x); scale.Add(targetObject.transform.localScale.y); scale.Add(targetObject.transform.localScale.z); List <List <float> > transformData = new List <List <float> >(); transformData.Add(position); transformData.Add(rotation); transformData.Add(scale); SerializableGameObject sObject = new SerializableGameObject(targetObject.name, serializableComponents, transformData, childs); return(sObject); }
/** * Add a Serializable Game Object to this Data Container * * @param serializableGameObject The object to add to the container **/ public void Add(SerializableGameObject serializableGameObject) { data.Add(serializableGameObject); }
public void DeserializeGameObject(SerializableGameObject serializableObject, Transform parent) { GameObject myObject = Instantiate(new GameObject(), Vector3.zero, Quaternion.identity, parent); myObject.name = serializableObject.name; foreach (SerializableComponent sComp in serializableObject.serializableComponents) { Type compType = sComp.componentType as Type; myObject.AddComponent(compType); for (int i = 0; i < sComp.propertiesNames.Count; i++) { try { string pName = sComp.propertiesNames[i]; object loadPropertyValue = sComp.values[i]; var pValue = loadPropertyValue; if (loadPropertyValue.GetType() == typeof(SerializableVector2)) { SerializableVector2 vector = (SerializableVector2)loadPropertyValue; pValue = new Vector2(vector.x, vector.y); } else if (loadPropertyValue.GetType() == typeof(SerializableVector3)) { SerializableVector3 vector = (SerializableVector3)loadPropertyValue; pValue = new Vector3(vector.x, vector.y, vector.z); } else if (loadPropertyValue.GetType() == typeof(SerializableQuaternion)) { SerializableQuaternion quat = (SerializableQuaternion)loadPropertyValue; pValue = new Quaternion(quat.x, quat.y, quat.z, quat.w); } else if (loadPropertyValue.GetType() == typeof(SerializableSprite)) { SerializableSprite sSprite = (SerializableSprite)loadPropertyValue; foreach (Sprite sprite in allSprites) { myObject.GetComponent <SpriteRenderer>().sortingOrder = 31; if (sprite.name.Equals(sSprite.spriteName)) { myObject.GetComponent <SpriteRenderer>().sprite = sprite; continue; } } } else if (loadPropertyValue.GetType() == typeof(SerializableColor)) { SerializableColor sColor = (SerializableColor)loadPropertyValue; pValue = new Color(sColor.r, sColor.g, sColor.b, sColor.a); } else if (loadPropertyValue.GetType() == typeof(SerializableVector4)) { SerializableVector4 sVector = (SerializableVector4)loadPropertyValue; pValue = new Vector4(sVector.x, sVector.y, sVector.z, sVector.w); } else if (loadPropertyValue.GetType() == typeof(SerializableBounds)) { SerializableBounds sBounds = (SerializableBounds)loadPropertyValue; pValue = new Bounds(new Vector3(sBounds.center[0], sBounds.center[1], sBounds.center[2]) , new Vector3(sBounds.size[0], sBounds.size[1], sBounds.size[2])); } else if (loadPropertyValue.GetType() == typeof(Serializable4x4Matrix)) { Serializable4x4Matrix sMatrix = (Serializable4x4Matrix)loadPropertyValue; Matrix4x4 matrix = new Matrix4x4(); //setting up the matrix values matrix[0, 0] = sMatrix.data[0, 0]; matrix[0, 1] = sMatrix.data[0, 1]; matrix[0, 2] = sMatrix.data[0, 2]; matrix[0, 3] = sMatrix.data[0, 3]; matrix[1, 0] = sMatrix.data[1, 0]; matrix[1, 1] = sMatrix.data[1, 1]; matrix[1, 2] = sMatrix.data[1, 2]; matrix[1, 3] = sMatrix.data[1, 3]; matrix[2, 0] = sMatrix.data[2, 0]; matrix[2, 1] = sMatrix.data[2, 1]; matrix[2, 2] = sMatrix.data[2, 2]; matrix[2, 3] = sMatrix.data[2, 3]; matrix[3, 0] = sMatrix.data[3, 0]; matrix[3, 1] = sMatrix.data[3, 1]; matrix[3, 2] = sMatrix.data[3, 2]; matrix[3, 3] = sMatrix.data[3, 3]; pValue = matrix; } else if (loadPropertyValue.GetType() == typeof(SerializableVector2[])) { SerializableVector2[] data = (SerializableVector2[])loadPropertyValue; Vector2[] vectors = new Vector2[data.Length]; for (int x = 0; x < data.Length; x++) { SerializableVector2 sVector = data[x]; vectors[x] = new Vector2(sVector.x, sVector.y); } pValue = vectors; } else if (loadPropertyValue.GetType() == typeof(SerializableVector3[])) { SerializableVector3[] data = (SerializableVector3[])loadPropertyValue; Vector3[] vectors = new Vector3[data.Length]; for (int x = 0; x < data.Length; x++) { SerializableVector3 sVector = data[x]; vectors[x] = new Vector3(sVector.x, sVector.y); } pValue = vectors; } else if (loadPropertyValue.GetType() == typeof(SerializableAnimatorController)) { SerializableAnimatorController sControlller = (SerializableAnimatorController)loadPropertyValue; foreach (RuntimeAnimatorController controller in allControllers) { if (controller.name.Equals(sControlller.animatorControllerName)) { myObject.GetComponent <Animator>().runtimeAnimatorController = controller; continue; } } } PropertyInfo pInfo = compType.GetProperty(pName); pInfo.SetValue(myObject.GetComponent(compType), pValue); } catch (Exception e) { Debug.LogWarning(e); } } } Vector3 position = new Vector3(); position.x = serializableObject.transformData[0][0]; position.y = serializableObject.transformData[0][1]; position.z = serializableObject.transformData[0][2]; Vector3 rotation = new Vector3(); rotation.x = serializableObject.transformData[1][0]; rotation.y = serializableObject.transformData[1][1]; rotation.z = serializableObject.transformData[1][2]; Vector3 scale = new Vector3(); scale.x = serializableObject.transformData[2][0]; scale.y = serializableObject.transformData[2][1]; scale.z = serializableObject.transformData[2][2]; myObject.transform.position = position; myObject.transform.eulerAngles = rotation; myObject.transform.localScale = scale; foreach (SerializableGameObject child in serializableObject.childs) { DeserializeGameObject(child, myObject.transform); } }
public void SetLevel(GameObject level) { GameObjectSerializer gameObjectSerializer = new GameObjectSerializer(); levelData = gameObjectSerializer.ToSerializableGameObject(level); }