A friend asked me how to use the Ugui in unity to achieve the scratch card function, did not do before, but I think the next, should use Shader can be achieved. So it took some time to realize the function of the next change.
The following is the implementation method.
Here I mainly use a script and a shader.
Shader "Unlit/transparent colored Eraser" {Properties {_maintex ("Base (RGB), Alpha (A)", 2D) = "White" {} _rendtex ("Base (RGB), Alpha (A)", 2D) = "white" {}} subshader {LOD + Tags { "Queue" = "Transparent" "ignoreprojector" = "True" "rendertype" = "Transparent"} Pass {cull off Lighting off Zwrite off Fog {Mode off} Offset-1,-1 colormask RGB alphatest Greater. Blend Srcalpha Oneminussrcalpha Colormaterial ambientanddiffuse cgprogram #pragma vertex vert #pragma frag ment frag #include "unitycg.cginc" sampler2d _maintex; FLOAT4 _maintex_st; Sampler2d _rendtex; struct appdata_t {float4 vertex:position; Half4 Color:color; FLOAT2 texcoord:texcoord0; }; struct V2F {float4 vertex:position; Half4 Color:color; FLOAT2 texcoord:texcoord0; }; v2f Vert (appdata_t v) {v2f o; O.vertex = Mul (UNITY_MATRIX_MVP, V.vertex); O.color = V.color; O.texcoord = V.texcoord; return o; } Half4 Frag (v2f in): COLOR {//Sample the texture half4 col = tex2 D (_maintex, In.texcoord) * IN.COLOR; Half4 rnd = tex2d (_rendtex, In.texcoord) * IN.COLOR; COL.A = RND.A; return col; } ENDCG}}}
Another script
usingUnityengine;usingUnityengine.ui;usingUnityengine.eventsystems;usingSystem.Collections; Public classUierasertexture:monobehaviour, ipointerdownhandler,ipointeruphandler{ Publicrawimage image; Public intBrushscale =4; Texture2d Texrender; Recttransform Mrecttransform; Canvas canvas; voidAwake () {Mrecttransform= getcomponent<recttransform> (); Canvas= Gameobject.find ("Canvas"). Getcomponent<canvas>(); } voidStart () {Texrender=NewTexture2d (Image.mainTexture.width, Image.maintexture.height,textureformat.argb32,true); Reset (); } BOOLIsmove =false; Public voidOnpointerdown (pointereventdata data) {Debug.Log ("Onpointerdown ..."+data.position); Start=Convertscenetoui (data.position); Ismove=true; } Public voidOnpointerup (pointereventdata data) {Ismove=false; Debug.Log ("Onpointerup ..."+data.position); OnMouseMove (data.position); Start=Vector2.zero; } voidUpdate () {if(ismove) {OnMouseMove (input.mouseposition); }} Vector2 start=Vector2.zero; Vector2 End=Vector2.zero; Vector2 Convertscenetoui (Vector3 posi) {Vector2 postion; if(Recttransformutility.screenpointtolocalpointinrectangle (Mrecttransform, Posi, Canvas.worldcamera, outpostion)) { returnpostion; } returnVector2.zero; } voidOnMouseMove (Vector2 position) {End=Convertscenetoui (position); Draw (NewRect (end.x+texrender.width/2, end.y+texrender.height/2, Brushscale, Brushscale)); if(start. Equals (Vector2.zero)) {return; } Rect disract=NewRect ((start+end). x/2+texrender.width/2, (start+end). y/2+texrender.height/2, Mathf.abs (End.x-start.x), Mathf.abs (end.y-start.y)); for(intx = (int) Disract.xmin; X < (int) Disract.xmax; X + +) { for(inty = (int) Disract.ymin; Y < (int) Disract.ymax; y++) {Draw (NewRect (x, Y, Brushscale, Brushscale)); }} Start=end; } voidReset () { for(inti =0; i < texrender.width; i++) { for(intj =0; J < Texrender.height; J + +) {color color=Texrender.getpixel (I,J); COLOR.A=1; Texrender.setpixel (I,j,color); }} texrender.apply (); Image.material.SetTexture ("_rendtex", Texrender); } voidDraw (rect rect) { for(intx = (int) Rect.xmin; X < (int) Rect.xmax; X + +) { for(inty = (int) Rect.ymin; Y < (int) Rect.ymax; y++) { if(X <0|| X > Texrender.width | | Y <0|| Y >texrender.height) {return; } Color Color=texrender.getpixel (x, y); COLOR.A=0; Texrender.setpixel (X,y,color); }} texrender.apply (); Image.material.SetTexture ("_rendtex", Texrender); }}
How to use please see OH.
Oneself PS a block diagram, I casually P's.
Create a new material ball, give him the shader, you can not give it a preset texture, and so on in the script to give. Hanging on the Ugui image, the script is also hanging on the image, Rawimage and image casually, I haven't tried. Everyone can try.
is not feeling very simple. Do you have any comments on my message or add me Q 14,535,662,831 to learn.
Unity3d Ugui for scratch card eraser