一大波优化
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using GameFramework;
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
@@ -20,8 +21,21 @@ namespace IndianOceanAssets.Engine2_5D
|
||||
[SerializeField]
|
||||
private bool isPlayer; // Is this the player?
|
||||
|
||||
[Header("消隐特效")]
|
||||
[Tooltip("死亡消隐使用的溶解材质(Custom/SpriteDissolve shader)")]
|
||||
[SerializeField] private Material dissolveMaterial;
|
||||
|
||||
[Tooltip("消隐动画持续时间(秒)")]
|
||||
[SerializeField] private float fadeDuration = 2f;
|
||||
|
||||
private bool _isDead = false; // 防止重复触发死亡
|
||||
|
||||
/// <summary>是否已死亡(只读)</summary>
|
||||
public bool IsDead => _isDead;
|
||||
|
||||
/// <summary>最大血量(只读)</summary>
|
||||
public int MaxHealth => maxHealth;
|
||||
|
||||
// Initializes health
|
||||
private void Start()
|
||||
{
|
||||
@@ -64,7 +78,67 @@ namespace IndianOceanAssets.Engine2_5D
|
||||
// Handles death logic and effects
|
||||
public void Die()
|
||||
{
|
||||
Instantiate(deathEffect, transform.position + new Vector3(0f, .5f, 0f), Quaternion.identity);
|
||||
if (deathEffect != null)
|
||||
Instantiate(deathEffect, transform.position + new Vector3(0f, .5f, 0f), Quaternion.identity);
|
||||
|
||||
// 如果有溶解材质,播放消隐动画后再销毁
|
||||
if (dissolveMaterial != null)
|
||||
{
|
||||
StartCoroutine(DissolveAndDestroy());
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 溶解消隐协程:切换材质 → 禁用碰撞/AI → 动画溶解 → 销毁。
|
||||
/// </summary>
|
||||
private IEnumerator DissolveAndDestroy()
|
||||
{
|
||||
// 禁用碰撞体和刚体,防止死亡过程中仍能触发碰撞或受重力下坠
|
||||
foreach (var col in GetComponents<Collider>())
|
||||
col.enabled = false;
|
||||
foreach (var col in GetComponents<Collider2D>())
|
||||
col.enabled = false;
|
||||
foreach (var rb in GetComponents<Rigidbody>())
|
||||
rb.isKinematic = true;
|
||||
foreach (var rb in GetComponents<Rigidbody2D>())
|
||||
rb.isKinematic = true;
|
||||
|
||||
// 禁用所有 MonoBehaviour(AI、动画等),防止死亡后仍移动/攻击
|
||||
foreach (var mb in GetComponents<MonoBehaviour>())
|
||||
{
|
||||
if (mb != this)
|
||||
mb.enabled = false;
|
||||
}
|
||||
|
||||
// 切换所有 SpriteRenderer 到溶解材质
|
||||
var renderers = GetComponentsInChildren<SpriteRenderer>();
|
||||
var originalMats = new Material[renderers.Length][];
|
||||
for (int i = 0; i < renderers.Length; i++)
|
||||
{
|
||||
originalMats[i] = renderers[i].materials;
|
||||
renderers[i].materials = new Material[] { new Material(dissolveMaterial) };
|
||||
}
|
||||
|
||||
// 动画溶解
|
||||
float elapsed = 0f;
|
||||
while (elapsed < fadeDuration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = Mathf.Clamp01(elapsed / fadeDuration);
|
||||
|
||||
for (int i = 0; i < renderers.Length; i++)
|
||||
{
|
||||
if (renderers[i] != null && renderers[i].material != null)
|
||||
renderers[i].material.SetFloat("_DissolveAmount", t);
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user