Вы здесь
Главная > Мой блог > Unity3d > UI Canvas > Создание UI используя c# в Unity 3D

Создание UI используя c# в Unity 3D

Introduction

New UI is introduced from Unity 4.6 on wards. New UI controls will be added in the similar way as other components in Unity.
Basic step is to create a GameObject and then add the required UI elements and other components to it. If you want to have an hierarchy of UI controls then add child GameObject and continue the process mentioned above.
Here we will create a simple dialog as show below just using scripts.

Name spaces for the new UI controls

MonoBehaviour implementation

We will have a game object added in to our scene and add a script to the object. From this script we will create UI controls.

Canvas Object

All the UI controls in the NEW UI should be a child of Canvas class. Lets start by creating Canvas class.
We will create a GameObject for canvas first and then set the layer to UI and also make it child of current GameObject.
Actual Canvas UI class and other required component will be added to this GameObject.

Now we will add components RectTransform, Canvas, CanvasScaler and GraphicRaycaster.

Creating event system

Next we will create EventSystem so that our controls will responds to events.

Just like the Canvas creation we will now add components to the event system GameObject

Panel to hold UI controls

We will use a Panel to hold our UI controls, so lets create the Panel now.

Now lets add the RectTransform component, RectTransform will be stretched which takes 80% of the Canvas size.

Now we will add a background image to the panel. We will use a simple png file as the background. png file should be present at Assets\Resources.

Creating Text UI control

Now lets go and create Text control.

Now we will add RectTransform which sets the size and position, CanvasRenderer and the actual Text component. Text will be positioned at (x, y)

Creating buttons

Creating button has two part, first creation of the button itself and the second part is creating a text object for displaying the text.

Now lets create the RectTransform and position our button accordingly.

Setting background image for button will be similar to that of panel’s. The png file button_bkg.png should be at Assets\Resources as before.

Creating the actual button, eventListner is of type UnityAction.

Next we will have to create a text object with the message and add it to the buttonObject. We can use the same code which creates the text object for this. So I am not a showing that here.

Ok now we have created NEW UI using just scripts.

Complete source code is pasted below. Please note that the code is no where near release quality.

UI Using prefab

Another, may be better, option is to use prefab. Create the UI using the unity editor and create prefab.

Using prefab we can launch the UI as shown below, Here DialogCanvas is the prefab and Yes and No are name of the GameObjects which has Button component.

OnYesClicked and OnNoClicked is declared as,

Источник: http://chikkooos.blogspot.com/

Добавить комментарий

Top