In ngui, uiwidget is the base class of all components. It stores the display content, colors, depth, position, size, angle, and Polygon Shape, which uipanel belongs. This is what the uiwidget undertakes. All subclasses of uiwidget have the same attributes and tasks as above. The relationship between uiwidget and uipanel is very close, because uipanel undertakes all the rendering work of uiwidget, while uiwidget only undertakes the data to be rendered for storage. Therefore, when the uiwidget changes the texture, material ball, or even the uipanel parent node, it will notify uipanel in time: "I have changed the configuration, and you have to get my rendering data again ".
Open uiwidget. CS and you can see the following code:
[Serialize] [serializefield] protected material mmat; // material [hideininspector] [serializefield] protected texture mtex; // texture [hideininspector] [serializefield] color mcolor = color. white; // color [hideininspector] [serializefield] bytes mpivot = bytes. center; // alignment position [hideininspector] [serializefield] int mdepth = 0; // depth protected transform mtrans; // coordinates protected uipanel mpanel; // corresponding uipanel protected bool mchanged = true; // whether to change protected bool mplaymode = true; // mode vector3 mdiffpos; // quaternion mdiffrot; // rotating difference vector3 mdiffscale; // scaling difference int mvisibleflag =-1; // visible sign // widget's generated geometry uigeometry mgeom = new uigeometry (); // multi-deformation instance
The most important method in uiwidget is onfill (), which is the method for updating and rendering the multi-edge type.
/// <summary>/// Virtual function called by the UIPanel that fills the buffers./// </summary>virtual public void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols) { }
The following is a uicolorqued ngui plug-in uicolorquad. CS written in the main program.
/// Author: Kk // using unityengine; using system. collections; /// <summary> // solid color square, composed of two triangles, only four vertices /// </Summary> [executeineditmode] [addcomponentmenu ("ngui/ac-plugins/colorquad")] public class uicolorquad: uiwidget {// <summary> /// the material used for solid color rectangular rendering. It is independent and not shared. /// </Summary> Private Static material m_uicolorquadmaterial = NULL; // static, unique, shared public override Material material {get {return uicolorquad. m_uic Olorquadmaterial;} protected override void awake () {base. awake ();} protected override void onstart () {base. onstart (); mchanged = true; // make it re-rendered once during start, otherwise there will be nothing after the client loads} public void setsize (float _ widht, float _ height) {base. width = (INT) _ widht; base. height = (INT) _ height; base. mchanged = true;} /// <summary> // displays the content. The job is to specify how to display and what to display. The content to be displayed is stored in the uiwidget // </Summary> /// <Param name = "Verts"> </param> /// <Param name = "Uvs "> displayed Polygon Shape </param> // <Param name =" Cols "> color allocation </param> Public override void onfill (betterlist <vector3> Verts, betterlist <vector2> Uvs, betterlist <color32> Cols) {// start to draw a grid, vertex, and rectangular vector3 [] arrverts = localcorners; // The rectangle bar is composed of four angles. For (INT I = 0; I <arrverts. length; I ++) {Verts. add (arrverts [I]);} // map point (INT I = 0; I <arrverts. length; I ++) {Uvs. add (New vector2 (0, 0);} // vertex color pmacolor = nguitools. applypma (this. color); // ngui PMA for (INT I = 0; I <arrverts. length; I ++) {cols. add (pmacolor) ;}}// create the void checkquadmaterial () {string szuseshadername = "unformatted/premultiplied colored"; // ngui ~ If (uicolorquad. m_uicolorquadmaterial = NULL | // regenerate the material = NULL | material. shader = NULL | material. shader. Name! = Szuseshadername) {gameobject. destroyimmediate (uicolorquad. m_uicolorquadmaterial); uicolorquad. m_uicolorquadmaterial = new material (shader. find (szuseshadername); uicolorquad. m_uicolorquadmaterial.name = "uicolorquadmaterial"; // generate a 1-point white texture texture2d whitetex = new texture2d (1, 1); For (INT y = 0; y <whitetex. height; ++ y) {for (INT x = 0; x <whitetex. width; ++ X) {whitetex. setpixel (X, Y, new color (1, 1, 1, 1) ;}} whitetex. apply (); uicolorquad. m_uicolorquadmaterial.settexture ("_ maintex", whitetex) ;}} protected override void onupdate () {base. onupdate (); If (mchanged) {mchanged = false; checkquadmaterial ();}}}
[CB] ngui series uiwidgets