public void Load(string path) { LivingRoom loadedRoom = SaveSystem.Load(path); foreach (GameObjectInfo goi in loadedRoom.GetObjects()) { GameObject g = FindObject(sceneParent.gameObject, goi.name); Transform obj = null; if (g != null) { obj = g.transform; } if (goi.name == "runtimeobject") { GameObject go = new GameObject("runtimeobject"); go.AddComponent <MeshFilter>().mesh = goi.mesh.GetMesh(); MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>(); meshRenderer.materials = new Material[goi.materials.Length]; for (int j = 0; j < goi.materials.Length; j++) { SerializableMaterial sMat = goi.materials[j]; Debug.Log(sMat.shaderName); Material mat = meshRenderer.materials[j]; mat.shader = Shader.Find(sMat.shaderName); for (int i = 0; i < sMat.propertyTextures.Count; i++) { byte[] bytes; Vector2 tiling, offset; string key = sMat.propertyTextures.ElementAt(i).Key; sMat.propertyTextures.TryGetValue(key, out bytes); sMat.propertyTiling.TryGetValue(key, out tiling); sMat.propertyOffset.TryGetValue(key, out offset); Texture2D texture2D = new Texture2D(2, 2); texture2D.LoadImage(bytes); texture2D.Apply(); mat.SetTexture(key, texture2D); mat.SetTextureScale(key, tiling); mat.SetTextureOffset(key, offset); } foreach (var property in sMat.propertyValues) { mat.SetFloat(property.Key, property.Value); } meshRenderer.materials[j] = mat; } go.transform.SetParent(sceneParent); obj = go.transform; go.AddComponent <MeshCollider>(); go.AddComponent <MGS.ContextMenu.ContextMenuObjectExample>(); go.tag = "Selectable"; go.gameObject.layer = 9; } obj.position = goi.position; obj.rotation = goi.rotation; obj.localScale = goi.scale; obj.gameObject.SetActive(goi.isActive); } }
public void SaveRoom(string path) { List <GameObjectInfo> roomObjects = new List <GameObjectInfo>(); Transform[] sceneTransforms = sceneParent.GetComponentsInChildren <Transform>(true); foreach (Transform t in sceneTransforms) { GameObject sceneObject = t.gameObject; /*goKey = "goKey" + "_" + i; * print("Saving ... goKey = " + goKey + " gameObject.name = " + sceneObject.name); * ES3.Save(goKey, sceneObject, filePath);*/ GameObjectInfo goi = new GameObjectInfo(); goi.isActive = sceneObject.activeSelf; goi.name = sceneObject.name; goi.position = sceneObject.transform.position; goi.rotation = sceneObject.transform.rotation; goi.scale = sceneObject.transform.localScale; if (sceneObject.name == "runtimeobject") { goi.isAddedRuntime = true; goi.mesh = new SerializableMesh(sceneObject.GetComponent <MeshFilter>().mesh); Material[] mats = sceneObject.GetComponent <MeshRenderer>().materials; goi.materials = new SerializableMaterial[mats.Length]; for (int i = 0; i < mats.Length; i++) { Material mat = mats[i]; SerializableMaterial sMat = new SerializableMaterial(); sMat.shaderName = mat.shader.ToString(); sMat.shaderName = sMat.shaderName.Trim("(UnityEngine.Shader)".ToCharArray()); sMat.shaderName = sMat.shaderName.Remove(sMat.shaderName.Length - 1, 1); string[] textureProperties = mat.GetTexturePropertyNames(); sMat.propertyTextures = new Dictionary <string, byte[]>(); sMat.propertyTiling = new Dictionary <string, Vector2>(); sMat.propertyOffset = new Dictionary <string, Vector2>(); sMat.propertyValues = new Dictionary <string, float>(); if (mat.HasProperty("_roughnessFactor")) { sMat.propertyValues.Add("_roughnessFactor", mat.GetFloat("_roughnessFactor")); } if (mat.HasProperty("_metallicFactor")) { sMat.propertyValues.Add("_metallicFactor", mat.GetFloat("_metallicFactor")); } foreach (string property in textureProperties) { Texture tex = mat.GetTexture(property); if (tex != null) { sMat.propertyTextures.Add(property, GetBytesOfTexture(tex)); sMat.propertyTiling.Add(property, mat.GetTextureScale(property)); sMat.propertyOffset.Add(property, mat.GetTextureOffset(property)); } } goi.materials[i] = sMat; } } roomObjects.Add(goi); } SaveSystem.Save(new LivingRoom(roomObjects.ToArray()), path); Debug.Log("successfully saved"); }