您好,登錄后才能下訂單哦!
寫在開篇:
越來越煩那些無腦轉發自己不做驗證的博主論壇樓主,網上好不容易找到一些資料,結果代碼搞下來卻是錯的,有些確實是因為版本問題太老不兼容,但是有些明顯是有問題的,轉發前自己試試就知道肯定是不能用的。結果。。。哎。。。真是不想說啥了。
這次是在小地圖中畫線畫圈,用到了動態繪制Mesh,小地圖需要對這些線進行裁切,所以去網上搜了一篇叫做《Unity NGUI UIPanel下對粒子的剪裁》的文章。當然還是感謝一下原作者提供的思路。我這里對這篇文章中涉及到的代碼進行了優化改動,使之可以使用。沒錯!使之可以使用!!!不然沒法用啊啊啊啊!!!還有讓我最抓狂的一點,就是某些特定分辨率下裁切范圍有問題的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"); } }
將這個文件掛載到帶有裁切功能的UIPanel上
這個Panel下的粒子或者Mesh使用的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 } } } }
如果有需要對shader做改動的,就用這個為范本進行修改吧
大功告成
哦對了,我這里進行了優化,如果Panel尺寸不變并且子對象不會動態添加,可以不用進行重復發送裁切數據,請大家根據自己的情況進行代碼修改,如果有以上的情況,將Clip函數的Invoke調用注釋打開即可
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。