Skip to content

Commit

Permalink
Turned CustomNavMesh static events into properties using instance bac…
Browse files Browse the repository at this point in the history
…king fields to fix bug where when entering a new scene the static events would reset to null after being subscribed to in the Start method of each hidden component
  • Loading branch information
jadvrodrigues committed Nov 28, 2020
1 parent 091b4eb commit c931dd6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5,901 deletions.
56 changes: 38 additions & 18 deletions Assets/CustomNavMesh/Scripts/CustomNavMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@
[ExecuteAlways, AddComponentMenu("")] // remove from Add Component list
public class CustomNavMesh : MonoBehaviour
{
/// <summary>
/// A delegate which can be used to register callback methods to be invoked after the
/// CustomNavMesh's hidden translation is updated.
/// </summary>
public delegate void OnHiddenTranslationUpdate();
/// <summary>
/// Subscribe functions to be called after the CustomNavMesh's hidden translation is updated.
/// This is reset every time you enter Play mode.
/// </summary>
public static event OnHiddenTranslationUpdate onHiddenTranslationUpdate;

static CustomNavMesh instance;
static CustomNavMesh Instance
{
Expand All @@ -26,10 +15,6 @@ static CustomNavMesh Instance
{
instance = (CustomNavMesh)FindObjectOfType(typeof(CustomNavMesh));

// Reset the static events to avoid them from calling methods from objects not present in the current scene
onHiddenTranslationUpdate = null;
onRenderHiddenUpdate = null;

if (instance == null)
{
if(!SceneManager.GetActiveScene().isLoaded) return null; // null if leaving the scene
Expand All @@ -46,6 +31,29 @@ static CustomNavMesh Instance
}
}

/// <summary>
/// A delegate which can be used to register callback methods to be invoked after the
/// CustomNavMesh's hidden translation is updated.
/// </summary>
public delegate void OnHiddenTranslationUpdate();

event OnHiddenTranslationUpdate m_onHiddenTranslationUpdate;
/// <summary>
/// Subscribe functions to be called after the CustomNavMesh's hidden translation is updated.
/// This is reset every time you enter Play mode.
/// </summary>
public static event OnHiddenTranslationUpdate onHiddenTranslationUpdate
{
add
{
if (Instance != null) Instance.m_onHiddenTranslationUpdate += value;
}
remove
{
if (Instance != null) Instance.m_onHiddenTranslationUpdate -= value;
}
}

[SerializeField, HideInInspector] Vector3 hiddenTranslation = new Vector3(1000.0f, 0.0f, 0.0f);
/// <summary>
/// The distance between the shown surface and the hidden surface where most of
Expand All @@ -62,7 +70,7 @@ public static Vector3 HiddenTranslation
if (Instance.hiddenTranslation != value)
{
Instance.hiddenTranslation = value;
onHiddenTranslationUpdate?.Invoke();
Instance.m_onHiddenTranslationUpdate?.Invoke();
}
}
}
Expand All @@ -72,11 +80,23 @@ public static Vector3 HiddenTranslation
/// CustomNavMesh's render hidden is updated.
/// </summary>
public delegate void OnRenderHiddenUpdate();

event OnRenderHiddenUpdate m_onRenderHiddenUpdate;
/// <summary>
/// Subscribe functions to be called after the CustomNavMesh's render hidden is updated.
/// This is reset every time you enter Play mode.
/// </summary>
public static event OnRenderHiddenUpdate onRenderHiddenUpdate;
public static event OnRenderHiddenUpdate onRenderHiddenUpdate
{
add
{
if (Instance != null) Instance.m_onRenderHiddenUpdate += value;
}
remove
{
if (Instance != null) Instance.m_onRenderHiddenUpdate -= value;
}
}

[SerializeField, HideInInspector] bool renderHidden = true;
/// <summary>
Expand All @@ -93,7 +113,7 @@ public static bool RenderHidden
if (Instance.renderHidden != value)
{
Instance.renderHidden = value;
onRenderHiddenUpdate?.Invoke();
Instance.m_onRenderHiddenUpdate?.Invoke();
}
}
}
Expand Down
Loading

0 comments on commit c931dd6

Please sign in to comment.