nomadro/apps/minigame-unity/Assets/Scripts/Editor/KenneySceneComposer.cs
Eric 58a845db32 Add WeChat minigame triple-engine scaffold with Godot Chiang Mai plaza SoT.
Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-06 20:17:14 +08:00

142 lines
7.2 KiB
C#

#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Nomadro.Minigame.Editor
{
/// <summary>
/// Places Kenney CC0 GLB city tiles + character GLBs into the live scene.
/// Requires com.unity.cloud.gltfast (auto-imports .glb as Prefab/GameObject).
/// </summary>
public static class KenneySceneComposer
{
const string Models = "Assets/Art/Kenney/Models";
const string Chars = "Assets/Art/Chars";
public static void Compose(Transform worldRoot, Transform player)
{
if (worldRoot == null) return;
AssetDatabase.Refresh();
// ground tiles
StampGrid(worldRoot, "grass.glb", 5, 5, 10f, 0f, 1.2f);
// roads cross
Place(worldRoot, "road-straight.glb", new Vector3(0, 0, 0), Quaternion.identity, 1.2f);
Place(worldRoot, "road-straight.glb", new Vector3(0, 0, 10), Quaternion.identity, 1.2f);
Place(worldRoot, "road-straight.glb", new Vector3(0, 0, -10), Quaternion.identity, 1.2f);
Place(worldRoot, "road-straight.glb", new Vector3(0, 0, 0), Quaternion.Euler(0, 90, 0), 1.2f);
Place(worldRoot, "road-straight.glb", new Vector3(10, 0, 0), Quaternion.Euler(0, 90, 0), 1.2f);
Place(worldRoot, "road-straight.glb", new Vector3(-10, 0, 0), Quaternion.Euler(0, 90, 0), 1.2f);
Place(worldRoot, "road-intersection.glb", Vector3.zero, Quaternion.identity, 1.2f);
Place(worldRoot, "road-straight-lightposts.glb", new Vector3(0, 0, 5), Quaternion.identity, 1.2f);
// buildings around plaza
Place(worldRoot, "building-small-a.glb", new Vector3(-10, 0, -8), Quaternion.Euler(0, 20, 0), 1.4f);
Place(worldRoot, "building-small-b.glb", new Vector3(9, 0, -6), Quaternion.Euler(0, -15, 0), 1.4f);
Place(worldRoot, "building-small-c.glb", new Vector3(8, 0, 9), Quaternion.Euler(0, 180, 0), 1.5f);
Place(worldRoot, "building-small-d.glb", new Vector3(-9, 0, 8), Quaternion.Euler(0, 90, 0), 1.4f);
Place(worldRoot, "building-garage.glb", new Vector3(11, 0, 2), Quaternion.Euler(0, -90, 0), 1.3f);
Place(worldRoot, "building-small-b.glb", new Vector3(-12, 0, 0), Quaternion.Euler(0, 90, 0), 1.3f);
Place(worldRoot, "building-small-a.glb", new Vector3(0, 0, 14), Quaternion.identity, 1.35f);
Place(worldRoot, "building-small-c.glb", new Vector3(12, 0, -9), Quaternion.Euler(0, -40, 0), 1.25f);
Place(worldRoot, "pavement-fountain.glb", new Vector3(0, 0, 0), Quaternion.identity, 1.1f);
Place(worldRoot, "grass-trees.glb", new Vector3(-6, 0, 4), Quaternion.identity, 1.2f);
Place(worldRoot, "grass-trees-tall.glb", new Vector3(6, 0, 5), Quaternion.identity, 1.2f);
Place(worldRoot, "grass-trees.glb", new Vector3(-5, 0, -5), Quaternion.identity, 1.15f);
Place(worldRoot, "grass-trees-tall.glb", new Vector3(5, 0, -4), Quaternion.identity, 1.15f);
// characters (Soldier as stylized people stand-ins)
PlaceChar(worldRoot, "Soldier.glb", new Vector3(4.5f, 0, -2.5f), 1.1f, player);
PlaceChar(worldRoot, "Soldier.glb", new Vector3(-4f, 0, 3f), 1.05f, player);
PlaceChar(worldRoot, "Soldier.glb", new Vector3(3f, 0, 8f), 1.15f, player);
PlaceChar(worldRoot, "Soldier.glb", new Vector3(-3.5f, 0, 7f), 1.0f, player);
PlaceChar(worldRoot, "Soldier.glb", new Vector3(1f, 0, -6f), 1.08f, player);
PlaceChar(worldRoot, "RobotExpressive.glb", new Vector3(7f, 0, 1f), 0.55f, player);
WorldBootstrap.StripAllColliders(worldRoot);
RemapMaterials(worldRoot);
}
static void StampGrid(Transform root, string file, int nx, int nz, float step, float y, float scale)
{
for (var x = -nx; x <= nx; x++)
for (var z = -nz; z <= nz; z++)
{
if (Mathf.Abs(x) + Mathf.Abs(z) < 2) continue; // leave plaza
Place(root, file, new Vector3(x * step, y, z * step), Quaternion.identity, scale);
}
}
static void Place(Transform root, string file, Vector3 pos, Quaternion rot, float scale)
{
var path = Path.Combine(Models, file).Replace('\\', '/');
var go = LoadGlb(path);
if (go == null) return;
var inst = Object.Instantiate(go, root);
inst.name = Path.GetFileNameWithoutExtension(file) + "_" + pos.x + "_" + pos.z;
inst.transform.SetPositionAndRotation(pos, rot);
inst.transform.localScale = Vector3.one * scale;
}
static void PlaceChar(Transform root, string file, Vector3 pos, float scale, Transform player)
{
var path = Path.Combine(Chars, file).Replace('\\', '/');
var go = LoadGlb(path);
if (go == null) return;
var inst = Object.Instantiate(go, root);
inst.name = "NPC_" + Path.GetFileNameWithoutExtension(file);
inst.transform.SetPositionAndRotation(pos, Quaternion.identity);
inst.transform.localScale = Vector3.one * scale;
var wander = inst.AddComponent<NpcWander>();
wander.lookAt = player;
wander.wander = true;
wander.radius = 1.4f;
var plate = new GameObject("Name");
plate.transform.SetParent(inst.transform, false);
plate.transform.localPosition = new Vector3(0, 2.2f / scale, 0);
var tm = plate.AddComponent<TextMesh>();
tm.text = Path.GetFileNameWithoutExtension(file) == "RobotExpressive" ? "Bot" : "Nomad";
tm.characterSize = 0.08f;
tm.fontSize = 28;
tm.anchor = TextAnchor.MiddleCenter;
tm.color = new Color(0.1f, 0.1f, 0.15f);
plate.AddComponent<BillboardY>();
}
static GameObject LoadGlb(string assetPath)
{
// glTFast imports .glb as GameObject / Prefab
var obj = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
if (obj != null) return obj;
// sometimes main asset is different type — load all
var all = AssetDatabase.LoadAllAssetsAtPath(assetPath);
foreach (var a in all)
{
if (a is GameObject g) return g;
}
Debug.LogWarning("[Nomadro] GLB not imported yet: " + assetPath);
return null;
}
static void RemapMaterials(Transform root)
{
var sh = Shader.Find("Mobile/Diffuse") ?? Shader.Find("Standard") ?? Shader.Find("Unlit/Color");
foreach (var r in root.GetComponentsInChildren<Renderer>(true))
{
if (r == null || r.sharedMaterial == null) continue;
var src = r.sharedMaterial;
Color c = Color.white;
if (src.HasProperty("_Color")) c = src.color;
else if (src.HasProperty("_BaseColor")) c = src.GetColor("_BaseColor");
var m = new Material(sh);
if (m.HasProperty("_Color")) m.color = c;
if (src.mainTexture != null) m.mainTexture = src.mainTexture;
r.sharedMaterial = m;
}
}
}
}
#endif