2016.4.14
Yesterday saw Glaze's Unity3d Institute Ugui an optimization efficiency tips: http://www.xuanyusong.com/archives/4006
The code he says is intact:
Using unityengine;using unityengine.ui;using system.collections;using unityeditor;using UnityEngine.EventSystems;// /<summary>///Create Text, Image when the default is not selected Raycasttarget, etc.//</summary>public class overridecreateuimenu{/// Lt;summary>///When UI elements are created for the first time. No canvas, EventSystem all to build, canvas as parent node//And then empty the location of the UI elements will be actively added to the canvas itself///in the non-UI tree Gameobject on the new UI element will also be actively added to the canvas Under (Default in the UI tree)///Join to the specified UI element///</summary> [MenuItem ("Gameobject/ui/image")] static void Creatimages () {var canvasobj = Securitycheck (); if (! Selection.activetransform)//created in the root folder, own the initiative to move to the canvas under {//Debug.Log ("No Selection of objects"); Image (). Transform. SetParent (Canvasobj.transform); } else//(Selection.activetransform) {if (! Selection.activetransform.getcomponentinparent<canvas> ())//Not under the UI tree {Image (). transfor M.setparent (Canvasobj.transform); } else {Image (); }}} private static Gameobject Image () {Gameobject go = new Gameobject ("X_image", typeof (Image)); Go. Getcomponent<image> (). Raycasttarget = false; Go.transform.SetParent (Selection.activetransform); Selection.activegameobject = go; return go; }//We want to set the default font [MenuItem ("Gameobject/ui/text")] static void Creattexts () {var canvasobj = Securitychec K (); if (! Selection.activetransform)//created in the root folder. Voluntarily move to the canvas under {//Debug.Log ("no object selected"); Text (). Transform. SetParent (Canvasobj.transform); } else//(Selection.activetransform) {if (! Selection.activetransform.getcomponentinparent<canvas> ())//Not under the UI tree {Text (). Transform . SetParent (Canvasobj.transform); } else {Text (); }}} private static GameobjECT Text () {Gameobject go = new Gameobject ("X_text", typeof (text)); var text = go. Getcomponent<text> (); Text.raycasttarget = false; Text.font = assetdatabase.loadassetatpath<font> ("ASSETS/ARTS/FONTS/ZH_CN. TTF "); Default font go.transform.SetParent (Selection.activetransform); Selection.activegameobject = go; Go. Addcomponent<outline> (); Add add-ons by default return go; }//Assume that the first time you create a UI element may not have a Canvas, EventSystem Object! private static Gameobject Securitycheck () {Gameobject canvas; var cc = object.findobjectoftype<canvas> (); if (!CC) {canvas = new Gameobject ("_canvas", typeof (Canvas)); } else {canvas = Cc.gameobject; } if (! Object.findobjectoftype<eventsystem> ()) {Gameobject EventSystem = new Gameobject ("_eventsystem", typeof (EventSystem)); } return canvas; }}
??
Unity custom Object Generation for Image and text