Certainly! Below is an enhanced version of the export/import scripts to include more complex data such as materials, textures, meshes, and a more extensive representation of each GameObject. This will allow you to serialize and deserialize an entire scene, preserving materials, meshes, textures, and other GameObject properties.
Export Script
Here’s an improved version that includes materials, textures, and meshes:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
public class SceneExporter : MonoBehaviour
{
[MenuItem("Tools/Export Scene")]
public static void ExportScene()
{
string folderPath = EditorUtility.SaveFolderPanel("Export Scene", "", "");
if (string.IsNullOrEmpty(folderPath)) return;
var allGameObjects = GameObject.FindObjectsOfType<GameObject>(true);
SceneData sceneData = new SceneData();
foreach (var go in allGameObjects)
{
var data = new GameObjectData(go);
sceneData.gameObjects.Add(data);
}
string json = JsonUtility.ToJson(sceneData, true);
File.WriteAllText(Path.Combine(folderPath, "sceneData.json"), json);
Debug.Log("Scene exported successfully to: " + folderPath);
}
[System.Serializable]
public class SceneData
{
public List<GameObjectData> gameObjects = new List<GameObjectData>();
}
[System.Serializable]
public class GameObjectData
{
public string name;
public string tag;
public Vector3 position;
public Quaternion rotation;
public Vector3 scale;
public List<MaterialData> materials = new List<MaterialData>();
public GameObjectData(GameObject go)
{
name = go.name;
tag = go.tag;
position = go.transform.position;
rotation = go.transform.rotation;
scale = go.transform.localScale;
// Gather materials
Renderer renderer = go.GetComponent<Renderer>();
if (renderer != null)
{
foreach (var material in renderer.sharedMaterials)
{
materials.Add(new MaterialData(material));
}
}
// Optionally serialize mesh data
MeshFilter meshFilter = go.GetComponent<MeshFilter>();
if (meshFilter != null)
{
var mesh = meshFilter.sharedMesh;
// Add other mesh data if needed
}
// Optionally save a reference to any children
foreach (Transform child in go.transform)
{
var childData = new GameObjectData(child.gameObject);
materials.Add(childData);
}
}
}
[System.Serializable]
public class MaterialData
{
public string name;
public string shaderName;
public List<TextureData> textures = new List<TextureData>();
public MaterialData(Material material)
{
name = material.name;
shaderName = material.shader.name;
foreach (var property in material.GetTexturePropertyNames())
{
Texture texture = material.GetTexture(property);
if (texture != null)
{
textures.Add(new TextureData(texture, property));
}
}
}
}
[System.Serializable]
public class TextureData
{
public string propertyName;
public string texturePath;
public TextureData(Texture texture, string propertyName)
{
this.propertyName = propertyName;
texturePath = AssetDatabase.GetAssetPath(texture);
}
}
}
Import Script
This import script will read the JSON file and recreate the objects, including their materials and textures:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
public class SceneImporter : MonoBehaviour
{
[MenuItem("Tools/Import Scene")]
public static void ImportScene()
{
string filePath = EditorUtility.OpenFilePanel("Import Scene", "", "json");
if (string.IsNullOrEmpty(filePath)) return;
string json = File.ReadAllText(filePath);
SceneExporter.SceneData sceneData = JsonUtility.FromJson<SceneExporter.SceneData>(json);
foreach (var goData in sceneData.gameObjects)
{
GameObject go = new GameObject(goData.name);
go.transform.position = goData.position;
go.transform.rotation = goData.rotation;
go.transform.localScale = goData.scale;
go.tag = goData.tag;
// Apply materials
Renderer renderer = go.AddComponent<MeshRenderer>();
if (goData.materials.Count > 0)
{
List<Material> materials = new List<Material>();
foreach (var matData in goData.materials)
{
var material = new Material(Shader.Find(matData.shaderName));
foreach (var texData in matData.textures)
{
Texture texture = AssetDatabase.LoadAssetAtPath<Texture>(texData.texturePath);
if (texture != null)
{
material.SetTexture(texData.propertyName, texture);
}
}
materials.Add(material);
}
renderer.sharedMaterials = materials.ToArray();
}
// Optionally reconstruct meshes from MeshFilters.
// Note: You might want to handle MeshFilter separately if you're saving meshes.
}
Debug.Log("Scene imported successfully.");
}
}
Explanation of Additions:
-
Material Serialization: The script now captures the material properties of each GameObject (name, shader name, and associated textures).
-
Texture Serialization: It includes a TextureData class to store the texture's path and property name.
-
GameObject Hierarchy: If needed, the child GameObjects can be serialized and processed, though this implementation only serializes the materials currently.
Important Considerations:
-
Mesh Data: Currently, the script doesn’t serialize meshes directly. However, if you want to handle mesh data serialization, you could add another class to represent mesh data and include it in the serialization process.
-
Performance: Exporting thousands of GameObjects can take some time. Consider adding asynchronous processing or split in batches if performance becomes an issue.
-
Prefab Instantiation: The import script uses GameObject creation. You'll want to modify it to work with prefabs if necessary.
-
Textures and Materials: When importing materials, it creates new instances of them. You may want to include a way to reference existing materials in your project.
Make sure to test the scripts thoroughly, as scene structures can vary greatly across projects, and you may need to adjust the code to best fit your unique requirements!