using UnityEngine; using UnityEditor; using IndianOceanAssets.Engine2_5D; /// /// 编辑器工具:一键创建灵灯 Prefab。 /// 菜单路径:Assets > 创建灵灯 Prefab /// public static class SpiritLanternPrefabCreator { [MenuItem("Assets/创建灵灯 Prefab")] public static void Create() { // 创建根物体 GameObject root = new GameObject("SpiritLantern"); // SpriteRenderer — 灵灯外观(使用默认 Sprite,后续可替换为美术资源) SpriteRenderer sr = root.AddComponent(); sr.color = new Color(1f, 0.85f, 0.3f, 0.9f); // 暖黄色灯光 sr.sortingOrder = 100; // 在遮罩之上渲染 // LightSource — 光照组件(复用现有光照系统,与主角遮罩效果一致) LightSource lightSource = root.AddComponent(); SerializedObject so = new SerializedObject(lightSource); so.FindProperty("radius").floatValue = 4f; so.FindProperty("intensity").floatValue = 1f; so.FindProperty("registerOnStart").boolValue = true; so.ApplyModifiedProperties(); // SpiritLantern — 灵灯行为脚本 root.AddComponent(); // CircleCollider2D — 触发器,检测怪物接触 // 【碰撞检测说明】 // 灵灯是主动检测方:灵灯的 Trigger 检测进入的怪物 Collider2D // 需确保 Edit > Project Settings > Physics 2D 碰撞矩阵中, // 灵灯所在 Layer 与怪物 Layer 的交叉项已勾选 CircleCollider2D collider = root.AddComponent(); collider.isTrigger = true; collider.radius = 0.5f; // Rigidbody2D — 物理碰撞需要(Trigger 需要至少一方有 Rigidbody) Rigidbody2D rb = root.AddComponent(); rb.isKinematic = true; rb.bodyType = RigidbodyType2D.Kinematic; // 设置 Layer 为 Default root.layer = LayerMask.NameToLayer("Default"); // 保存为 Prefab string prefabPath = "Assets/eco/Prefabs/SpiritLantern.prefab"; PrefabUtility.SaveAsPrefabAsset(root, prefabPath); // 清理场景中的临时物体 Object.DestroyImmediate(root); Debug.Log($"[SpiritLantern] Prefab 已创建: {prefabPath}"); EditorUtility.PingAsset(AssetDatabase.LoadMainAssetAtPath(prefabPath)); } }