write me a c# script for Unity that will export every single gameobject in scene meshes keeping their all their transform information, materials, textures then make an importer for putting them all back in place texturing / positioning them how they were and everything. Optimize how you write the code it will be for exporting thousands of gameobjects, meshes, materials, textures
Assistant
Creating a C# script to export and import every GameObject's mesh, transform information, materials, and textures in Unity is a complex task. Below, I'll provide a simplified C# script for both exporting and importing game objects.
Export Script
This script will traverse all GameObjects in the scene, gather their mesh, transform, material, and texture information, and save that information into JSON format.
csharp
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
public class SceneExporter
{
[MenuItem("Tools/Export Scene")]
public static void ExportScene()
{
var jsonData = new List<GameObjectData>();
foreach (var gameObj in GameObject.FindObjectsOfType<GameObject>(true))
{
var data = new GameObjectData
{
Name = gameObj.name,
Position = gameObj.transform.position,
Rotation = gameObj.transform.rotation,
Scale = gameObj.transform.localScale,
Materials = new List<MaterialData>()
};
var meshFilter = gameObj.GetComponent<MeshFilter>();
if (meshFilter != null && meshFilter.sharedMesh != null)
{
data.MeshName = meshFilter.sharedMesh.name;
if (gameObj.GetComponent<Renderer>() is Renderer renderer)
{
foreach (var material in renderer.sharedMaterials)
{
if (material != null)
{
var materialData = new MaterialData
{
Name = material.name,
ShaderName = material.shader.name,
TextureNames = new List<string>()
};
foreach (var texture in material.GetTexturePropertyNames())
{
var tex = material.GetTexture(texture);
if (tex != null)
{
materialData.TextureNames.Add(tex.name);
}
}
data.Materials.Add(materialData);
}
}
}
}
jsonData.Add(data);
}
var json = JsonConvert.SerializeObject(jsonData, Formatting.Indented);
File.WriteAllText(Application.dataPath + "/ExportedScene.json", json);
Debug.Log("Scene exported to ExportedScene.json");
}
public class GameObjectData
{
public string Name;
public Vector3 Position;
public Quaternion Rotation;
public Vector3 Scale;
public string MeshName;
public List<MaterialData> Materials;
}
public class MaterialData
{
public string Name;
public string ShaderName;
public List<string> TextureNames;
}
}
Import Script
This script will read the JSON file created by the export script, recreate the GameObjects, and restore their properties such as position, rotation, scale, materials, and textures.
csharp
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
public class SceneImporter
{
[MenuItem("Tools/Import Scene")]
public static void ImportScene()
{
var jsonPath = Application.dataPath + "/ExportedScene.json";
if (!File.Exists(jsonPath))
{
Debug.LogError("ExportedScene.json not found!");
return;
}
var json = File.ReadAllText(jsonPath);
var gameObjectDataList = JsonConvert.DeserializeObject<List<SceneExporter.GameObjectData>>(json);
foreach (var data in gameObjectDataList)
{
var newObj = new GameObject(data.Name);
newObj.transform.position = data.Position;
newObj.transform.rotation = data.Rotation;
newObj.transform.localScale = data.Scale;
// Load mesh from assets
var mesh = Resources.Load<Mesh>(data.MeshName);
if (mesh)
{
var meshFilter = newObj.AddComponent<MeshFilter>();
meshFilter.sharedMesh = mesh;
var renderer = newObj.AddComponent<MeshRenderer>();
List<Material> materials = new List<Material>();
foreach (var materialData in data.Materials)
{
var material = new Material(Shader.Find(materialData.ShaderName));
foreach (var textureName in materialData.TextureNames)
{
var texture = Resources.Load<Texture>(textureName);
if (texture != null)
{
material.SetTexture("_MainTex", texture);
}
}
materials.Add(material);
}
renderer.sharedMaterials = materials.ToArray();
}
}
Debug.Log("Scene imported from ExportedScene.json");
}
}
Notes:
Dependencies: This code uses Newtonsoft.Json for JSON serialization/deserialization. You might need to include the Newtonsoft.Json library, which can be found in the Unity Asset Store or added via a package manager.
Mesh Handling: The import function assumes that the meshes and textures are available in the Resources folder. You should make sure to put all your meshes there so that they can be loaded correctly.
Optimization Considerations: If you plan to export thousands of objects, consider breaking the data into smaller parts, optimizing memory use, and adjusting the file format based on your needs.
Debugging: Be sure to test the implementation in smaller test scenes before deploying in larger scenes to confirm reliability.
This script provides a base structure; modifications may be needed based on project structure and specific requirements.