栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

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

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


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);
        });
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/899830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号