using UnityEngine; using UnityEditor; using System.IO; /// /// OGG → Setup → Create Gun Splat Weapon /// Builds: /// • Assets/Prefabs/Weapons/GunSplat.prefab — the held weapon (SimpleGun) /// • Assets/Prefabs/Pickups/GunSplat_Pickup.prefab — world pickup (spins/bobs, press E) /// • Assets/Items/GunSplat_Item.asset — ItemDefinition (type = Weapon) /// public static class GunSplatSetup { private const string GLB_PATH = "Assets/Models/LidarScans/Gun Splat.glb"; private const string WEAPON_PREFAB = "Assets/Prefabs/Weapons/GunSplat.prefab"; private const string PICKUP_PREFAB = "Assets/Prefabs/Pickups/GunSplat_Pickup.prefab"; private const string ITEM_DEF_PATH = "Assets/Items/GunSplat_Item.asset"; [MenuItem("OGG/Setup/Create Gun Splat Weapon")] public static void CreateGunSplat() { // ── 1. Ensure output folders exist ────────────────────────── EnsureFolder("Assets/Prefabs/Weapons"); EnsureFolder("Assets/Prefabs/Pickups"); EnsureFolder("Assets/Items"); // ── 2. Load the GLB model ──────────────────────────────────── GameObject modelAsset = AssetDatabase.LoadAssetAtPath(GLB_PATH); if (modelAsset == null) { Debug.LogError($"[GunSplatSetup] Could not find GLB at: {GLB_PATH}"); EditorUtility.DisplayDialog("Gun Splat Setup", $"Could not find model at:\n{GLB_PATH}", "OK"); return; } // ── 3. Build the held-weapon prefab ────────────────────────── GameObject weaponRoot = new GameObject("GunSplat"); AttachVisual(weaponRoot, modelAsset); SimpleGun gun = weaponRoot.AddComponent(); gun.damage = 30f; gun.range = 120f; gun.fireRate = 6f; gun.maxAmmo = 24; gun.isAutomatic = false; if (File.Exists(DataRelative(WEAPON_PREFAB))) AssetDatabase.DeleteAsset(WEAPON_PREFAB); GameObject savedWeapon = PrefabUtility.SaveAsPrefabAsset(weaponRoot, WEAPON_PREFAB); Object.DestroyImmediate(weaponRoot); if (savedWeapon == null) { Debug.LogError("[GunSplatSetup] Failed to save weapon prefab."); return; } Debug.Log($"[GunSplatSetup] Weapon prefab → {WEAPON_PREFAB}"); // ── 4. Create / update ItemDefinition ──────────────────────── ItemDefinition item = AssetDatabase.LoadAssetAtPath(ITEM_DEF_PATH); bool isNew = item == null; if (isNew) item = ScriptableObject.CreateInstance(); item.itemName = "Gun Splat"; item.type = ItemDefinition.ItemType.Weapon; item.weaponPrefab = savedWeapon; item.description = "A janky lidar-scanned sidearm. Shoots first, looks weird always."; if (isNew) AssetDatabase.CreateAsset(item, ITEM_DEF_PATH); else EditorUtility.SetDirty(item); AssetDatabase.SaveAssets(); Debug.Log($"[GunSplatSetup] ItemDefinition → {ITEM_DEF_PATH}"); // ── 5. Build the world pickup prefab ───────────────────────── GameObject pickupRoot = new GameObject("GunSplat_Pickup"); // Visual child — the GLB model AttachVisual(pickupRoot, modelAsset); // PickupItem component PickupItem pickup = pickupRoot.AddComponent(); pickup.definition = item; pickup.spinSpeed = 90f; pickup.bobHeight = 0.15f; pickup.bobSpeed = 2.0f; pickup.pickupRadius = 2.2f; pickup.pickupKey = KeyCode.E; if (File.Exists(DataRelative(PICKUP_PREFAB))) AssetDatabase.DeleteAsset(PICKUP_PREFAB); GameObject savedPickup = PrefabUtility.SaveAsPrefabAsset(pickupRoot, PICKUP_PREFAB); Object.DestroyImmediate(pickupRoot); if (savedPickup == null) { Debug.LogError("[GunSplatSetup] Failed to save pickup prefab."); return; } Debug.Log($"[GunSplatSetup] Pickup prefab → {PICKUP_PREFAB}"); AssetDatabase.Refresh(); // ── 6. Done ────────────────────────────────────────────────── EditorUtility.DisplayDialog( "Gun Splat Setup ✓", "Created:\n" + $"• {WEAPON_PREFAB}\n" + $"• {PICKUP_PREFAB}\n" + $"• {ITEM_DEF_PATH}\n\n" + "Drag GunSplat_Pickup into the scene wherever you want it to spawn.\n" + "It will spin, bob, and show an [E] prompt when the player gets close.", "Sick"); EditorGUIUtility.PingObject(savedPickup); Selection.activeObject = savedPickup; } // ─── Helpers ───────────────────────────────────────────────────── static void AttachVisual(GameObject parent, GameObject modelAsset) { GameObject visual = (GameObject)PrefabUtility.InstantiatePrefab(modelAsset); if (visual == null) visual = Object.Instantiate(modelAsset); visual.name = "Visual"; visual.transform.SetParent(parent.transform, false); visual.transform.localPosition = Vector3.zero; visual.transform.localRotation = Quaternion.identity; visual.transform.localScale = Vector3.one; } static string DataRelative(string assetPath) => Application.dataPath + "/../" + assetPath; static void EnsureFolder(string path) { string[] parts = path.Split('/'); string current = parts[0]; for (int i = 1; i < parts.Length; i++) { string next = current + "/" + parts[i]; if (!AssetDatabase.IsValidFolder(next)) AssetDatabase.CreateFolder(current, parts[i]); current = next; } } }