``` docs(readme): 更新文档说明 - 添加项目使用指南 - 完善API接口说明 - 修正错误的配置示例 ``` 注意:由于您提供的code differences部分为空,以上是一个示例模板。当您有具体的代码变更时,请提供详细的变更内容以便生成更准确的commit message。
121 lines
3.2 KiB
C#
121 lines
3.2 KiB
C#
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 控制冲击波扩散效果
|
||
/// 可通过代码动态调整颜色、大小、速度等参数,实现不同技能的复用
|
||
/// </summary>
|
||
public class ShockwaveEffect : MonoBehaviour
|
||
{
|
||
[Header("扩散参数")]
|
||
[Tooltip("扩散持续时间(秒)")]
|
||
public float duration = 1f;
|
||
|
||
[Tooltip("扩散完成后是否自动销毁")]
|
||
public bool autoDestroy = true;
|
||
|
||
[Header("运行时可配置参数(可选)")]
|
||
[Tooltip("覆盖材质颜色(Alpha=0时不覆盖)")]
|
||
public Color overrideColor = new Color(0, 0, 0, 0);
|
||
|
||
[Tooltip("覆盖扩散半径(<=0时使用Transform.localScale)")]
|
||
public float overrideRadius = 0f;
|
||
|
||
[Tooltip("覆盖圆环粗细(<=0时使用材质默认值)")]
|
||
public float overrideRingWidth = 0f;
|
||
|
||
private Material material;
|
||
private float elapsed;
|
||
private bool isPlaying;
|
||
private Vector3 initialScale;
|
||
|
||
private void Start()
|
||
{
|
||
// 获取材质实例(不影响原材质)
|
||
var renderer = GetComponent<MeshRenderer>();
|
||
if (renderer != null)
|
||
{
|
||
material = renderer.material; // 自动创建材质实例
|
||
|
||
// 应用覆盖参数
|
||
if (overrideColor.a > 0f)
|
||
{
|
||
material.SetColor("_RingColor", overrideColor);
|
||
}
|
||
|
||
if (overrideRingWidth > 0f)
|
||
{
|
||
material.SetFloat("_RingWidth", overrideRingWidth);
|
||
}
|
||
|
||
if (overrideRadius > 0f)
|
||
{
|
||
// 根据半径计算所需的Scale
|
||
// Quad默认1x1,半径0.5,所以 Scale = radius * 2
|
||
float scale = overrideRadius * 2f;
|
||
transform.localScale = new Vector3(scale, scale, scale);
|
||
}
|
||
|
||
// 从0开始扩散
|
||
material.SetFloat("_Progress", 0f);
|
||
isPlaying = true;
|
||
elapsed = 0f;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("ShockwaveEffect: 找不到MeshRenderer组件");
|
||
if (autoDestroy)
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (!isPlaying || material == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
elapsed += Time.deltaTime;
|
||
float progress = Mathf.Clamp01(elapsed / duration);
|
||
|
||
// 更新Progress参数(0到1)
|
||
material.SetFloat("_Progress", progress);
|
||
|
||
// 扩散完成
|
||
if (progress >= 1f)
|
||
{
|
||
isPlaying = false;
|
||
|
||
if (autoDestroy)
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
// 清理材质实例
|
||
if (material != null)
|
||
{
|
||
Destroy(material);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 快捷方法:设置扩散参数
|
||
/// </summary>
|
||
public void SetupEffect(float radius, float duration, Color color, float ringWidth = 0f)
|
||
{
|
||
this.overrideRadius = radius;
|
||
this.duration = duration;
|
||
this.overrideColor = color;
|
||
if (ringWidth > 0f)
|
||
{
|
||
this.overrideRingWidth = ringWidth;
|
||
}
|
||
}
|
||
}
|