除了常見的HighLightSystem來實現的高亮功能,其實還有很多的方法實現物體的高亮。
在 Unity資源商店 搜索OutLine,就會有很多免費好用的高亮插件。
下面介紹一下 QuickOutline這個插件,在 Unity資源商店 搜索到后,點擊進去就可以看到 QuickOutline 的相關信息。
點擊 在Unity中打開,將插件導入到工程里邊。查看Demo你會發現有五種高亮模式。
依次分別是:
-
Silhouette Only:模型被遮擋的部分整體高亮;
-
Outline Hidden:模型被遮擋的部分輪廓高亮;
-
Outline All:整個模型的輪廓高亮;
-
Outline And Silhouette:整個模型輪廓高亮+被遮擋的部分整體高亮;
-
Outline Visible:模型未被遮擋的部分輪廓高亮;
用法很簡單,只需要給你想要高亮的模型掛上Outline這個腳本就可以
- OutLine Mode:選擇高亮類型:
(1)Silhouette Only:模型被遮擋的部分整體高亮;
(2)Outline Hidden:模型被遮擋的部分輪廓高亮;
(3)Outline All:整個模型的輪廓高亮;
(4)Outline And Silhouette:整個模型輪廓高亮+被遮擋的部分整體高亮;
(5)Outline Visible:模型未被遮擋的部分輪廓高亮; - OutLine Color:選擇高亮的顏色;
- OutLint Width :高亮輪廓的寬度;(調節這個值可以實現閃爍高亮的效果)
- Precompute OutLine:啟用預計算:按頂點計算在編輯器中執行,并與對象序列化。 + "禁用預計算:在Awake()運行時執行逐頂點計算。 這可能會導致大網格的暫停。
小球被大球擋住時的高亮效果:
可以根據個人需求修改Outline腳本
修改代碼部分:
case Mode.OutlineAndSilhouette:/*outlineMaskMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.LessEqual);outlineFillMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.Always);outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);*/outlineMaskMaterial.SetFloat("_ZTest", outlineWidth);outlineFillMaterial.SetFloat("_ZTest", 0);outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);break;
這里把整體高亮:
通過鼠標控制物體的高亮:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MouseController : MonoBehaviour
{//鼠標選中的物體private GameObject selectedObj;void Update(){if (Input.GetMouseButtonDown(0)){if (selectedObj == null){RaycastHit hit = this.CastRay();if (!hit.collider.gameObject){return;}else{if (hit.collider.gameObject.GetComponent<Outline>()){selectedObj = hit.collider.gameObject;selectedObj.GetComponent<Outline>().enabled = true;}return;}}else{selectedObj.GetComponent<Outline>().enabled = false;selectedObj = null;}}}//創建射線檢測private RaycastHit CastRay(){Vector3 screenFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane);Vector3 screenNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);Vector3 far = Camera.main.ScreenToWorldPoint(screenFar);Vector3 near = Camera.main.ScreenToWorldPoint(screenNear);RaycastHit hit;Physics.Raycast(near, far - near, out hit);return hit;}}