"Unity" 8.2 GUI style and Guiskin

Source: Internet
Author: User

Categories: Unity, C #, VS2015

Date Created: 2016-04-27 First, custom GUI Control

Feature controls (functional control) are necessary for the game, and the appearance of these controls is important for the beauty of the game. In Unitygui, there are many details of the appearance of a micro-control widget (control).

By default, when you create a control when you do not define Guistyle, the default guistyle of Unity is applied. This style is built into Unity and can be used to quickly prototype a published game, or you can choose not to style the control.

If you are dealing with a large number of different guistyles, you can define all guistyles in a single guiskin. Guiskin is not just a collection of guistyles. The style (style) changes the appearance of the GUI control

Guistyles mimics the cascading style sheet (CSS) of the Web browser. Many CSS methods are adjusted, including the differentiation of the individual state attributes of the style and the separation of the content from the appearance.

Where control defines the content, and style (style) defines the appearance. This allows you to create combinations, such as function toggle keys (Toggle) that look like regular buttons (button). Iii. the difference between skin and style

As mentioned above, Guiskins is a collection of guistyles. Style defines the appearance of a GUI control. If you want to use a style, you can also use no skin (skins).

1, processing style (style)

All GUI controls have an optional last argument: The Guistyle that is used to display the control. If it is ignored, the default Guistyle of Unity is used. Style processing is done internally by applying the name of the control type as a string, so the GUI. button () using the "buttons" style, GUI. Toggle () uses the "Toggle" style, and so on. However, you can override the default Guistyle by specifying the default Guistyle of the control as the last parameter.

usingUnityengine;usingSystem.Collections; Public classGuitest:monobehaviour {voidOngui () {//Create a label that uses the Guistyle "box"Gui. Label (NewRect (0,0, $, -),"Hi-i ' m a label looking like a box","Box"); //Create a button that uses the toggle key GuistyleGui. Button (NewRect (Ten, $, the, -),"This is a button","Toggle"); }}

2. Create a public variable Guistyle

When you declare a public guistyle variable, all the elements of that style are displayed in the viewer (Inspector). You can edit all the different values here:

usingUnityengine;usingSystem.Collections; Public classGuitest:monobehaviour { PublicGuistyle CustomButton; voidOngui () {//Create a button. Submit the above defined Guistyle as the style usedGui. Button (NewRect (Ten,Ten, Max, -),"I am a Custom Button", CustomButton); }}

3. Change different style elements

When you have declared Guistyle, you can modify this style in the viewer (Inspector). A large number of States (state) are defined in the viewer and applied to any type of control.

Note: You must assign a background (Background) color to each control state before applying the specified text color. Iv. Treatment of skin (Skins)

Unity's default controls look simple. In the game development process, the developer will be based on the type and content of the game does not count a set of personalized game interface. In unity, you can change the default style of a control by configuring Guiskin to make it look like a game-style control.

The Guiskin contains many different styles (styles), which basically completely update the design of all GUI controls (GUI control).

1. Create a new Guiskin

To create a Guiskin, from the menu bar Select resources (Assets), create a (create)->gui skin (GUI skin) This will create a new GUI skins within your project folder (GUI Skin). Select the GUI skin to see all the guistyles defined by the skin (skins) in the viewer (Inspector).

2. Apply the skin to the GUI

To use the skin you create, assign it to Gui.skin in the Ongui () function:

usingUnityengine;usingSystem.Collections; Public classGuitest:monobehaviour { PublicGuiskin MySkin; voidOngui () {//The skin is assigned to the skin that is currently being used. Gui.skin =MySkin; //Create a button. This will get the default "button" style from the skin assigned to MySkin. Gui. Button (NewRect (Ten,Ten, Max, -),"skinned Button"); }}

This way, you can switch the skin at will with one Ongui () Call:

usingUnityengine;usingSystem.Collections; Public classGuitest:monobehaviour { PublicGuiskin MySkin; Private BOOLToggle =true; voidOngui () {//assign the skin to the skin that is currently usedGui.skin =MySkin; //creates a toggle key. This will get the "button" style from the skin assigned to MySkin. Toggle = GUI. Toggle (NewRect (Ten,Ten, Max, -), Toggle,"skinned Button","Button"); //assign current skin to Unity's default skinGui.skin =NULL; //Create a button. This will get the default "button" style from the built-in skin. Gui. Button (NewRect (Ten, *, Max, -),"built-in Button"); }}

3. Example

Guiskin need to be created and configured before use. To create a method, click the Create button above the project view and click the Guiskin option in the pop-up menu. Another option is to open "Assets", "Create", "Guiskin" in the menu bar.

The new Guiskin file appears in Project view, click the Guiskin file, and you can set its parameters in Inspector view.

In a Guiskin, you can set the style of all GUI controls, such as button, toggle, and so on.

The following is an example of a button that shows how to set its style through Guiskin.

Suppose you now need to change the background picture of the button, and set the font on the button in italics, you can first make the following picture material:

First put the button's background and font file in project's assets, then view the Guiskin Viewer view, click button to collapse the item, expand the button's style parameter, change normal, Hover, Active, on Normal, Onhover, on Active, font, font size are some of the parameters.

You can now try the modified effect. Declare a variable of type Cuiskin in the Cui script, and the value of the variable in the Inspector view is not set to the newly modified Cuiskin file.

Assigning a variable to the Cui.skin parameter before the control code is drawn, the control is drawn in the user-configured Guiskin style, as shown in the following example code:

Public Guiskin Myguiskin;

void Ongui ()

{

Gui.skin=myguiskin;

Gui. Button (Rect (60,50,240,60), "custom Button");

}

Press the play button and you will see the following effect:

"Unity" 8.2 GUI style and Guiskin

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.