Unity中怎么實現(xiàn)新手引導(dǎo)鏤空效果-創(chuàng)新互聯(lián)

Unity中怎么實現(xiàn)新手引導(dǎo)鏤空效果,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

成都創(chuàng)新互聯(lián)長期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新源企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè),新源網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

一、實現(xiàn)思路

創(chuàng)建有8個頂點的Mesh,內(nèi)外邊界都是四邊形(矩形)。只生成內(nèi)、外邊之間的Mesh,內(nèi)層矩形就產(chǎn)生了鏤空部分,外層的4個頂點,是組件自身RectTransform的四個頂點,內(nèi)層的4個頂點,使用鏤空目標(biāo)(_target)RectTransform的四個頂點。確定內(nèi)層的頂點的時候需要注意,多數(shù)情況下_target和HollowOutMask都不在同一個本地坐標(biāo)空間,所以需要使用CalculateRelativeRectTransformBounds計算出HollowOutMask空間下坐標(biāo)這種鏤空的表現(xiàn),可以稍稍提高下性能。因為鏤空的位置不參與渲染,Overdraw會降低

UGUI提供了ICanvasRaycastFilter接口,我們實現(xiàn)IsRaycastLocationValid方法,就可以很方便的控制HollowOutMask是否要攔截下在某一點觸發(fā)的事件

二、這個組件的作用

這個組件做了兩件事情:表現(xiàn)上鏤空一塊區(qū)域和不攔截鏤空范圍上的事件

三、代碼實現(xiàn)

using UnityEngine;using UnityEngine.UI; /// <summary>/// 實現(xiàn)鏤空效果的Mask組件/// </summary>public class HollowOutMask : MaskableGraphic, ICanvasRaycastFilter{ [SerializeField] private RectTransform _target;  private Vector3 _targetMin = Vector3.zero; private Vector3 _targetMax = Vector3.zero;  private bool _canRefresh = true; private Transform _cacheTrans = null;  /// <summary> /// 設(shè)置鏤空的目標(biāo) /// </summary> public void SetTarget(RectTransform target) { _canRefresh = true; _target = target; _RefreshView(); }  private void _SetTarget(Vector3 tarMin, Vector3 tarMax) { if (tarMin == _targetMin && tarMax == _targetMax)  return; _targetMin = tarMin; _targetMax = tarMax; SetAllDirty(); }  private void _RefreshView() { if (!_canRefresh) return; _canRefresh = false;  if (null == _target) {  _SetTarget(Vector3.zero, Vector3.zero);  SetAllDirty(); } else {  Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(_cacheTrans, _target);  _SetTarget(bounds.min, bounds.max); } }  protected override void OnPopulateMesh(VertexHelper vh) { if (_targetMin == Vector3.zero && _targetMax == Vector3.zero) {  base.OnPopulateMesh(vh);  return; }  vh.Clear();  // 填充頂點 UIVertex vert = UIVertex.simpleVert; vert.color = color;  Vector2 selfPiovt = rectTransform.pivot; Rect selfRect = rectTransform.rect; float outerLx = -selfPiovt.x * selfRect.width; float outerBy = -selfPiovt.y * selfRect.height; float outerRx = (1 - selfPiovt.x) * selfRect.width; float outerTy = (1 - selfPiovt.y) * selfRect.height; // 0 - Outer:LT vert.position = new Vector3(outerLx, outerTy); vh.AddVert(vert); // 1 - Outer:RT vert.position = new Vector3(outerRx, outerTy); vh.AddVert(vert); // 2 - Outer:RB vert.position = new Vector3(outerRx, outerBy); vh.AddVert(vert); // 3 - Outer:LB vert.position = new Vector3(outerLx, outerBy); vh.AddVert(vert);  // 4 - Inner:LT vert.position = new Vector3(_targetMin.x, _targetMax.y); vh.AddVert(vert); // 5 - Inner:RT vert.position = new Vector3(_targetMax.x, _targetMax.y); vh.AddVert(vert); // 6 - Inner:RB vert.position = new Vector3(_targetMax.x, _targetMin.y); vh.AddVert(vert); // 7 - Inner:LB vert.position = new Vector3(_targetMin.x, _targetMin.y); vh.AddVert(vert);  // 設(shè)定三角形 vh.AddTriangle(4, 0, 1); vh.AddTriangle(4, 1, 5); vh.AddTriangle(5, 1, 2); vh.AddTriangle(5, 2, 6); vh.AddTriangle(6, 2, 3); vh.AddTriangle(6, 3, 7); vh.AddTriangle(7, 3, 0); vh.AddTriangle(7, 0, 4); }  bool ICanvasRaycastFilter.IsRaycastLocationValid(Vector2 screenPos, Camera eventCamera) { if (null == _target) return true; // 將目標(biāo)對象范圍內(nèi)的事件鏤空(使其穿過) return !RectTransformUtility.RectangleContainsScreenPoint(_target, screenPos, eventCamera); }  protected override void Awake() { base.Awake(); _cacheTrans = GetComponent<RectTransform>(); } #if UNITY_EDITOR void Update() { _canRefresh = true; _RefreshView(); }#endif}

看完上述內(nèi)容,你們掌握Unity中怎么實現(xiàn)新手引導(dǎo)鏤空效果的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前標(biāo)題:Unity中怎么實現(xiàn)新手引導(dǎo)鏤空效果-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://muchs.cn/article12/dcoodc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、關(guān)鍵詞優(yōu)化、小程序開發(fā)、自適應(yīng)網(wǎng)站外貿(mào)建站、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)