using System; using System.Collections.Generic; using System.Linq; using Unity.Netcode; using Unity.Netcode.Components; using UnityEditor; using UnityEngine; using UnityEngine.Rendering; namespace GrassCutter.EditorTools { /// Network prefabs: crew, cargo, mowers and every buildable piece (the depot, belts). public static partial class ProjectBuilder { static readonly List NetPrefabs = new List(); static PhysicsMaterial _propPhys, _slick, _beltPhys, _hullPhys; static void BuildPrefabs() { System.IO.Directory.CreateDirectory(Prefabs); NetPrefabs.Clear(); _propPhys = PhysMat("PM_Prop", 0.6f, 0.5f, 0.15f); _slick = PhysMat("PM_Slick", 0.05f, 0.05f, 0f); _beltPhys = PhysMat("PM_Belt", 0.25f, 0.2f, 0f); _hullPhys = PhysMat("PM_Hull", 0.2f, 0.15f, 0.05f); // Crew member Net("Player", Layers.Player, go => { // whoever leaves takes their body with them; everything else stays in the world go.GetComponent().DontDestroyWithOwner = false; var cap = go.AddComponent(); cap.height = 1.3f; cap.radius = 0.38f; cap.center = new Vector3(0, 0.65f, 0); go.AddComponent(); var nt = go.AddComponent(); nt.AuthorityMode = NetworkTransform.AuthorityModes.Owner; nt.UseQuaternionSynchronization = true; // on a ball people lean every way NoScale(nt); var head = new GameObject("Head").transform; head.SetParent(go.transform, false); head.localPosition = new Vector3(0, 1.12f, 0); var avatar = new GameObject("Avatar"); avatar.transform.SetParent(go.transform, false); avatar.AddComponent(); // it puts on whoever the player chose var pa = go.AddComponent(); pa.head = head; go.AddComponent(); go.AddComponent(); go.AddComponent(); }, rigidbody: false); Net("TeamState", 0, go => go.AddComponent(), rigidbody: false, transformSync: false); // Cargo: a grass bundle, hay, a pressed brick CargoPrefab("Cargo_Grass", "SM_Prop_Hay_Bale_Square_01_Green", CargoKind.Grass, 1f, "Grass bundle", 1f, 0.3f); CargoPrefab("Cargo_Hay", "SM_Prop_Hay_Bale_Square_01", CargoKind.Hay, 1f, "Hay", 1f, 0.3f); CargoPrefab("Cargo_Brick", "SM_Prop_Hay_Bale_Square_Plastic_01", CargoKind.Brick, 2f, "Pressed brick", 1f, 0.3f); // Mowers VehiclePrefab("Vehicle_Push", "SM_PushMower", VehicleKind.Push, 90f); VehiclePrefab("Vehicle_Rider", "SM_RiderMower", VehicleKind.Rider, 260f); VehiclePrefab("Vehicle_Pickup", "SM_Veh_Pickup_01", VehicleKind.Pickup, 900f, PackVehicle); VehiclePrefab("Vehicle_Tractor", "SM_Veh_Tractor_01", VehicleKind.Tractor, 700f, PackVehicle); VehiclePrefab("Vehicle_Combine", "SM_Veh_Harvester_01", VehicleKind.Combine, 1400f, PackVehicle); // stage 17: built from the pack's parts in gc_stage17.py, already at their size and facing forward VehiclePrefab("Vehicle_Hauler", "SM_HaulTractor", VehicleKind.Hauler, 1100f); VehiclePrefab("Vehicle_Mulcher", "SM_WideMulcher", VehicleKind.Mulcher, 1000f); VehiclePrefab("Vehicle_Baler", "SM_BalerTractor", VehicleKind.Baler, 900f); BuildPiecePrefabs(); // Network prefab list const string listPath = Prefabs + "NetworkPrefabs.asset"; var list = AssetDatabase.LoadAssetAtPath(listPath); if (!list) { list = ScriptableObject.CreateInstance(); AssetDatabase.CreateAsset(list, listPath); } foreach (var p in list.PrefabList.ToList()) list.Remove(p); foreach (var p in NetPrefabs) if (p.name != "Player") list.Add(new NetworkPrefab { Prefab = p }); EditorUtility.SetDirty(list); AssetDatabase.SaveAssets(); } static void CargoPrefab(string name, string modelName, CargoKind kind, float mass, string display, float hold, float scale) { Net(name, Layers.Prop, go => { var model = Child(go.transform, modelName, Vector3.zero, Quaternion.identity, PrimitiveType.Cylinder, new Vector3(0.46f, 0.34f, 0.46f), new Vector3(0, 0.34f, 0)); model.localScale *= scale; var b = LocalBounds(go.transform, model); // centre the body on its pivot so it tumbles nicely model.localPosition -= new Vector3(0f, b.center.y, 0f); b.center = new Vector3(b.center.x, 0f, b.center.z); var bc = go.AddComponent(); bc.center = b.center; bc.size = b.size * 0.98f; bc.sharedMaterial = kind == CargoKind.Brick ? _slick : _propPhys; var rb = go.GetComponent(); rb.mass = mass; rb.angularDamping = 0.4f; var g = go.AddComponent(); g.displayName = display; g.holdDistance = hold; var c = go.AddComponent(); c.kind = kind; c.halfHeight = b.extents.y; }); } /// The pack's machines are built for people a head taller than our crew. const float PackVehicle = 0.75f; /// A place on a machine that its model doesn't name: the ones from the pack only have wheels. static Transform Spot(Transform root, Transform found, string name, Vector3 at) { if (found) return found; var t = new GameObject(name).transform; t.SetParent(root, false); t.localPosition = at; return t; } static void VehiclePrefab(string name, string modelName, VehicleKind kind, float mass, float scale = 1f) { Net(name, Layers.Vehicle, go => { var model = Child(go.transform, modelName, Vector3.zero, Quaternion.identity, PrimitiveType.Cube, new Vector3(1.2f, 0.6f, 3f), new Vector3(0, 0.3f, 0)); model.localScale *= scale; // the pack's vehicles look down -Z if (modelName.StartsWith("SM_Veh_")) model.localRotation = Quaternion.AngleAxis(180f, Vector3.up) * model.localRotation; if (kind == VehicleKind.Tractor && Model("SM_MowerBar")) { // no loader on this one: a mower bar instead foreach (var t in model.GetComponentsInChildren(true)) if (t.name.EndsWith("_Arm") || t.name.EndsWith("_Bucket")) t.gameObject.SetActive(false); Child(go.transform, "SM_MowerBar", new Vector3(0f, 0.55f, 1.9f), Quaternion.identity, PrimitiveType.Cube, new Vector3(2.8f, 0.3f, 0.8f), Vector3.zero); } foreach (var r in go.GetComponentsInChildren()) r.shadowCastingMode = ShadowCastingMode.On; var b = LocalBounds(go.transform, go.transform); // hull: a box lifted off the wheel contact (the vehicle rides on its spring probes, the hull never drags) // + a cabin box on the bigger machines var hull = go.AddComponent(); bool small = kind == VehicleKind.Rider || kind == VehicleKind.Push; float hullH = small ? 0.7f : 1.4f; const float lift = 0.25f; hull.center = new Vector3(b.center.x, b.min.y + lift + hullH * 0.5f, b.center.z); hull.size = new Vector3(b.size.x * 0.92f, hullH, b.size.z * 0.9f); hull.sharedMaterial = _hullPhys; if (!small) Box(go.transform, "Cabin", new Vector3(b.center.x, b.min.y + hullH + (b.size.y - hullH) * 0.4f, b.center.z), new Vector3(b.size.x * 0.6f, (b.size.y - hullH) * 0.8f, b.size.z * 0.45f), _hullPhys); var rb = go.GetComponent(); rb.mass = mass; rb.linearDamping = 0.2f; rb.angularDamping = 1.2f; rb.centerOfMass = new Vector3(0f, b.min.y + 0.25f, 0f); rb.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative; var v = go.AddComponent(); v.kind = kind; // behind the steering wheel, if the model has none of its own var wheel = model.GetComponentsInChildren(true).FirstOrDefault(t => t.name.EndsWith("SteeringW")); var seatAt = wheel ? go.transform.InverseTransformPoint(wheel.position) + new Vector3(0f, -0.1f, -0.45f) : new Vector3(0f, b.center.y, b.center.z); v.seat = Spot(go.transform, FindIn(model, "Seat"), "Seat", seatAt); bool ownIntake = FindIn(go.transform, "Intake"); v.intake = Spot(go.transform, FindIn(go.transform, "Intake"), "Intake", new Vector3(0f, b.min.y + 0.2f, b.max.z - 0.3f)); v.wake = Spot(go.transform, FindIn(model, "Wake"), "Wake", new Vector3(0f, b.min.y + 0.2f, b.min.z)); v.bars = FindIn(model, "Bars"); v.arm = FindIn(model, "Arm"); v.fill = FindIn(model, "Fill"); v.fanL = FindIn(model, "Fan_L"); v.fanR = FindIn(model, "Fan_R"); v.reel = FindIn(go.transform, "Reel") ?? model.GetComponentsInChildren(true).FirstOrDefault(t => t.name.EndsWith("_Reel")); v.wheels = model.GetComponentsInChildren(true).Where(t => t.name.Contains("Wheel_")).ToArray(); v.chute = FindIn(model, "Chute"); v.hitch = FindIn(model, "Hitch"); v.shell = model; // the strip is cut where the cutter is, as wide as it is: a mower bar, a header's reel, a deck's blades, // a mulcher's wings (which also fold to get through a shed door) var cutter = FindIn(go.transform, "SM_MowerBar") ?? v.reel; var wings = FindIn(model, "Booms"); if (cutter && kind != VehicleKind.Pickup) { var cb = LocalBounds(go.transform, cutter); if (wings) cb.Encapsulate(LocalBounds(go.transform, wings)); if (!ownIntake) v.intake.localPosition = new Vector3(cb.center.x, b.min.y + 0.2f, cb.center.z); v.barWidth = cb.size.x; } if (wings) v.fold = v.reel ? new[] { wings, v.reel } : new[] { wings }; v.length = b.size.z; v.width = b.size.x * 0.5f; }); } // ───────────────────────── build pieces ───────────────────────── const float BeltTop = 0.35f; /// A solid box around the model's footprint (minus a margin) so pieces can be aimed at and walked against. static BoxCollider Solid(Transform root, Transform model, float shrink = 0.08f, float frontCut = 0f) { var b = LocalBounds(root, model); var bc = Box(root, "Solid", new Vector3(b.center.x, b.center.y, b.center.z - frontCut * 0.5f), new Vector3(Mathf.Max(0.1f, b.size.x - shrink * 2f), b.size.y, Mathf.Max(0.1f, b.size.z - shrink * 2f - frontCut)), null); return bc; } static void BuildPiecePrefabs() { foreach (var def in GameDefs.Pieces) { var d = def; Net(d.prefab, Layers.Build, go => { go.AddComponent().piece = d.id; if (d.id == PieceId.Belt) { // stretched to the run when it's laid BeltBody(go.transform, 1f, 1f); Drive(go.transform, null); return; } if (GameDefs.IsDeck(d.id)) { go.AddComponent(); return; } if (GameDefs.IsPanel(d.id)) { go.AddComponent(); return; } var model = Child(go.transform, d.model, Vector3.zero, Quaternion.identity, PrimitiveType.Cube, new Vector3(d.sizeX, 1f, d.sizeZ), new Vector3(0, 0.5f, 0)); model.localScale *= d.scale; foreach (var r in model.GetComponentsInChildren()) r.shadowCastingMode = ShadowCastingMode.On; foreach (var t in model.GetComponentsInChildren(true)) if (t.name == "EndIn" || t.name == "EndOut") t.gameObject.SetActive(false); switch (d.id) { case PieceId.Depot: // only the rim is solid: bundles have to land in the mouth, however tall the building is var solid = Solid(go.transform, model); solid.transform.localPosition = new Vector3(solid.transform.localPosition.x, 0.6f, solid.transform.localPosition.z); solid.size = new Vector3(solid.size.x, 1.2f, solid.size.z); go.AddComponent().mouth = FindIn(model, "Mouth"); break; case PieceId.Intake: { // the model's origin is the middle of its back face, which goes against the deck: the footprint is 1.5 m deep model.localPosition += new Vector3(0f, 0f, -0.75f); var body = Box(go.transform, "Solid", new Vector3(0f, 0.45f, 0f), new Vector3(2.6f, 0.9f, 1.5f), null); var mouthT = new GameObject("Mouth").transform; mouthT.SetParent(go.transform, false); mouthT.localPosition = new Vector3(0f, 1.0f, 0f); var dep = go.AddComponent(); dep.mouth = mouthT; dep.halfX = 1.3f; dep.halfZ = 0.75f; dep.backToDeck = true; break; } case PieceId.Garage: go.AddComponent(); ShedParts(go, model, d.scale); break; case PieceId.UnloadBay: { // the model runs 7 m from the grate's front lip to the hatches: centre it on the footprint model.localPosition += new Vector3(0f, 0f, -0.5f); // the grate is a low step a mower rolls over, the bin behind it is solid Box(go.transform, "Grate", new Vector3(0f, 0.06f, 1.5f), new Vector3(4.2f, 0.12f, 4f), null); Box(go.transform, "Solid", new Vector3(0f, 1.2f, -2f), new Vector3(3.4f, 2.4f, 3f), null); go.AddComponent(); break; } case PieceId.Binder: Solid(go.transform, model); go.AddComponent(); break; case PieceId.Catcher: Solid(go.transform, model); go.AddComponent(); break; case PieceId.DronePad: { // the pad is low: a box over the planks only, so the drones aren't in it Solid(go.transform, model); var drone = Child(go.transform, "SM_Drone", new Vector3(0.75f, 0.68f, 0.75f), Quaternion.identity, PrimitiveType.Cube, new Vector3(0.7f, 0.3f, 0.7f), Vector3.zero); drone.localScale *= 1.4f; foreach (var r in drone.GetComponentsInChildren()) r.shadowCastingMode = ShadowCastingMode.On; go.AddComponent().drone = drone; break; } case PieceId.Reaper: Solid(go.transform, model); var reaper = go.AddComponent(); reaper.muzzle = FindIn(model, "Muzzle"); reaper.reel = FindIn(model, "Reel"); break; case PieceId.RobotI: case PieceId.RobotII: Solid(go.transform, model); var bot = Child(go.transform, "SM_RobotMower", Vector3.zero, Quaternion.identity, PrimitiveType.Cube, new Vector3(0.9f, 0.4f, 1.2f), new Vector3(0, 0.2f, 0)); if (d.id == PieceId.RobotII) bot.localScale *= GameDefs.Robot(d.id).width / GameDefs.Robot(PieceId.RobotI).width; go.AddComponent().bot = bot; break; case PieceId.SkyMower: { // the drone pad, and one drone two and a half times the size of the fetching ones Solid(go.transform, model); var sky = Child(go.transform, "SM_Drone", new Vector3(0f, 0.7f, 0f), Quaternion.identity, PrimitiveType.Cube, new Vector3(1.8f, 0.5f, 1.8f), Vector3.zero); sky.localScale *= 3.5f; foreach (var r in sky.GetComponentsInChildren()) r.shadowCastingMode = ShadowCastingMode.On; go.AddComponent().bot = sky; break; } case PieceId.Dryer: case PieceId.Press: var plinth = Solid(go.transform, model); // the windmill's sails swing far outside its plinth if (d.id == PieceId.Dryer) { plinth.transform.localPosition = new Vector3(0f, 1.6f, 0f); plinth.size = new Vector3(d.sizeX - 0.2f, 3.2f, d.sizeZ - 0.2f); } var machine = go.AddComponent(); machine.lamp = Part(model, "Lamp"); machine.spin = FindIn(model, "Spin"); break; case PieceId.Pole: Box(go.transform, "Solid", new Vector3(0f, 1.8f, 0f), new Vector3(0.5f, 3.6f, 0.5f), null); go.AddComponent(); break; case PieceId.BatteryS: case PieceId.BatteryL: Solid(go.transform, model); var battery = go.AddComponent(); battery.gauge = FindIn(model, "Gauge"); battery.lamp = Part(model, "Lamp"); break; case PieceId.Splitter: { Box(go.transform, "BeltBase", new Vector3(0, BeltTop * 0.5f, 0), new Vector3(1f, BeltTop, 1f), _beltPhys); Drive(go.transform, model).paddle = FindIn(model, "Paddle"); go.AddComponent().sign = Part(model, "Sign"); break; } case PieceId.Merger: { Box(go.transform, "BeltBase", new Vector3(0, BeltTop * 0.5f, 0), new Vector3(1f, BeltTop, 1f), _beltPhys); Drive(go.transform, model); go.AddComponent().sign = Part(model, "Sign"); break; } // structures: what you walk against and on case PieceId.Wall: case PieceId.WindowWall: Box(go.transform, "Solid", new Vector3(0f, 1.5f, 0f), new Vector3(3f, 3f, 0.2f), null); break; case PieceId.DoorWall: Box(go.transform, "SolidL", new Vector3(-1.05f, 1.5f, 0f), new Vector3(0.9f, 3f, 0.2f), null); Box(go.transform, "SolidR", new Vector3(1.05f, 1.5f, 0f), new Vector3(0.9f, 3f, 0.2f), null); Box(go.transform, "Lintel", new Vector3(0f, 2.65f, 0f), new Vector3(1.2f, 0.7f, 0.2f), null); break; case PieceId.Railing: Box(go.transform, "Solid", new Vector3(0f, 0.55f, 0f), new Vector3(3f, 1.1f, 0.12f), null); break; case PieceId.Fence: Box(go.transform, "Solid", new Vector3(0f, 0.45f, 0f), new Vector3(3f, 0.9f, 0.1f), null); break; case PieceId.Roof: { // one slope from the eave at the front (+Z) to the ridge 1.5 m up at the back var slope = Box(go.transform, "Slope", new Vector3(0f, 0.75f, 0f), new Vector3(3.2f, 0.16f, 3.4f), null); slope.transform.localRotation = Quaternion.Euler(Mathf.Atan2(1.5f, 3f) * Mathf.Rad2Deg, 0f, 0f); break; } case PieceId.Stairs: // twelve treads, 0.25 m up and 0.25 m on each, climbing toward +Z from the foot at z = -1.5 for (int i = 0; i < 12; i++) Box(go.transform, "Tread" + i, new Vector3(0f, (i + 1) * 0.125f, -1.5f + (i + 0.5f) * 0.25f), new Vector3(1.2f, (i + 1) * 0.25f, 0.25f), null); Box(go.transform, "Landing", new Vector3(0f, 2.9f, 1.55f), new Vector3(1.2f, 0.2f, 0.1f), null); break; case PieceId.Steps: for (int i = 0; i < 6; i++) Box(go.transform, "Step" + i, new Vector3(0f, (i + 1) * 0.125f, -1.2f + (i + 0.5f) * 0.4f), new Vector3(2f, (i + 1) * 0.25f, 0.4f), null); break; case PieceId.Pillar: Box(go.transform, "Solid", new Vector3(0f, 1.5f, 0f), new Vector3(0.32f, 3f, 0.32f), null); break; case PieceId.Lamppost: Box(go.transform, "Solid", new Vector3(0f, 1.3f, 0f), new Vector3(0.3f, 2.6f, 0.3f), null); go.AddComponent().glow = Part(model, "Glow"); break; case PieceId.Flagpole: Box(go.transform, "Solid", new Vector3(0f, 2f, 0f), new Vector3(0.25f, 4f, 0.25f), null); break; case PieceId.Planter: case PieceId.Bench: case PieceId.Crates: Solid(go.transform, model); break; } }, rigidbody: false, transformSync: false); } } static void BeltBody(Transform root, float w, float len) { Box(root, "BeltBase", new Vector3(0, BeltTop * 0.5f, 0), new Vector3(w, BeltTop, len), _beltPhys); Box(root, "RailL", new Vector3(-w * 0.5f + 0.03f, BeltTop + 0.1f, 0), new Vector3(0.06f, 0.2f, len), null); Box(root, "RailR", new Vector3(w * 0.5f - 0.03f, BeltTop + 0.1f, 0), new Vector3(0.06f, 0.2f, len), null); } static T Part(Transform root, string name) where T : Component { var t = FindIn(root, name); return t ? t.GetComponent() : null; } /// /// The pack garage as a shed machines drive out of: its own door swapped for ours (hinged on its top edge so it can /// tilt up), the outlet hatches on the back wall the bundles come out of, and the spots inside and on the pad. /// The door bay is at +X once the pack model is turned round; out is +Z. /// public static VehicleShed ShedParts(GameObject go, Transform model, float scale, bool outlet = true) { // the pack's garage has its bays on -Z: turned round, they face the pad, where our door and outlet expect them model.localRotation = Quaternion.AngleAxis(180f, Vector3.up) * model.localRotation; var packDoor = FindIn(model, "SM_Bld_Garage_01_Door"); var door = Model("SM_GarageDoor") ? Child(model.parent, "SM_GarageDoor", model.localPosition, Quaternion.identity, PrimitiveType.Cube, Vector3.one, Vector3.zero) : null; if (door) { door.localScale *= scale; if (packDoor) packDoor.gameObject.SetActive(false); } // the outlet was modelled on the back wall of the garage at 0.65: move it back onto this one's wall // the outer ridges of the corrugated wall stand 0.08 m proud of its valleys (more at this scale): the box sits clear // of them, or from inside the shed its hatches show through the wall's grooves float wall = 3.18f * scale + 0.12f; if (outlet && Model("SM_GarageOutlet")) Child(model.parent, "SM_GarageOutlet", model.localPosition + new Vector3(0f, 0f, -(wall - 2.067f)), Quaternion.identity, PrimitiveType.Cube, Vector3.one, Vector3.zero); ShedWalls(go.transform, scale); var shed = go.AddComponent(); shed.door = door ? FindIn(door, "Door") : packDoor; float bay = 4.503f * scale; shed.halfWide = 6.9f * scale; shed.backWall = -wall + 0.3f; shed.doorLine = 3.19f * scale; shed.eaves = 2.75f * scale; shed.inside = new Vector3(bay, 0f, -0.4f); shed.outside = new Vector3(bay, 0f, shed.doorLine + 6f); return shed; } /// /// The pack garage as walls you can walk in between: back, sides, roof, and a front with its three bays open /// (the pack's bays are 3.1 m wide and 2.86 m high at scale 1, with posts between them). /// static void ShedWalls(Transform root, float s) { float w = 6.944f * s, d = 3.19f * s, h = 2.864f * s, top = 3.3f * s, t = 0.25f; Box(root, "WallBack", new Vector3(0f, top * 0.5f, -d + t * 0.5f), new Vector3(2f * w, top, t), null); Box(root, "WallL", new Vector3(-w + t * 0.5f, top * 0.5f, 0f), new Vector3(t, top, 2f * d), null); Box(root, "WallR", new Vector3(w - t * 0.5f, top * 0.5f, 0f), new Vector3(t, top, 2f * d), null); Box(root, "Lintel", new Vector3(0f, (h + top) * 0.5f, d - t * 0.5f), new Vector3(2f * w, top - h, t), null); Box(root, "Roof", new Vector3(0f, top + 0.15f, 0f), new Vector3(2f * w, 0.3f, 2f * d), null); // posts between the bays: from the outer corners to 6.06, and between 2.94 and 1.56 either side foreach (var (a, b) in new[] { (6.064f, 6.944f), (1.56f, 2.942f) }) { float cx = (a + b) * 0.5f * s, wx = (b - a) * s; Box(root, "Post", new Vector3(cx, h * 0.5f, d - t * 0.5f), new Vector3(wx, h, t), null); Box(root, "Post", new Vector3(-cx, h * 0.5f, d - t * 0.5f), new Vector3(wx, h, t), null); } } static Transform FindIn(Transform root, string name) { foreach (var t in root.GetComponentsInChildren(true)) if (t.name == name) return t; return null; } static T Drive(Transform root, Transform model) where T : Conveyor { var go = new GameObject("Drive"); go.transform.SetParent(root, false); var bc = go.AddComponent(); bc.isTrigger = true; bc.center = new Vector3(0, BeltTop + 0.25f, 0); bc.size = new Vector3(0.92f, 0.5f, 1f); var conv = go.AddComponent(); conv.usePerkSpeed = true; var belts = new List(); if (model) foreach (var r in model.GetComponentsInChildren()) if (r.name == "Belt") belts.Add(r); conv.belts = belts.ToArray(); return conv; } static BoxCollider Box(Transform parent, string name, Vector3 center, Vector3 size, PhysicsMaterial mat, float yaw = 0f) { var go = new GameObject(name); go.transform.SetParent(parent, false); go.transform.localPosition = center; go.transform.localRotation = Quaternion.Euler(0, yaw, 0); var bc = go.AddComponent(); bc.size = size; if (mat) bc.sharedMaterial = mat; return bc; } // ───────────────────────── helpers ───────────────────────── static PhysicsMaterial PhysMat(string name, float stat, float dyn, float bounce) { var path = Prefabs + name + ".physicMaterial"; var m = AssetDatabase.LoadAssetAtPath(path); if (!m) { m = new PhysicsMaterial(name); AssetDatabase.CreateAsset(m, path); } m.staticFriction = stat; m.dynamicFriction = dyn; m.bounciness = bounce; m.frictionCombine = PhysicsMaterialCombine.Average; EditorUtility.SetDirty(m); return m; } static void NoScale(NetworkTransform nt) { nt.SyncScaleX = false; nt.SyncScaleY = false; nt.SyncScaleZ = false; } static GameObject Net(string name, int layer, Action build, bool rigidbody = true, bool transformSync = true) { var go = new GameObject(name); go.layer = layer; var no = go.AddComponent(); no.DontDestroyWithOwner = true; if (rigidbody) { var rb = go.AddComponent(); rb.interpolation = RigidbodyInterpolation.Interpolate; rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; var nt = go.AddComponent(); nt.AuthorityMode = NetworkTransform.AuthorityModes.Owner; nt.UseQuaternionSynchronization = true; NoScale(nt); go.AddComponent(); } else if (transformSync && name != "Player") { var nt = go.AddComponent(); NoScale(nt); } build(go); foreach (var t in go.GetComponentsInChildren(true)) t.gameObject.layer = layer; var path = Prefabs + name + ".prefab"; var prefab = PrefabUtility.SaveAsPrefabAsset(go, path); UnityEngine.Object.DestroyImmediate(go); NetPrefabs.Add(prefab); return prefab; } /// Instantiates a model (or a fallback primitive) as a child. Returns the child transform. static Transform Child(Transform parent, string model, Vector3 pos, Quaternion rot, PrimitiveType fallback, Vector3 fbScale, Vector3 fbOffset, Quaternion? fbRot = null) { var m = Model(model); GameObject go; if (m) { go = (GameObject)PrefabUtility.InstantiatePrefab(m); go.transform.SetParent(parent, false); go.transform.localPosition = pos; go.transform.localRotation = rot * m.transform.localRotation; } else { Debug.LogWarning($"[Builder] Missing model {model}, using a primitive"); go = GameObject.CreatePrimitive(fallback); UnityEngine.Object.DestroyImmediate(go.GetComponent()); go.name = model; go.transform.SetParent(parent, false); go.transform.localPosition = pos + fbOffset; go.transform.localRotation = (fbRot ?? Quaternion.identity) * rot; go.transform.localScale = fbScale; } return go.transform; } static Bounds LocalBounds(Transform root, Transform model) { var rs = model.GetComponentsInChildren(); if (rs.Length == 0) return new Bounds(Vector3.up, Vector3.one); var b = new Bounds(root.InverseTransformPoint(rs[0].bounds.center), Vector3.zero); foreach (var r in rs) { var wb = r.bounds; for (int i = 0; i < 8; i++) { var c = wb.center + Vector3.Scale(wb.extents, new Vector3((i & 1) == 0 ? -1 : 1, (i & 2) == 0 ? -1 : 1, (i & 4) == 0 ? -1 : 1)); b.Encapsulate(root.InverseTransformPoint(c)); } } return b; } } }