Unity Easy performant outline高亮插件中没有高亮闪烁的功能,如何去简单的添加一个闪烁的功能

/*
 *FileName:      HighLightManager.cs
 *Author:        MinTao
 *Date:          2022/03/10 16:23:33
 *UnityVersion:  2020.3.0f1c1
 *Description:   高亮管理脚本
*/

using System.Collections;
using EPOOutline;
using UnityEngine;

public class HighLightManager : UnitySingleton<HighLightManager>
{
    /// 
    /// 是否开启高亮闪烁闪烁
    /// 
    private static bool IsFlash = false;


    private bool IsCloseFlash = false;

    /// 
    /// 添加OutLinable组件
    /// 
    public static void AddOutLinable(GameObject gameObject)
    {
        if (!gameObject.GetComponent<Outlinable>())
        {
            gameObject.AddComponent<Outlinable>();
            gameObject.GetComponent<Outlinable>().AddAllChildRenderersToRenderingList();
        }
    }

    /// 
    /// 关闭OutLinable组件
    /// 
    public static void CloseOutLinable(GameObject gameObject)
    {
        if (gameObject.GetComponent<Outlinable>())
        {
            gameObject.GetComponent<Outlinable>().enabled = false;
        }
    }

    /// 
    /// 开启OutLinable组件
    /// 
    public static void OpenOutLinable(GameObject gameObject)
    {
        if (gameObject.GetComponent<Outlinable>())
        {
            gameObject.GetComponent<Outlinable>().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<Outlinable>())
            {
                yield return new WaitForSeconds(interval);
                gameObject.GetComponent<Outlinable>().enabled = true;
                yield return new WaitForSeconds(interval);
                gameObject.GetComponent<Outlinable>().enabled = false;
            }

            StartCoroutine(Flash(gameObject, interval));
        }
        else
        {
            gameObject.GetComponent<Outlinable>().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);
        });
    }

你可能感兴趣的:(unity,c#,游戏引擎)