Exporting every object, including its meshes, materials, textures, and positions in a Unity scene at runtime can be quite complex, as Unity does not typically support this through direct API calls in runtime like you would find in an editor script. However, I can provide you with a basic example of how you might approach this problem using serialization to export data to a JSON format.
Please note that this script will not handle every edge case or complex scenario (like dynamic materials, or specific prefab instances) but provides a starting point.
First, make sure you have a folder named Exports in your Unity project's Assets directory, as this script will save the data there.
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
public class SceneExporter : MonoBehaviour
{
[System.Serializable]
public class MeshData
{
public string name;
public Vector3 position;
public Vector3 rotation;
public Vector3 scale;
public List<string> materialNames;
public MeshData(string name, Vector3 position, Vector3 rotation, Vector3 scale, List<string> materialNames)
{
this.name = name;
this.position = position;
this.rotation = rotation;
this.scale = scale;
this.materialNames = materialNames;
}
}
[System.Serializable]
public class SceneData
{
public List<MeshData> meshDataList = new List<MeshData>();
}
private void Start()
{
ExportScene();
}
public void ExportScene()
{
SceneData sceneData = new SceneData();
// Find all MeshFilter components in the scene
MeshFilter[] meshFilters = FindObjectsOfType<MeshFilter>();
foreach (var meshFilter in meshFilters)
{
GameObject go = meshFilter.gameObject;
Vector3 position = go.transform.position;
Vector3 rotation = go.transform.rotation.eulerAngles;
Vector3 scale = go.transform.localScale;
List<string> materialNames = new List<string>();
Renderer renderer = go.GetComponent<Renderer>();
if (renderer != null)
{
foreach (Material material in renderer.sharedMaterials)
{
materialNames.Add(material.name);
}
}
MeshData meshData = new MeshData(go.name, position, rotation, scale, materialNames);
sceneData.meshDataList.Add(meshData);
}
string json = JsonSerializer.Serialize(sceneData, new JsonSerializerOptions { WriteIndented = true });
string filePath = Path.Combine(Application.dataPath, "Exports", "scene_export.json");
File.WriteAllText(filePath, json);
Debug.Log($"Scene exported to: {filePath}");
}
}
How to Use the Script
- Create a new C# script in Unity and name it
SceneExporter.
- Replace the contents of the script with the provided code.
- Attach the
SceneExporter script to any GameObject in your scene, or create an empty GameObject and attach it there.
- When you play the scene, the script will automatically export the scene's mesh information to a JSON file in the
Assets/Exports folder.
Important Notes
- Textures & Materials: This script captures the names of the materials used by each mesh. If you want to export texture information, you'll need to expand the
MeshData class to include texture paths and look up the textures by name.
- Data Complexity: Unity assets like textures, mesh files, and materials are complex and often need to be handled with care. You might need additional serialization logic if you want to export complete asset references.
- Limitations: This is a basic implementation and doesn’t address all types of components or specific needs (like lights, cameras, etc.), and will not handle nested or complex hierarchies effectively.
- Permission: Ensure your application has permission to write files if you plan to deploy this in an application environment.
Final Note
This code sample is a starting point. Depending on your requirements, you might need to adapt and expand it substantially for more complex projects, including a more complete serialization of materials, textures, and other types of objects in the scene.