File size: 2,613 Bytes
40c0886 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 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;
}
}
}
|