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
(18) unity4.6 learn ugui Chinese Document ------- tips-creating UI elements from Scripting