Preface: Just from Python to U3d, the first project involves drawing lines, just beginning to think that is not simple, is not to get the position after coloring, H5 in the canvas can be achieved, when I looked at Unity's document after a face confused, incredibly no related methods. No way can only be found on the internet, involving a lot of drawing lines, various pits.
Unity draw Line The simplest way to implement Linerender components:
Linerender is relatively simple to implement.
1 Public classLine:monobehaviour2 {3 /*4 Linerenderer Draw Line advantage is simple, easy to control, on the mobile side of the effect is better5 Cons: 1. The line width can not be highly customized (the brush effect is not good to achieve),6 2. Cannot repeat the decal (crayon) (after 2017, the map can be repeated but the effect is not very good--texture Mode is set to repeat),7 3, the eraser practice is more complex:8 1. Draw a white line with a linerenderer9 2. Render the image on the renderertextureTen 3. Use shader to buckle off the white (the disadvantage cannot draw white) One 4, Save method self-energy A 5, there are too many things to save the recovery record . - */ - PrivateGameobject linerendererobj; the Privatelinerenderer Linerenderer; - PrivateList<vector3> Worldpos =NewList<vector3>(); - PublicGameobject Linerendererpre; - Private BOOLmoving; + Private voidUpdate () - { + A #regionBrush at if(Input.getmousebuttondown (0)) - { -moving =true; - Initline (color.red); - - } in if(Input.getmousebuttonup (0)) - { tomoving =false; + worldpos.clear (); - } the #endregion * $ Panax Notoginseng - #regionRubber the if(Input.getmousebuttondown (1)) + { Amoving =true; the Initline (color.white); + - } $ if(Input.getmousebuttonup (1)) $ { -moving =false; - worldpos.clear (); the } - #endregionWuyi if(moving) the { -Vector3 pos = Camera.main.ScreenToWorldPoint (input.mouseposition);//get the world coordinates of the click Point WuPos.z =0;//set to 0 for better results - if(Worldpos.count >1) About { $ if(Vector3.distance (POS, Worldpos[worldpos.count-1]) <0.08f) - { - return; - } A } + the Worldpos.add (POS); - Draw (); $ //Bezierpathdraw ();//Bézier curve Optimization brush effect is not very different, very card the the } the } the - Private voidinitline (color color) in { theLinerendererobj = Instantiate (Linerendererpre, This. Transform); theLinerenderer = linerendererobj.getcomponent<linerenderer>(); AboutLinerenderer.numcapvertices =5;//controls the number of points added at the end of the more rounded theLinerenderer.numcornervertices =5;//controls the number of points added at the corner is more rounded theLinerenderer.startcolor = color;//the color of the start theLinerenderer.endcolor = color;//End of color +Linerenderer.startwidth =0.2f;//width of start -Linerenderer.endwidth =0.2f;//End of width the }Bayi Private voidDraw () the { theLinerenderer.positioncount =Worldpos.count; - linerenderer.setpositions (Worldpos.toarray ()); -}
View Code
Unity3d Brush Implementation Series 01-linerender