The tailoring of a particle or custom mesh under Unity NGUI UIPanel

Source: Internet
Author: User
Tags mul

Written at the beginning:

More and more annoying those without brain forwarding themselves do not do the verification of Bo main forum landlord, the Internet finally found some information, the result code is wrong, and some really because the version of the problem is too old incompatible, but some obviously have problems, forwarding before you try to know is definitely not available. Results... Hey... I really don't want to say anything.

This time in the small map to draw a line circle, using the dynamic mapping mesh, small maps need to cut these lines, so went online search for a "Unity NGUI UIPanel under the cut of particles" article. Of course, thank you for the ideas provided by the original author. I have optimized the code involved in this article to make it available. That's right! Make it possible to use!!! Otherwise you can't use AH ah Ah!!! And let me the most crazy point, is some specific resolution to cut the scope of the problem of the bug, I also get rid of!!!

Nonsense not much to say, on the code

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&NBSP;&LT;&NBSP;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");     }}

Attach this file to the UIPanel with the trim function

The shader code used by the particle or mesh under this panel is as follows:

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&NBSP;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             }          }        }} 

If you need to change the shader, use this as a template to modify it

Done


Oh yes, I have optimized here, if the panel size is not changed and the sub-object is not dynamically added, you can not repeat the cutting data, please make code changes according to their own situation, if the above situation, the clip function of the Invoke call comment open can be

This article is from the "Shadow of the Moon, shadow of the Sea" blog, please be sure to keep this source http://abyss.blog.51cto.com/3625755/1982795

The tailoring of a particle or custom mesh under Unity NGUI UIPanel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.