(18) unity4.6 learn Ugui Chinese Document ------- tips-Creating UI elements from scripting, ugui-creating

Source: Internet
Author: User

(18) unity4.6 learn Ugui Chinese Document ------- tips-Creating UI elements from scripting, ugui-creating

Hello everyone, I am Sun Guangdong. Reprinted please indicate the source: http://write.blog.csdn.net/postedit/38922399

More comprehensive content please see my game pretty cool address: http://www.unitymanual.com/forum.php? Mod = guide & view = my

4. CreatingUI elements from scripting


If you are creating a dynamic UI, where the UI elements in the game appear, disappear, or change based on user operations or other operations, you may need to make a script, the new ui elements of the instance are based on the custom logic.


Creating a prefab of the UI element


To easily and dynamically instantiate a UI element, the first step is to create a preset body of the UI element type that you want to instantiate. Set the user interface element. You want it to look like it in the scene, and drag it to the Project View to make it a preset.


For example, the preset body of a Button may be a game object with an Image component, a Button component, and a sub-game object with a Text component. You can set different parameters based on your needs.


You may wonder why we don't have an API to create various types of controls, including visual effects and everything. The reason is that this method is infinite. For example, a button can be set. It may be an image, text, or both? Maybe even multiple images? What are text fonts, colors, font sizes, and Alignment Methods? What genie should images use? By allowing you to create a preset and instantiate it, you can set it exactly as you want. If you want to change the appearance and feeling of your user interface in the future, you can only change the preset, which will be reflected in your UI, including dynamic user interface creation.


Instantiating the UI element


The preset bodies of the UI elements are instantiated normally using the Instantiate method. We recommend that you use the Transform. SetParent method and worldPositionStays parameter to set the parent object of the UI element to false during instantiation.


Positioning the UI element


Generally, RectTransform is used to locate the UI element. If the UI element is a sub-object of the Layout Group, the steps for automatic locating and locating can be skipped.


It is useful when locating a Rect Transform. first determine whether it has or should have any stretching behavior. AnchorMin and anchorMax attributes do not have the same tensile behavior.


For non-stretched Rect Transform, it is easiest to set this position by setting the anchoredPosition and sizeDelta attributes. The position of the AnchoredPosition specified by the AnchoredPosition and anchors. The same size of SizeDelta is sometimes not stretched.


For stretched Rect Transform, it can be the location of the offsetMin and offsetMax attributes used for simple settings. The lower left corner of the left anchor rectangle of the angle specified by the OffsetMin attribute. The OffsetMax attribute specifies the upper right corner of the right anchor rectangle of the relative upper right anchor.


Customizing the UI Element


If you instantiate multiple UI elements dynamically, you will expect them to look the same and unlikely. Whether it's a button in the menu, inventory or something else, you may want individual items to have different texts or images that do different things when interacting.


This is through obtaining various components and changing their attributes. From how to use UnityEvents in the script, see the script reference about the image and text components ,.


 


Zookeeper


Related Article

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.