Files
gold_dolphin/unity/Assets/Editor/SpiritLanternPrefabCreator.cs
T
2026-07-05 15:33:41 +08:00

63 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using UnityEditor;
using IndianOceanAssets.Engine2_5D;
/// <summary>
/// 编辑器工具:一键创建灵灯 Prefab。
/// 菜单路径:Assets > 创建灵灯 Prefab
/// </summary>
public static class SpiritLanternPrefabCreator
{
[MenuItem("Assets/创建灵灯 Prefab")]
public static void Create()
{
// 创建根物体
GameObject root = new GameObject("SpiritLantern");
// SpriteRenderer — 灵灯外观(使用默认 Sprite,后续可替换为美术资源)
SpriteRenderer sr = root.AddComponent<SpriteRenderer>();
sr.color = new Color(1f, 0.85f, 0.3f, 0.9f); // 暖黄色灯光
sr.sortingOrder = 100; // 在遮罩之上渲染
// LightSource — 光照组件(复用现有光照系统,与主角遮罩效果一致)
LightSource lightSource = root.AddComponent<LightSource>();
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<SpiritLantern>();
// SphereCollider — 触发器,检测怪物接触(使用3D物理系统匹配敌人的CapsuleCollider
// 【碰撞检测说明】
// 灵灯是主动检测方:灵灯的 Trigger 检测进入的怪物 Collider
// 需确保 Edit > Project Settings > Physics 碰撞矩阵中,
// 灵灯所在 Layer 与怪物 Layer 的交叉项已勾选
SphereCollider collider = root.AddComponent<SphereCollider>();
collider.isTrigger = true;
collider.radius = 5f; // 补偿 Transform scale 0.1,实际碰撞半径 = 5 * 0.1 = 0.5
// Rigidbody — 物理碰撞需要(Trigger 需要至少一方有 Rigidbody
Rigidbody rb = root.AddComponent<Rigidbody>();
rb.isKinematic = true;
rb.useGravity = false;
// 设置 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}");
var asset = AssetDatabase.LoadMainAssetAtPath(prefabPath);
Selection.activeObject = asset;
EditorGUIUtility.PingObject(asset);
}
}