using System.Collections; using EPOOutline; using UnityEngine; public class HighLightManager : UnitySingleton{ /// /// 是否开启高亮闪烁闪烁 /// private static bool IsFlash = false; private bool IsCloseFlash = false; ////// 添加OutLinable组件 /// public static void AddOutLinable(GameObject gameObject) { if (!gameObject.GetComponent()) { gameObject.AddComponent (); gameObject.GetComponent ().AddAllChildRenderersToRenderingList(); } } /// /// 关闭OutLinable组件 /// public static void CloseOutLinable(GameObject gameObject) { if (gameObject.GetComponent()) { gameObject.GetComponent ().enabled = false; } } /// /// 开启OutLinable组件 /// public static void OpenOutLinable(GameObject gameObject) { if (gameObject.GetComponent()) { gameObject.GetComponent ().enabled = true; } } private int invokeCount = 0; //解决重复点击,协程叠加导致高亮闪烁异常的问题 /// /// 关闭物体高亮闪烁 /// /// /// public void CloseFlashOutLinable(GameObject gameObject, float interval) { invokeCount = 0; //关闭高亮后重置执行次数 Debug.Log("Stop Flash"); IsFlash = false; StartCoroutine(Flash(gameObject, interval)); } ////// 物体高亮闪烁 /// /// 需要高亮闪烁的物体 /// 闪烁的间隔 值越小闪烁频率越快 public void FalshOutLinable(GameObject gameObject, float interval) { IsFlash = true; invokeCount++; //每执行一次,执行次数叠加一次 Debug.Log(invokeCount); if (invokeCount == 1) //执行次数为1时为首次执行,执行协程 { Debug.Log("First Flash"); StartCoroutine(Flash(gameObject, interval)); } if (invokeCount > 1) //执行次数大于一的时候,停止此次及以后的协程 { Debug.Log("Not for the first flash,Stop this flash"); StopCoroutine(Flash(gameObject, interval)); } } ////// 这里用协程去控制闪烁 /// IEnumerator Flash(GameObject gameObject, float interval) { if (IsFlash) { if (gameObject.GetComponent()) { yield return new WaitForSeconds(interval); gameObject.GetComponent ().enabled = true; yield return new WaitForSeconds(interval); gameObject.GetComponent ().enabled = false; } StartCoroutine(Flash(gameObject, interval)); } else { gameObject.GetComponent ().enabled = false; yield return null; } } }
使用方法如下:
void Start()
{
btn.onClick.AddListener(delegate
{
HighLightManager.Instance.FalshOutLinable(model, 0.2f);
});
btn1.onClick.AddListener(delegate
{
HighLightManager.Instance.CloseFlashOutLinable(model, 0.2f);
});
}



