using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace Golems
{
///
/// Ghost Object Behaviour sends all attached renderers via the AlbertGhostMemory effect
/// NOTE:
/// This is ExecuteInEditMode so that in the effect is visible in Editor at edit time. This
/// helps Timeline authoring and similar as the alpha changes will be visible.
///
[ExecuteInEditMode]
public class GhostObjectBehaviour : MonoBehaviour
{
private static SRDebuggerGhostAreRealSetting m_SRDebuggerGhostAreRealSettingInstance = null;
///
/// Global Alpha applied to all the renders attached
/// to this behaviour. This is applied POST drawing
/// everything so no clipping through meshes
/// N.B Variable is serialized to allow setting it, but requires public controller to be set via animations
///
[SerializeField]
[Range(0f,1f)]
private float m_Alpha = 1.0f;
public float Alpha => m_Alpha;
///
/// Flag to ignore the Global Alpha driven via the
/// Alert Ghost Memory effect Settings. Will use m_Alpha if true
///
[SerializeField]
private bool m_IgnoreGlobalAlpha = false;
public bool IgnoreGlobalAlpha => m_IgnoreGlobalAlpha;
///
/// Really this should now just be m_Renderers..
/// See docs for GhostObject
///
[SerializeField]
private GhostObject m_GhostObject = new GhostObject();
public GhostObject GhostObject => m_GhostObject;
[SerializeField]
private List m_ReplacementMemoryMaterials;
public List ReplacementMemoryMaterials => m_ReplacementMemoryMaterials;
private void OnEnable()
{
GhostObjectCollection.Instance.AddGhostObjectBehaviour(this);
}
private void Start()
{
if (m_SRDebuggerGhostAreRealSettingInstance == null)
{
m_SRDebuggerGhostAreRealSettingInstance = new SRDebuggerGhostAreRealSetting();
}
if (m_SRDebuggerGhostAreRealSettingInstance != null)
{
m_SRDebuggerGhostAreRealSettingInstance.MakeGhostsGhosty += MakeGhostGhosty;
m_SRDebuggerGhostAreRealSettingInstance.MakeGhostsReal += MakeGhostReal;
if (m_SRDebuggerGhostAreRealSettingInstance.GetGhostsGhosty())
{
MakeGhostGhosty();
}
else
{
MakeGhostReal();
}
}
}
private void OnDestroy()
{
if (m_SRDebuggerGhostAreRealSettingInstance != null)
{
m_SRDebuggerGhostAreRealSettingInstance.MakeGhostsGhosty -= MakeGhostGhosty;
m_SRDebuggerGhostAreRealSettingInstance.MakeGhostsReal -= MakeGhostReal;
}
}
private void OnDisable()
{
GhostObjectCollection.Instance.RemoveGhostObjectBehaviour(this);
}
private void MakeGhostGhosty()
{
if (m_GhostObject != null)
{
foreach (var renderer in m_GhostObject.Renderers)
{
renderer.gameObject.layer = LayerMask.NameToLayer("Default");
}
}
}
private void MakeGhostReal()
{
if (m_GhostObject != null)
{
foreach (var renderer in m_GhostObject.Renderers)
{
if (renderer != null)
{
renderer.gameObject.layer = LayerMask.NameToLayer("Ghost");
}
}
}
}
///
/// Set the local Alpha of the
/// Ghost Object Behaviour to the passed value
///
///
public void SetAlpha(float alpha)
{
if (alpha <= 1 && alpha >= 0 )
{
m_Alpha = alpha;
}
}
#if UNITY_EDITOR
private void GrabAllRenderers()
{
var renderList = new List();
foreach(var rend in GetComponentsInChildren())
{
if (rend as ParticleSystemRenderer)
{
continue;
}
if (rend as TrailRenderer)
{
continue;
}
if (rend as LineRenderer)
{
continue;
}
renderList.Add(rend);
}
if (renderList.Count > 0)
{
m_GhostObject.SetRenderers(renderList.ToArray());
}
}
#endif
class SRDebuggerGhostAreRealSetting
{
public UnityAction MakeGhostsReal;
public UnityAction MakeGhostsGhosty;
private bool m_IsGhosty = false;
public bool GetGhostsGhosty()
{
return m_IsGhosty;
}
public void SetGhostsGhosty(bool shouldBeGhosty)
{
if (shouldBeGhosty != m_IsGhosty)
{
if (shouldBeGhosty)
{
Debug.Log("make em Ghosty");
MakeGhostsGhosty?.Invoke();
}
else
{
Debug.Log("make em Real");
MakeGhostsReal?.Invoke();
}
m_IsGhosty = shouldBeGhosty;
}
}
}
}
///
/// Ghost Object is somewhat deprecated. However it remains as the Renderers in the scene
/// are all hooked up to the instance of GhostObject via GhostObjectBehaviour.
/// N.B We use to only pass around GhostObject but features got more demanding
///
[System.Serializable]
public class GhostObject
{
///
/// Renderers to be drawn through Albert Ghost Memory Pass
///
[SerializeField]
private Renderer[] m_Renderers;
public Renderer[] Renderers => m_Renderers;
public void SetRenderers(Renderer[] renderers)
{
m_Renderers = renderers;
}
}
}