Unity实现通用的物体高亮闪烁的方法(轮廓或整体高亮闪烁)

 

一、导入HighlightingSystem高亮插件到项目中

 

二、编写一个控制物体轮廓高亮闪烁的脚本,如下图所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Global
{
	public class Global_CtrlHightLighting : MonoBehaviour
	{
        public HighlightableObject m_ho;                                        //持有当前外发光需要的组件
        private float shineTime = 1F;                                           //持续发光时间



        /// <summary>
        /// 高亮闪烁设置
        /// </summary>
        public void HighLightSettings()
        {
            //循环往复外发光开启(参数为:颜色1,颜色2,切换时间)
            m_ho.FlashingOn(Color.cyan, Color.grey, shineTime);

            //关闭循环往复外发光
            m_ho.FlashingOff();


            //持续外发光开启(参数:颜色)
            m_ho.ConstantOn(Color.blue);

            //关闭持续外发光
            m_ho.ConstantOff();
        }

        /// <summary>
        /// 启用高亮
        /// </summary>
        public void EnableHighLight()
        {
            //开启外发光
            m_ho.FlashingOn(Color.cyan, Color.grey, shineTime);
        }

        /// <summary>
        /// 禁用高亮
        /// </summary>
        public void DisableHighLight()
        {
            //关闭外发光
            m_ho.FlashingOff();
        }

    }//class_end
}

三、编写通用的实现物体轮廓或者整体高亮闪烁的脚本,如下所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Global;
using kernal;
using Control;

namespace View
{
	public class View_SingleEquipmentPartControl : MonoBehaviour
	{
        #region   添加的控制组件
        private HighlightableObject _HighlightableObject;                       //物体发光必要组件
        private Global_CtrlHightLighting _CtrlHightLighting;                    //物体发光控制组件
        private GameObjectDisplayUI _GameObjectDisplayUI;                       //面板显示在部件对应位置的控制组件

        #endregion


      
        #region   鼠标移入移出参数
        public bool isMouseEnter = false;                                       //鼠标是否进入
        public bool isMouseArea = false;                                        //鼠标是否在区域内
        private string _MouseEnterEquipmentPartName;                            //鼠标移入的设备部件名称

        #endregion

        #region   使用Shader实现高亮效果参数
        public GameObject[] equipmentParts = new GameObject[10];                //需要改变颜色的设备部件
        private Dictionary<string, Material> _EquipmentPartsOriginalMatDic;     //设备拆分部件原本材质球字典
        private Material _HighlightMat;                                         //高亮效果材质球
        private Material[] _HighlightMats;                                      //高亮效果材质球集合



        #endregion

        #region   设备部件详细信息参数
        public Transform equipmentPartControl;
        private View_EquipmentPartInfo _EquipmentPartInfo;

        #endregion

        void Start()
		{
                       
            //给该部件添加必要的控制组件
            AddControlComponet();

            //实例化
            _EquipmentPartsOriginalMatDic = new Dictionary<string, Material>();

         }
         

        #region   鼠标事件方法

        /// <summary>
        /// 鼠标进入设备部件高亮
        /// </summary>
        private void OnMouseEnter()
        {

            isMouseEnter = true;

            if (_HighlightableObject != null && _CtrlHightLighting != null)
            {
                //开启高亮
                IsEquipmentPartHighlight(0, true);

             
                _MouseEnterEquipmentPartName = this.gameObject.name;
                //print("_MouseEnterEquipmentPartName=" + _MouseEnterEquipmentPartName);
            }

        }

        //鼠标在设备部件区域内
        private void OnMouseOver()
        {
            print("鼠标在区域内");
            isMouseArea = true;

        }


        /// <summary>
        /// 鼠标退出设备部件
        /// </summary>
        private void OnMouseExit()
        {

            isMouseEnter = false;
            isMouseArea = false;

            if (_HighlightableObject != null && _CtrlHightLighting != null)
            {

                //不开启高亮
                IsEquipmentPartHighlight(0);

                //清空鼠标移入的部件名称
                _MouseEnterEquipmentPartName = "";
            }
        }

        #endregion


        #region   私有方法

        /// <summary>
        /// 给该部件添加必要的控制组件
        /// </summary>
        private void AddControlComponet()
        {
            _HighlightableObject = new HighlightableObject();
            if (this.gameObject.GetComponent<HighlightableObject>() == null)
            {
                _HighlightableObject = this.gameObject.AddComponent<HighlightableObject>();
            }
            else
            {
                _HighlightableObject = this.gameObject.GetComponent<HighlightableObject>();
            }
            
            
            _CtrlHightLighting = new Global_CtrlHightLighting();
            if (this.gameObject.GetComponent<Global_CtrlHightLighting>()==null)
            {
                _CtrlHightLighting = this.gameObject.AddComponent<Global_CtrlHightLighting>();
                _CtrlHightLighting.m_ho = _HighlightableObject;
            }
            else
            {
                _CtrlHightLighting = this.gameObject.GetComponent<Global_CtrlHightLighting>();
                _CtrlHightLighting.m_ho = _HighlightableObject;
            }

        }


        /// <summary>
        /// 设备部件实现高亮方法
        /// </summary>
        /// <param name="number">高亮实现的方法类型</param>
        /// <param name="isEnableHighlight">是否开启高亮(默认不开启)</param>
        private void IsEquipmentPartHighlight(int number,bool isEnableHighlight=false)
        {
            switch (number)
            {
                case 0:
                   
                    if (isEnableHighlight)
                    {
                        //部件高亮设置(shader)
                        EnbaleEquipmentHighLightOfShader(equipmentParts);
                    }
                    else
                    {
                        //禁用高亮(shader)
                        DisbaleEquipmentHighLightOfShader(equipmentParts);
                    }
                    
                    break;
                case 1:
                    if (isEnableHighlight)
                    {
                        //部件高亮设置(Highlight System 插件方式)
                        EnableEquipmentHighligtOfPlugins();
                    }
                    else
                    {
                        //禁用设备高亮插件Highlight System 插件方式)
                        DisableEquipmentHighlightOfPlugins();
                    }
                   
                    break;

                default:

                    break;
            }
        }


        #region   设备部件实现高亮(Shader方式)
        /// <summary>
        /// 启用设备部件高亮(shader实现)
        /// </summary>
        /// <param name="go">需要改变颜色的设备部件</param>
        private void EnbaleEquipmentHighLightOfShader(GameObject[] go)
        {
            _HighlightMat = Resources.Load("Prefabs/HighlightMat/HightBlue") as Material;
            
            int number = go.Length;
            _HighlightMats = new Material[number];

            //print("需要改变颜色的部件数量="+number);
            if (number>=1)
            {
                for (int i=0;i<number;i++)
                {
                    _HighlightMats[i] = _HighlightMat;
                    //print("部件="+go[i]);
                    if (go[i].GetComponent<MeshRenderer>() != null)
                    {
                        if (!_EquipmentPartsOriginalMatDic.ContainsKey(go[i].name))
                        {
                            Material mat = go[i].GetComponent<Renderer>().material;
                            //print("材质球=" + mat);
                            _EquipmentPartsOriginalMatDic.Add(go[i].name, mat);
                          
                        }
                        go[i].GetComponent<MeshRenderer>().material = _HighlightMats[i];

                    }
                }
               
            }

        }
        /// <summary>
        /// 禁用设备部件高亮(shader实现)
        /// </summary>
        /// <param name="go">需要改变颜色的设备部件</param>
        private void DisbaleEquipmentHighLightOfShader(GameObject[] go)
        {
            int number = go.Length;
            if (number>=1)
            {
                for (int i=0;i<number;i++)
                {
                    if (_EquipmentPartsOriginalMatDic.ContainsKey(go[i].name))
                    {
                        Material mat;
                        _EquipmentPartsOriginalMatDic.TryGetValue(go[i].name, out mat);
                        go[i].GetComponent<Renderer>().material = mat;

                    }
                }
            }
        }

        #endregion

        #region  设备部件实现高亮(Highlight System 插件方式)

        /// <summary>
        /// 启用高亮插件
        /// </summary>
        private void EnableEquipmentHighligtOfPlugins()
        {
            //高亮设置
            _CtrlHightLighting.HighLightSettings();
            //启用高亮
            _CtrlHightLighting.EnableHighLight();
        }

        /// <summary>
        /// 禁用设备高亮插件
        /// </summary>
        private void DisableEquipmentHighlightOfPlugins()
        {
            //高亮设置
            _CtrlHightLighting.HighLightSettings();
            //禁用高亮
            _CtrlHightLighting.DisableHighLight();
        }

        #endregion


     

       

    }//class_end
}

四、将 View_SingleEquipmentPartControl脚本添加给需要实现轮廓或者整体高亮闪烁的物体(这里默认使用整体高亮闪烁),给该物体添加碰撞体、指定对应的需要实现整体高亮闪烁的物体,如下图所示:

五、运行测试、鼠标移入到该物体,该物体高亮、鼠标移出该物体则该物体恢复为正常,如下图所示:

六、实现物体整体高亮的Shader链接如下:

https://download.csdn.net/download/xiaochenxihua/10662546

注意: private Material _HighlightMat;                                         //高亮效果材质球 的加载路径根据自身的项目(实现物体整体高亮的Shader放置的位置)改变。

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页