当前位置:网站首页>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
边栏推荐
- Is it safe to open an account online? Xiaobai asks for guidance
- Stimulsoft Ultimate呈现报告和仪表板
- Stm32cubemx learning (5) input capture experiment
- DNS protocol and its complete DNS query process
- How to calculate critical weight indicators?
- STM32CubeMX 學習(5)輸入捕獲實驗
- 初识生成对抗网络(11)——利用Pytorch搭建WGAN生成手写数字
- Electronics: Lesson 008 - Experiment 6: very simple switches
- Go language learning tutorial (13)
- How to calculate the independence weight index?
猜你喜欢
TCP stuff
How to calculate the D value and W value of statistics in normality test?
企业全面云化的时代——云数据库的未来
测一测现在的温度
初识生成对抗网络(11)——利用Pytorch搭建WGAN生成手写数字
How to calculate critical weight indicators?
Log in to MySQL 5.7 under ubuntu18 and set the root password
Quickly build a real-time face mask detection system in five minutes (opencv+paddlehub with source code)
双周投融报:资本埋伏Web3基础设施
STM32CubeMX 学习(5)输入捕获实验
随机推荐
是否可以给数据库表授予删除列对象的权限?为什么?
Overview of image super score: the past and present life of image super score in a single screen (with core code)
4 raisons inconnues d'utiliser le "déplacement sûr à gauche"
Deep learning series 48:deepfaker
Quickly build a real-time face mask detection system in five minutes (opencv+paddlehub with source code)
How to calculate the positive and negative ideal solution and the positive and negative ideal distance in TOPSIS method?
Biweekly investment and financial report: capital ambush Web3 infrastructure
Rank sum ratio (RSR) index calculation
Rosparam statement
What is SKU and SPU? What is the difference between SKU and SPU
五分钟快速搭建一个实时人脸口罩检测系统(OpenCV+PaddleHub 含源码)
What are the indicators of DEA?
TCP 加速小记
In 2022, which industry will graduates prefer when looking for jobs?
软件工程复习题
什么是SKU和SPU,SKU,SPU的区别是什么
A solution to slow startup of Anaconda navigator
软件测试月薪10K如何涨到30K,只有自动化测试能做到
Unit conversion - mm to pixel - pixel to MM
FFT [template]