open-world-city-unity / Assets /Scripts /BuildingGenerator.cs
ryzerrr's picture
Upload folder using huggingface_hub
6cae93f verified
Raw
History Blame Contribute Delete
2.61 kB
using UnityEngine;
namespace UnityAgent
{
/// <summary>
/// Procedural building generator: builds a box building mesh with
/// emissive windows and adds a BoxCollider for collision.
/// </summary>
public static class BuildingGenerator
{
public static GameObject Generate(string name, Vector3 position, Vector3 size,
Material wallMaterial, Material windowMaterial)
{
GameObject go = new GameObject(name);
go.transform.position = position;
// Walls
GameObject walls = GameObject.CreatePrimitive(PrimitiveType.Cube);
walls.name = "Walls";
walls.transform.SetParent(go.transform, false);
walls.transform.localScale = size;
walls.transform.localPosition = Vector3.up * size.y * 0.5f;
if (wallMaterial != null) walls.GetComponent<MeshRenderer>().sharedMaterial = wallMaterial;
// Windows: a smaller cube with the window material, slightly offset on each side.
if (windowMaterial != null)
{
GameObject windows = GameObject.CreatePrimitive(PrimitiveType.Cube);
windows.name = "Windows";
windows.transform.SetParent(go.transform, false);
windows.transform.localScale = size * 0.99f;
windows.transform.localPosition = Vector3.up * size.y * 0.5f;
var mr = windows.GetComponent<MeshRenderer>();
mr.sharedMaterial = windowMaterial;
}
// Collider (the cube already has one, but ensure it's a BoxCollider).
var box = go.GetComponent<BoxCollider>();
if (box == null) box = go.AddComponent<BoxCollider>();
box.size = size;
box.center = Vector3.up * size.y * 0.5f;
return go;
}
/// <summary>
/// Build an emissive window material procedurally so the agent does
/// not depend on any asset on disk.
/// </summary>
public static Material MakeWindowMaterial(Color glow)
{
var m = new Material(Shader.Find("Standard"));
m.name = "WindowMat";
m.color = Color.black;
m.SetColor("_EmissionColor", glow);
m.EnableKeyword("_EMISSION");
return m;
}
public static Material MakeWallMaterial(Color col)
{
var m = new Material(Shader.Find("Standard"));
m.name = "WallMat";
m.color = col;
return m;
}
}
}