В настоящее время работаю над проектом Unity, и у меня возникла проблема, когда объект не разрушается.дочерние объекты - это всего лишь 2 изображения и текстовый объект без других сценариев.Иногда объект удаляется правильно, а иногда нет.Кто-нибудь может понять, почему это так?MEC является заменой сопрограмм для единства, но проблема возникает и с сопрограммами Unity, и это была моя первая попытка ее исправить.
Эта проблема, похоже, возникает чаще под нагрузкой.
using MEC;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
[RequireComponent(typeof(CanvasGroup))]
public class UINotificationEffect : MonoBehaviour
{
// Private
private CanvasGroup canvas_group;
private float time_passed;
private bool fading_out;
// Public
public float alive_time = 3.0f;
public float fade_time = 0.5f;
public string message = "Notification message";
private void Awake()
{
canvas_group = GetComponent<CanvasGroup>();
canvas_group.alpha = 0;
}
private void Start()
{
gameObject.GetComponentInChildren<TextMeshProUGUI>().text = message;
Timing.RunCoroutine(FadeTo(1.0f, fade_time), gameObject);
}
private void Update()
{
// At alive time end, fade out and get rid of this gameobject
time_passed += Time.deltaTime;
if (time_passed >= alive_time && !fading_out)
{
Timing.RunCoroutine(FadeTo(0.0f, fade_time), gameObject);
fading_out = true;
}
// Destroy gameobject after alive time + fade time is over.
if (fading_out && canvas_group.alpha <= 0.02f)
{
Timing.KillCoroutines(gameObject);
Destroy(gameObject, 1f);
}
}
/// <summary>
/// Fade alpha of the canvas group in a certain direction (controlled by alpha_value).
/// </summary>
/// <param name="alpha_value">Fade to full (1.0) or fade to nothing (0.0)?</param>
/// <param name="transition_time">The fading time of the alpha.</param>
/// <returns></returns>
IEnumerator<float> FadeTo(float alpha_value, float transition_time)
{
if (gameObject != null && gameObject.activeInHierarchy)
{
float alpha = canvas_group.alpha;
for (float t = 0.0f; t <= 1.0f; t += Time.deltaTime / transition_time)
{
canvas_group.alpha = Mathf.Lerp(alpha, alpha_value, t);
yield return Timing.WaitForOneFrame;
}
}
yield break;
}
/// <summary>
/// Update the text that should be displayed.
/// </summary>
/// <param name="text">The new message.</param>
public void UpdateText(string text)
{
message = text;
gameObject.GetComponentInChildren<TextMeshProUGUI>().text = message;
}
/// <summary>
/// Stop all coroutines when the gameobject is destroyed.
/// </summary>
private void OnDestroy()
{
//StopAllCoroutines();
}
}