About unity implementing custom polygon Picture effects
1. Create rawimageeditor Editor extension script (placed in Project Editor folder, not created)
1 /*************************************************2 * Project name: Dynamic change Picture display edge number3 * Script Creator: Magic card4 * Script creation time: 2018.01.275 * Scripting feature: Rawimageeditor editor feature rewrite6 * ***********************************************/7 usingSystem.Collections;8 usingSystem.Collections.Generic;9 usingUnityengine;Ten usingUnityeditor; One usingUnityeditor.ui; A -[Customeditor (typeof(uicircle),true)] - [Caneditmultipleobjects] the Public classUicircleinspector:rawimageeditor - { - - Public Override voidOninspectorgui () + { - Base. Oninspectorgui (); +Uicircle Circle = target asuicircle; Acircle.segments = Mathf.clamp (Editorguilayout.intfield ("Uicircle Polygon", circle.segments),3, the)///Set the minimum of the number of sides to the maximum value (3-360) at } - -}
2. Create a rawimage redraw script
1 /*************************************************2 * Project name: Dynamic change Picture display edge number3 * Script Creator: Magic card4 * Script creation time: 2018.01.275 * Scripting feature: Rawimage re-drawing scripts6 * ***********************************************/7 usingSystem;8 usingUnityengine;9 usingUnityengine.ui;Ten One Public classUicircle:rawimage A { - Const intFill_percent = -; - floatThickness =5; the - [Serializefield] -[Range (3, the)] - int_segments = $; + - Public intSegments + { A Get{return_segments;} at Set - { - if(_segments! =value) - { -_segments =value; - Setverticesdirty (); in #ifUnity_editor - UnityEditor.EditorUtility.SetDirty (transform); to #endif + } - } the } * $ Panax Notoginseng protected Override voidOnrecttransformdimensionschange () - { the Base. Onrecttransformdimensionschange (); + This. Thickness = (float) Mathf.clamp ( This. Thickness,0, RectTransform.rect.width/2); A } the + protected Override voidOnpopulatemesh (vertexhelper VH) - { $ floatouter =-recttransform.pivot.x *RectTransform.rect.width; $ floatInner =-recttransform.pivot.x * RectTransform.rect.width + This. Thickness; - - VH. Clear (); the -Vector2 PrevX =Vector2.zero;WuyiVector2 Prevy =Vector2.zero; theVector2 uv0 =NewVector2 (0,0); -Vector2 Uv1 =NewVector2 (0,1); WuVector2 Uv2 =NewVector2 (1,1); -Vector2 Uv3 =NewVector2 (1,0); About Vector2 Pos0; $ Vector2 pos1; - Vector2 Pos2; - Vector2 Pos3; - A floatTW =RectTransform.rect.width; + floatth =RectTransform.rect.height; the - floatAnglebystep = (FILL_PERCENT/100F * (MATHF.PI * 2f))/segments; $ floatCurrentangle =0f; the for(inti =0; I < segments +1; i++) the { the the floatc =Mathf.cos (currentangle); - floats =Mathf.sin (currentangle); in theStepthroughpointsandfill (outer, inner,refPrevX,refPrevy, outPos0, outPOS1, outPos2, outPos3, C, s); the AboutUv0 =NewVector2 (POS0.X/TW +0.5f, pos0.y/th +0.5f); theUv1 =NewVector2 (POS1.X/TW +0.5f, pos1.y/th +0.5f); theUv2 =NewVector2 (POS2.X/TW +0.5f, pos2.y/th +0.5f); theUv3 =NewVector2 (POS3.X/TW +0.5f, pos3.y/th +0.5f); + -Vh. Adduivertexquad (Setvbo (New[] {pos0, pos1, Pos2, POS3},New[] {uv0, uv1, Uv2, Uv3}); the BayiCurrentangle + =Anglebystep; the } the } - - Private voidStepthroughpointsandfill (floatOuterfloatInnerrefVector2 PrevX,refVector2 Prevy, outVector2 Pos0, outVector2 POS1, outVector2 Pos2, outVector2 Pos3,floatCfloats) the { thePos0 =PrevX; thePOS1 =NewVector2 (outer * c, outer *s); the -Pos2 =Vector2.zero; thePOS3 =Vector2.zero; the thePrevX =pos1;94Prevy =Pos2; the } the the protecteduivertex[] Setvbo (vector2[] vertices, vector2[] uvs)98 { Aboutuivertex[] Vbo =Newuivertex[4]; - for(inti =0; I < vertices. Length; i++)101 {102 varVert =Uivertex.simplevert;103Vert.color =color;104Vert.position =Vertices[i]; theVert.uv0 =Uvs[i];106Vbo[i] =Vert;107 }108 returnVbo;109 } the 111}
3. Find an empty object under a canvas, mount the uicircle script, drag the picture you want to display to texture, and change the number of picture edges as needed
4. as follows:
About unity implementing custom polygon Picture effects