Perekyp/Assets/Editor/SpawnCubesEditor.cs

117 lines
4.0 KiB
C#

using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
namespace Perekyp.EditorTools
{
[InitializeOnLoad]
public static class SpawnCubesEditor
{
static SpawnCubesEditor()
{
EditorApplication.delayCall += SpawnCubesOnce;
}
private static void SpawnCubesOnce()
{
// Execute spawn of the 6 cubes
ExecuteSpawn();
}
[MenuItem("Tools/Spawn 6 Cubes (2 Rows)")]
public static void ExecuteSpawn()
{
// Clean up previous groups if existing
GameObject oldGroup1 = GameObject.Find("ThreeCubes");
if (oldGroup1 != null)
{
Undo.DestroyObjectImmediate(oldGroup1);
}
GameObject oldGroup2 = GameObject.Find("CubesGroup");
if (oldGroup2 != null)
{
Undo.DestroyObjectImmediate(oldGroup2);
}
GameObject parent = new GameObject("CubesGroup");
Undo.RegisterCreatedObjectUndo(parent, "Create CubesGroup Parent");
// Row 1 (first 3 cubes) and Row 2 (3 new cubes next to them)
// Player is around Z = 5.24 facing negative Z, so Z = 3.5 and Z = 2.0 are directly in view.
Vector3[] positions = new Vector3[]
{
// Row 1 (first row)
new Vector3(-2.0f, 1.0f, 3.5f),
new Vector3( 0.0f, 1.0f, 3.5f),
new Vector3( 2.0f, 1.0f, 3.5f),
// Row 2 (second row, right next to / in front of the first row)
new Vector3(-2.0f, 1.0f, 2.0f),
new Vector3( 0.0f, 1.0f, 2.0f),
new Vector3( 2.0f, 1.0f, 2.0f)
};
Color[] colors = new Color[]
{
// Row 1: Red, Green, Blue
new Color(0.95f, 0.26f, 0.21f), // Red
new Color(0.30f, 0.69f, 0.31f), // Green
new Color(0.13f, 0.59f, 0.95f), // Blue
// Row 2: Red, Green, Blue (matching set)
new Color(0.95f, 0.26f, 0.21f), // Red
new Color(0.30f, 0.69f, 0.31f), // Green
new Color(0.13f, 0.59f, 0.95f) // Blue
};
string[] names = new string[]
{
"Cube_Red_Row1",
"Cube_Green_Row1",
"Cube_Blue_Row1",
"Cube_Red_Row2",
"Cube_Green_Row2",
"Cube_Blue_Row2"
};
Shader urpShader = Shader.Find("Universal Render Pipeline/Lit")
?? Shader.Find("URP/Lit")
?? Shader.Find("Standard");
for (int i = 0; i < 6; i++)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = names[i];
cube.transform.position = positions[i];
cube.transform.localScale = Vector3.one;
cube.transform.SetParent(parent.transform);
var renderer = cube.GetComponent<Renderer>();
if (renderer != null && urpShader != null)
{
Material mat = new Material(urpShader);
mat.name = $"Mat_{names[i]}";
mat.color = colors[i];
renderer.sharedMaterial = mat;
}
Undo.RegisterCreatedObjectUndo(cube, $"Create {names[i]}");
}
Selection.activeGameObject = parent;
var activeScene = EditorSceneManager.GetActiveScene();
if (activeScene.IsValid())
{
EditorSceneManager.MarkSceneDirty(activeScene);
EditorSceneManager.SaveScene(activeScene);
Debug.Log($"[SpawnCubesEditor] Successfully spawned 6 cubes (2 rows of 3) on scene '{activeScene.name}'!");
}
else
{
Debug.Log("[SpawnCubesEditor] Successfully spawned 6 cubes in the scene!");
}
}
}
}