Unity custom Object Generation for Image and text

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.