Skip to content

Commit

Permalink
fix(Events): improve enable/disable component event with time, no lon…
Browse files Browse the repository at this point in the history
…ger with just behaviours
  • Loading branch information
jcs090218 committed Aug 26, 2022
1 parent dcf3008 commit 0314b03
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace JCSUnity
{
/// <summary>
/// Disable behaviours after a certain time.
/// Disable components after a certain time.
/// </summary>
public class JCS_DisableWithTimeEvent : MonoBehaviour
{
Expand All @@ -22,9 +22,9 @@ public class JCS_DisableWithTimeEvent : MonoBehaviour

[Header("** Runtime Variables (JCS_DisableWithTimeEvent) **")]

[Tooltip("Behaviours that take effect.")]
[Tooltip("Components that take effect.")]
[SerializeField]
private List<Behaviour> mBehaviours = null;
private List<Component> mComponents = null;

[Tooltip("Time before disable.")]
[SerializeField]
Expand All @@ -33,7 +33,7 @@ public class JCS_DisableWithTimeEvent : MonoBehaviour

/* Setter & Getter */

public List<Behaviour> Behaviours { get { return this.mBehaviours; } set { this.mBehaviours = value; } }
public List<Component> Components { get { return this.mComponents; } set { this.mComponents = value; } }
public float time { get { return this.mTime; } set { this.mTime = value; } }
public float timer { get { return this.mTimer; } set { this.mTimer = value; } }

Expand All @@ -49,8 +49,8 @@ private void Update()
mTimer = 0.0f;

// disable all components
foreach (var comp in mBehaviours)
comp.enabled = false;
foreach (var comp in mComponents)
JCS_Util.EnableComponent(comp, false);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Assets/JCSUnity/Scripts/Events/Enable/JCS_EnableWithTimeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace JCSUnity
{
/// <summary>
/// Enable behaviours after a certain time.
/// Enable components after a certain time.
/// </summary>
public class JCS_EnableWithTimeEvent : MonoBehaviour
{
Expand All @@ -22,9 +22,9 @@ public class JCS_EnableWithTimeEvent : MonoBehaviour

[Header("** Runtime Variables (JCS_EnableWithTimeEvent) **")]

[Tooltip("Behaviours that take effect.")]
[Tooltip("Components that take effect.")]
[SerializeField]
private List<Behaviour> mBehaviours = null;
private List<Component> mComponents = null;

[Tooltip("Time before enable.")]
[SerializeField]
Expand All @@ -33,7 +33,7 @@ public class JCS_EnableWithTimeEvent : MonoBehaviour

/* Setter & Getter */

public List<Behaviour> Behaviours { get { return this.mBehaviours; } set { this.mBehaviours = value; } }
public List<Component> Components { get { return this.mComponents; } set { this.mComponents = value; } }
public float time { get { return this.mTime; } set { this.mTime = value; } }
public float timer { get { return this.mTimer; } set { this.mTimer = value; } }

Expand All @@ -49,8 +49,8 @@ private void Update()
mTimer = 0.0f;

// enable all components
foreach (var comp in mBehaviours)
comp.enabled = true;
foreach (var comp in mComponents)
JCS_Util.EnableComponent(comp, true);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions Assets/JCSUnity/Scripts/Util/JCS_Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ namespace JCSUnity
/// </summary>
public static class JCS_Util
{
/// <summary>
/// Do enable/distance component.
/// </summary>
/// <param name="comp"> Component reference you want to act. </param>
/// <param name="act"> Boolean to assign to enabled variable. </param>
public static void EnableComponent(Component comp, bool act)
{
/* Behaviour */
{
var behaviour = comp as Behaviour;
if (behaviour != null)
{
behaviour.enabled = act;
return;
}
}

/* Collider */
{
var collider = comp as Collider;
if (collider != null)
{
collider.enabled = act;
return;
}
}
}

/// <summary>
/// Spawn a gmae object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# JCS_DisableWithTimeEvent

Disable behaviours after a certain time.
Disable components after a certain time.

## Variables

| Name | Description |
|:-----------|:-------------------------------------|
| Behaviours | Behaviours that take effect. |
| Components | Components that take effect. |
| time | Time before inactive the gameobject. |
4 changes: 2 additions & 2 deletions docs/ScriptReference/Events/Enable/JCS_EnableWithTimeEvent.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# JCS_EnableWithTimeEvent

Enable behaviours after a certain time.
Enable components after a certain time.

## Variables

| Name | Description |
|:-----------|:-------------------------------------|
| Behaviours | Behaviours that take effect. |
| Components | Components that take effect. |
| time | Time before inactive the gameobject. |
1 change: 1 addition & 0 deletions docs/ScriptReference/Util/JCS_Util.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All code utility is stored here.

| Name | Description |
|:-------------------------------------|:--------------------------------------------------------------------------------------------------|
| EnableComponent | Do enable/distance component. |
| SpawnGameObject | Spawn a game object. |
| WithInRange | Check if the value is within the range. |
| WithInArrayRange | Check if the index valid within the array length.&nbsp&nbsp(0 ~ (length - 1)) |
Expand Down

0 comments on commit 0314b03

Please sign in to comment.