寫在開篇:
越來越煩那些無腦轉(zhuǎn)發(fā)自己不做驗(yàn)證的博主論壇樓主,網(wǎng)上好不容易找到一些資料,結(jié)果代碼搞下來卻是錯(cuò)的,有些確實(shí)是因?yàn)榘姹締栴}太老不兼容,但是有些明顯是有問題的,轉(zhuǎn)發(fā)前自己試試就知道肯定是不能用的。結(jié)果。。。哎。。。真是不想說啥了。
這次是在小地圖中畫線畫圈,用到了動(dòng)態(tài)繪制Mesh,小地圖需要對(duì)這些線進(jìn)行裁切,所以去網(wǎng)上搜了一篇叫做《Unity NGUI UIPanel下對(duì)粒子的剪裁》的文章。當(dāng)然還是感謝一下原作者提供的思路。我這里對(duì)這篇文章中涉及到的代碼進(jìn)行了優(yōu)化改動(dòng),使之可以使用。沒錯(cuò)!使之可以使用!??!不然沒法用啊啊啊?。。?!還有讓我最抓狂的一點(diǎn),就是某些特定分辨率下裁切范圍有問題的bug,我也改掉了?。?!
廢話不多說了,上代碼
CustomUIClipper.cs
using System; using UnityEngine; [RequireComponent(typeof(UIPanel))] public class CustomUIClipper : MonoBehaviour { const string ShaderName = "Bleach/Particles Additive Area Clip"; const float ClipInterval = 0.5f; UIPanel m_targetPanel; Shader m_shader; void Start() { // find panel m_targetPanel = GetComponent<UIPanel>(); if (m_targetPanel == null) throw new ArgumentNullException("Cann't find the right UIPanel"); if (m_targetPanel.clipping != UIDrawCall.Clipping.SoftClip) throw new InvalidOperationException("Don't need to clip"); m_shader = Shader.Find(ShaderName); //if (!IsInvoking("Clip")) // InvokeRepeating("Clip", 0, ClipInterval); Clip(); } Vector4 CalcClipArea() { var clipRegion = m_targetPanel.finalClipRegion; Vector4 nguiArea = new Vector4() { x = clipRegion.x - clipRegion.z / 2, y = clipRegion.y - clipRegion.w / 2, z = clipRegion.x + clipRegion.z / 2, w = clipRegion.y + clipRegion.w / 2 }; var uiRoot = m_targetPanel.root; var pos = m_targetPanel.transform.position; float rate1 = (float)Screen.width / (float)Screen.height; float rate2 = (float)uiRoot.manualWidth / (float)uiRoot.manualHeight; float h = 2f; float w = h * rate1; float tempH = h / uiRoot.manualHeight; float tempW = w / uiRoot.manualWidth; float tempRate = Mathf.Max(tempW, tempH); if (rate1 < rate2) { tempRate = Mathf.Min(tempW, tempH); } Vector4 result = new Vector4() { x = pos.x + nguiArea.x * tempRate, y = pos.y + nguiArea.y * tempRate, z = pos.x + nguiArea.z * tempRate, w = pos.y + nguiArea.w * tempRate }; return result; } void Clip() { Vector4 clipArea = CalcClipArea(); Renderer[] renderers = GetComponentsInChildren<Renderer>(); for (int i = 0; i < renderers.Length; ++i) { var mat = renderers[i].material; if (mat.shader.name != ShaderName) mat.shader = m_shader; mat.SetVector("_Area", clipArea); } } void OnDestroy() { //CancelInvoke("Clip"); } }將這個(gè)文件掛載到帶有裁切功能的UIPanel上
這個(gè)Panel下的粒子或者M(jìn)esh使用的shader代碼如下:
Shader "Bleach/Particles Additive Area Clip" { Properties { _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5) _MainTex ("Particle Texture", 2D) = "white" {} _Area ("Area", Vector) = (0,0,1,1) } Category { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } Blend SrcAlpha One AlphaTest Greater .01 ColorMask RGB Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_particles #include "UnityCG.cginc" sampler2D _MainTex; fixed4 _TintColor; float4 _Area; struct appdata_t { float4 vertex : POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; float2 worldPos : TEXCOORD1; }; float4 _MainTex_ST; v2f vert (appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex); o.color = v.color; o.worldPos = mul(_Object2World, v.vertex).xy; return o; } fixed4 frag (v2f i) : SV_Target { bool inArea = i.worldPos.x >= _Area.x && i.worldPos.x <= _Area.z && i.worldPos.y >= _Area.y && i.worldPos.y <= _Area.w; return inArea? 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord) : fixed4(0,0,0,0); } ENDCG } } } }如果有需要對(duì)shader做改動(dòng)的,就用這個(gè)為范本進(jìn)行修改吧
大功告成
哦對(duì)了,我這里進(jìn)行了優(yōu)化,如果Panel尺寸不變并且子對(duì)象不會(huì)動(dòng)態(tài)添加,可以不用進(jìn)行重復(fù)發(fā)送裁切數(shù)據(jù),請(qǐng)大家根據(jù)自己的情況進(jìn)行代碼修改,如果有以上的情況,將Clip函數(shù)的Invoke調(diào)用注釋打開即可
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站標(biāo)題:UnityNGUIUIPanel下對(duì)粒子或自定義Mesh-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://www.rwnh.cn/article0/hopoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、Google、網(wǎng)站設(shè)計(jì)公司、商城網(wǎng)站、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容