优化整体流程和修复HUD数值更新BUG
This commit is contained in:
@@ -78,16 +78,20 @@ namespace IndianOceanAssets.Engine2_5D
|
||||
// Handles death logic and effects
|
||||
public void Die()
|
||||
{
|
||||
Debug.Log($"[HealthSystem] Die() called on {gameObject.name}, dissolveMaterial={(dissolveMaterial != null ? dissolveMaterial.name : "NULL")}");
|
||||
|
||||
if (deathEffect != null)
|
||||
Instantiate(deathEffect, transform.position + new Vector3(0f, .5f, 0f), Quaternion.identity);
|
||||
|
||||
// 如果有溶解材质,播放消隐动画后再销毁
|
||||
if (dissolveMaterial != null)
|
||||
{
|
||||
Debug.Log($"[HealthSystem] Starting DissolveAndDestroy coroutine on {gameObject.name}");
|
||||
StartCoroutine(DissolveAndDestroy());
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"[HealthSystem] No dissolve material, destroying {gameObject.name} immediately");
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
@@ -97,6 +101,8 @@ namespace IndianOceanAssets.Engine2_5D
|
||||
/// </summary>
|
||||
private IEnumerator DissolveAndDestroy()
|
||||
{
|
||||
Debug.Log($"[HealthSystem] DissolveAndDestroy START on {gameObject.name}");
|
||||
|
||||
// 禁用碰撞体和刚体,防止死亡过程中仍能触发碰撞或受重力下坠
|
||||
foreach (var col in GetComponents<Collider>())
|
||||
col.enabled = false;
|
||||
@@ -114,15 +120,30 @@ namespace IndianOceanAssets.Engine2_5D
|
||||
mb.enabled = false;
|
||||
}
|
||||
|
||||
// 切换所有 SpriteRenderer 到溶解材质
|
||||
// 等一帧,确保禁用生效
|
||||
yield return null;
|
||||
|
||||
// 切换所有 SpriteRenderer 到溶解材质(保留原始纹理和颜色)
|
||||
var renderers = GetComponentsInChildren<SpriteRenderer>();
|
||||
var originalMats = new Material[renderers.Length][];
|
||||
Debug.Log($"[HealthSystem] DissolveAndDestroy: {renderers.Length} SpriteRenderers on {gameObject.name}");
|
||||
|
||||
for (int i = 0; i < renderers.Length; i++)
|
||||
{
|
||||
originalMats[i] = renderers[i].materials;
|
||||
renderers[i].materials = new Material[] { new Material(dissolveMaterial) };
|
||||
// 创建溶解材质实例,并复制原始纹理 + 颜色
|
||||
var dissolveMat = new Material(dissolveMaterial);
|
||||
if (renderers[i].sprite != null)
|
||||
dissolveMat.mainTexture = renderers[i].sprite.texture;
|
||||
dissolveMat.SetColor("_Color", renderers[i].color);
|
||||
dissolveMat.SetFloat("_DissolveAmount", 0f);
|
||||
|
||||
renderers[i].material = dissolveMat;
|
||||
}
|
||||
|
||||
// 再等一帧,确保材质切换生效
|
||||
yield return null;
|
||||
|
||||
Debug.Log($"[HealthSystem] Starting dissolve animation, fadeDuration={fadeDuration}");
|
||||
|
||||
// 动画溶解
|
||||
float elapsed = 0f;
|
||||
while (elapsed < fadeDuration)
|
||||
@@ -139,6 +160,7 @@ namespace IndianOceanAssets.Engine2_5D
|
||||
yield return null;
|
||||
}
|
||||
|
||||
Debug.Log($"[HealthSystem] Dissolve complete, destroying {gameObject.name}");
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user