当前位置:网站首页>Day 5 script and UI System
Day 5 script and UI System
2022-06-25 08:28:00 【Code Knight】
What is the GUI?
One 、 Use script to operate common UI Control

1、 Create game interface
Create a new one 3D engineering , By default Scene New China UI Control . The control is actually a game object , stay Hierarchy Right click in the blank area of the window to open the menu , Select in menu UI, There are many controls to choose from .



(1)Canvas And EventSystem brief introduction

(2) Interface scale problem


(3) build UI Test scenarios
Tip:

-01-02

-03

-04

-05

such , A simple UI The interface is set up .
Tip:
The canvas size we have built in the scene may be different from the game ruler , So we want the canvas size to change with the screen size , The following modifications are required :

2、 Rectangular transformation (Rect Transform) Components





3、 picture (Image) Components

Code :
using UnityEngine;
using UnityEngine.UI;//UI The script should contain secondary namespaces
public class NewBehaviourScript : MonoBehaviour
{
Image image;
// You can specify another picture in the compiler
public Sprite otherSprite;
float fillAmount = 0;
void Start()
{
image = GetComponent<Image>();
// Change the picture directly to another picture
if (otherSprite!=null)
{
image.sprite = otherSprite;
}
// Change the picture type to Filled,360° fill , Easy to rotate animation
image.type = Image.Type.Filled;
image.fillMethod = Image.FillMethod.Radial360;
}
void Update()
{
// Make an animated effect of rotating the display , The straight line effect is similar
// Value 0~1
image.fillAmount = fillAmount;
fillAmount += 0.01f;
if (fillAmount > 10)
{
fillAmount = 0;
}
}
}
Demonstration effect :
Tip:

4、 Text (Text) Components


This is a paragraph. <color=#ff0000ff> rich <b> writing </b><size=50> Ben </size></color> 
5、 Button (Button) Components
Button is Unity The control commonly used in :


(1) Button appearance switching method

(2) Buttons are combined controls

(3)OnClick( Click on ) event


using UnityEngine;
public class ButtoTest : MonoBehaviour
{
public void TestButtonClick(int param)
{
Debug.Log("clicked it");
Debug.Log(" The event parameters are :" + param);
}
}



Tip:

6、 Radio buttons (Toggle) Components

using UnityEngine;
using UnityEngine.UI;
public class ToggleTest : MonoBehaviour
{
Toggle toggle;
void Start()
{
toggle = GetComponent<Toggle>();
// Initial unchecked
toggle.isOn = false;
}
public void TestToggleChange(bool b)
{
if(b)
{
Debug.Log(" Check the radio box ");
}
else
{
Debug.Log(" Uncheck the radio box ");
}
}
}




7、 Slider bar (Slider) Components


using UnityEngine;
using UnityEngine.UI;
public class SliderTest : MonoBehaviour
{
// Controlled pictures
public Image image;
// Slide bar component
Slider slider;
void Start()
{
slider = GetComponent<Slider>();
slider.minValue = 0;
slider.maxValue = 1;
// Change the picture type to Filled,360 fill
image.type = Image.Type.Filled;
image.fillMethod = Image.FillMethod.Radial360;
}
void Update()
{
// For each frame, the value of the slider determines the fill size of the picture
image.fillAmount = slider.value;
}
}

8、 Input box (Input Field) Components


9、 Scroll area (Scroll Rect) Components




Tip:




public void OnScrollChange(Vector2 pos)
{
Debug.Log(" Scroll position :"+pos)
}Two 、 Scripts and event systems


Tip:

1、 Common input events




2、 Common input event parameters




3、 Dynamically add event response methods


using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
Button btn;
// Get three sub buttons , Add respectively OnClick event
btn = transform.GetChild(0).GetComponent<Button>();
btn.onClick.AddListener(Btn1);
// use lambda The same goes for expressions
btn = transform.GetChild(1).GetComponent<Button>();
btn.onClick.AddListener(() => { Debug.Log(" Button 2"); });
btn = transform.GetChild(2).GetComponent<Button>();
btn.onClick.AddListener(Btn3);
}
void Btn1()
{
Debug.Log(" Button 1");
}
void Btn3()
{
Debug.Log(" Button 3");
Debug.Log(" Delete button 3 Response function of ");
Button btn = transform.GetChild(2).GetComponent<Button>();
btn.onClick.RemoveAllListeners();
}
}

4、 Event trigger (Event Trigger)
5、 Advanced techniques for dynamically binding events




3、 ... and 、 Example : Interface making and adaptation

1、 Set up UI canvas







2、 Make game interface preparation
(1) Prepare my material

(2) Initialize the material

open Window ----2D---Sprite Editor open Sprite Editor window ,( then Project Select the picture in the window )


Next, stretch to see that the rounded corners of the image will be very smooth .

3、 Making the game interface
(1) Interface partition

Build the top control
Maintain zoom
4、 Make backpack interface

5、 Use the progress bar to make a blood bar

边栏推荐
- 如何成为一名软件测试高手? 月薪3K到17K,我做了什么?
- How do I install the software using the apt get command?
- [supplementary question] 2021 Niuke summer multi school training camp 4-N
- Use Adobe Acrobat pro to resize PDF pages
- Static web server
- A solution to slow startup of Anaconda navigator
- [supplementary question] 2021 Niuke summer multi school training camp 6-n
- [red flag Cup] Supplementary questions
- Cloud computing exam version 1 0
- 4 reasons for adopting "safe left shift"
猜你喜欢

Allgero reports an error: program has encoded a problem and must exit The design will be saved as a . SAV file

How to analyze the grey prediction model?

Deep learning series 45: overview of image restoration

家庭服务器门户Easy-Gate

Daily question brushing record (III)

Deep learning series 48:deepfaker

GPU calculation

五分钟快速搭建一个实时人脸口罩检测系统(OpenCV+PaddleHub 含源码)

双周投融报:资本埋伏Web3基础设施

Network model -- OSI model and tcp/ip model
随机推荐
VOCALOID notes
Is the securities account given by Qiantang education business school safe? Can I open an account?
初体验完全托管型图数据库 Amazon Neptune
Stm32cubemx Learning (5) Input capture Experiment
Want to open an account, is it safe to open an online stock account?
Measure the current temperature
堆栈认知——栈溢出实例(ret2libc)
2022年毕业生求职找工作青睐哪个行业?
Find out the possible memory leaks caused by the handler and the solutions
How to calculate the characteristic vector, weight value, CI value and other indicators in AHP?
[unexpected token o in JSON at position 1 causes and solutions]
TCP acceleration notes
Data-centric vs. Model-centric. The Answer is Clear!
Is there any risk in making new bonds
Use Adobe Acrobat pro to resize PDF pages
Free SSL certificate acquisition tutorial
Talk about the future of cloud native database
Bluecmsv1.6- code audit
双周投融报:资本埋伏Web3基础设施
TCP MIN_RTO 辩证考



