Skip to content

Commit e4cb56e

Browse files
committed
feat: Add callback when on destroy
1 parent 26a2272 commit e4cb56e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Assets/JCSUnity/Scripts/Events/Destroy/JCS_DestroyObjectWithTime.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* $Notice: See LICENSE.txt for modification and distribution information
77
* Copyright (c) 2016 by Shen, Jen-Chieh $
88
*/
9+
using System;
910
using System.Collections.Generic;
1011
using UnityEngine;
1112
using MyBox;
@@ -19,6 +20,9 @@ public class JCS_DestroyObjectWithTime : MonoBehaviour
1920
{
2021
/* Variables */
2122

23+
// Execute when the time is up.
24+
public Action onTimeIsUp = null;
25+
2226
private float mTimer = 0;
2327

2428
private bool mTimesUp = false;
@@ -38,7 +42,7 @@ public class JCS_DestroyObjectWithTime : MonoBehaviour
3842

3943
[Tooltip("While destroying, fade out the game object.")]
4044
[SerializeField]
41-
private bool mDestroyWithAlphaEffect = true;
45+
private bool mWithAlphaEffect = true;
4246

4347
[Tooltip("Fade out objects that fade out after the time is up.")]
4448
[SerializeField]
@@ -64,7 +68,7 @@ private void Update()
6468
{
6569
mTimer += JCS_Time.ItTime(mTimeType);
6670

67-
if (mDestroyWithAlphaEffect)
71+
if (mWithAlphaEffect)
6872
{
6973
if (mDestroyTime - mTimer <= mFadeTime)
7074
FadeOut();
@@ -73,15 +77,17 @@ private void Update()
7377
if (mDestroyTime < mTimer)
7478
{
7579
mTimesUp = true;
80+
81+
onTimeIsUp?.Invoke();
82+
7683
Destroy(gameObject);
7784
}
7885
}
7986

8087
/// <summary>
81-
/// Return the first fade object in the array.
88+
/// Retrieve the first fade object.
8289
/// </summary>
83-
/// <returns></returns>
84-
public JCS_FadeObject GetFadeObject()
90+
public JCS_FadeObject FirstFadeObject()
8591
{
8692
foreach (JCS_FadeObject fo in mFadeObjects)
8793
{

docs/ScriptReference/Events/Destroy/JCS_DestroyObjectWithTime.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ Destroy the game object by time.
44

55
## Variables
66

7-
| Name | Description |
8-
|:------------------------|:-----------------------------------------------------|
9-
| mDestroyTime | Target time to destroy. |
10-
| mDestroyWithAlphaEffect | While destroying, fade out the game object. |
11-
| FadeObjects | Fade out objects that fade out after the time is up. |
12-
| FadeObjects | Fade out objects that fade out after the time is up. |
13-
| FadeTime | How long it fades. |
7+
| Name | Description |
8+
|:-----------------|:-----------------------------------------------------|
9+
| onTimeIsUp | onTimeIsUp |
10+
| mDestroyTime | Target time to destroy. |
11+
| mWithAlphaEffect | While destroying, fade out the game object. |
12+
| FadeObjects | Fade out objects that fade out after the time is up. |
13+
| FadeObjects | Fade out objects that fade out after the time is up. |
14+
| FadeTime | How long it fades. |
15+
16+
## Functions
17+
18+
| Name | Description |
19+
|:----------------|:--------------------------------|
20+
| FirstFadeObject | Retrieve the first fade object. |

0 commit comments

Comments
 (0)