Development of the time necessary to get a sub-object component of the interface, although very simple, but if every time you get the code, it is a key thing very scary.
Now the project uses a tool, the interface point, will be exported to match the interface structure of the class. As long as the class is initialized, it is convenient to get the component of this interface object.
The tool idea is this:
1, according to the name of the child object to determine the components to be obtained. For example, the suffix _txt indicates that uilabel,_go represents Gameobject.
2. Iterate through all child objects and cache the sub-objects with suffixes.
3, according to the cache sub-object, according to a certain string stitching rules, write to local script.
An interface structure and the exported interface script results are as follows
As long as the interface resources (Gameobject) are passed in, you can directly get the sub-object components, greatly improving the development efficiency. Of course, the project has a series of encapsulation and abstraction of this script, which is shown separately.
(PS: This is my first project, so I am not sure how other projects deal with this problem, if there is a better idea to tell me)
Gossip less, directly on the dry. (Note that the comparison of the whole, it will not show my slag of expression ability)
Tool Script:
usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingUnityeditor;usingUnityengine; Public classCreatesprite {//object of the current action Private StaticGameobject Curgo; //the type of component that the suffix corresponds to Public Staticdictionary<string,string> Typemap =Newdictionary<string,string>() { { "SP",typeof(Uisprite). Name}, {"txt",typeof(UILabel). Name}, {"btn",typeof(UIButton). Name}, {"Go",typeof(Gameobject). Name},}; //Script Templates Private StaticCreatespriteunit Info; //under the Project window, select the interface that you want to export, and then click gameobject/Export Script[MenuItem ("gameobject/Export Scripts")] Public Static voidcreatespriteaction () {gameobject[] gameobjects=selection.gameobjects; //guarantee that there is only one object if(gameobjects.length==1) {Info=NewCreatespriteunit (); Curgo= gameobjects[0]; Readchild (Curgo.transform); Info.classname= Curgo.name +"UIPanel"; Info. Wtiteclass (); Info=NULL; Curgo=NULL; } Else{Editorutility.displaydialog ("Warning","you can only choose one gameobject .","Determine"); } } //traversing all child objects, the Getchild method can only get the first layer of child objects. Public Static voidreadchild (Transform tf) {foreach(Transform ChildinchTF) { string[] Typearr = Child.name.Split ('_'); if(Typearr.length >1) { stringTypekey = Typearr[typearr.length-1]; if(Typemap.containskey (Typekey)) {INFO.EVENLIST.ADD (NewUiinfo (Child.name, Typekey, Buildgameobjectpath (child). Replace (Curgo.name +"/",""))); } } if(Child.childcount >0) {readchild (child); } } } //gets the path, which is the name of the current object, and needs to replace the turn-around part with replace Private Static stringBuildgameobjectpath (Transform obj) {varBuffer =NewStringBuilder (); while(obj! =NULL) { if(buffer. Length >0) buffer. Insert (0,"/"); Buffer. Insert (0, Obj.name); Obj=obj.parent; } returnbuffer. ToString (); }}//template for exporting scripts Public classcreatespriteunit{ Public stringclassname; Public stringTemplate =@"using unityengine;using system.collections;using system.collections.generic;public class @ClassName {@fields pub LIC void Onawake (Gameobject viewgo) {@body1} public void OnDestroy () {@body2}}"; //all child object information for the cache PublicList<uiinfo> evenlist =NewList<uiinfo>(); /// <summary> ///write the stitched script locally. ///(You can have a window to support renaming and select the path, the real project is with these features)/// </summary> Public voidWtiteclass () {BOOLFlag =true; BOOLthrowOnInvalidBytes =false; UTF8Encoding encoding=Newutf8encoding (flag, throwOnInvalidBytes); BOOLAppend =false; StreamWriter writer=NewStreamWriter (Application.datapath +"/"+ classname +". CS", append, encoding); Writer. Write (Getclasss ()); Writer. Close (); Assetdatabase.refresh (); } //Script Stitching Public stringgetclasss () {varFields =NewStringBuilder (); varBody1 =NewStringBuilder (); varBody2 =NewStringBuilder (); for(inti =0; I < Evenlist. Count; i++) {fields. Appendline ("\ t"+Evenlist[i].field); Body1. Appendline ("\t\t"+evenlist[i].body1); Body2. Appendline ("\t\t"+evenlist[i].body2); } template= Template. Replace ("@ClassName", classname). Trim (); Template= Template. Replace ("@body1", Body1. ToString ()). Trim (); Template= Template. Replace ("@body2", Body2. ToString ()). Trim (); Template= Template. Replace ("@fields", fields. ToString ()). Trim (); returntemplate; }}//Sub-object information Public classuiinfo{ Public stringfield; Public stringbody1; Public stringBody2; PublicUiinfo (stringNamestringTypekey,stringpath) {Field=string. Format ("Public {0} {1};", Createsprite.typemap[typekey], name); if(Typekey = ="Go") {body1=string. Format ("{0} = ViewGO.transform.Find (\ "{1}\"). Gameobject;", name, Path, Createsprite.typemap[typekey]); } Else{body1=string. Format ("{0} = ViewGO.transform.Find (\ "{1}\"). Getcomponent<{2}> ();", name, Path, Createsprite.typemap[typekey]); } body2=string. Format ("{0} = null;", name); }}
Unity tools for getting the UI interface structure