当前位置:网站首页>只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体
只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体
2022-07-25 10:29:00 【InfoQ】
- CSDN主页
- GitHub开源地址
- Unity3D插件分享
- 简书地址
- 我的个人博客
- QQ群:1040082875
一、前言
二、搭建场景
2-1 预制体制作



using UnityEngine;
using UnityEngine.UI;
public class PersonItem : MonoBehaviour
{
public Text ID;
public Text Name;
public Text Sex;
public Text Age;
public Text Post;
public Text Work;
public Button ViewInfo;
public Image Backgroud;
}


2-2 新建人员弹窗


2-2 显示人员详细信息弹窗


三、实现代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PersonControl : MonoBehaviour
{
public PersonItem PersonItemParfabs;//人员预制体
public Transform PersonParent;//人员父节点
public GameObject PopupAddInfo;//弹窗 添加信息
public InputField InputName;
public Dropdown DropSex;
public InputField InputAge;
public InputField InputPost;
public InputField InputWorkExp;
public Button BtnAddInfo;//弹窗 中添加信息的所有组件
public GameObject PopupShowInfol;//弹窗 显示信息
public Text Name;
public Text Sex;
public Text Age;
public Text Post;
public Text WorkExp;//弹窗中要显示的信息的所有组件
public Button AddPerson;//添加人员按钮
private int ID = 0;
void Start()
{
//初始化关闭所有弹窗
PopupAddInfo.SetActive(false);
PopupShowInfol.SetActive(false);
//添加按钮事件
AddPerson.onClick.AddListener(() =>
{
PopupAddInfo.SetActive(true);
});
BtnAddInfo.onClick.AddListener(AddInfoEvent);
}
private void AddInfoEvent()
{
ID++;
PersonItem item = Instantiate(PersonItemParfabs, Vector3.zero, Quaternion.identity, PersonParent);
item.ID.text = ID.ToString();
item.Name.text = InputName.text;
item.Sex.text = DropSex.captionText.text;
item.Age.text = InputAge.text;
item.Post.text = InputPost.text;
item.Work.text = InputWorkExp.text;
item.ViewInfo.onClick.AddListener(() => ShowInfo(item.Name.text, item.Sex.text, item.Age.text, item.Post.text, item.Work.text));
item.Backgroud.color = ID % 2 == 1 ? Color.blue : Color.red;//通过奇偶区分 背景演示是蓝色还是红色 用了一个三元表达式
PopupAddInfo.SetActive(false);
}
private void ShowInfo(string name, string sex, string age, string post, string work)
{
PopupShowInfol.SetActive(true);
Name.text = name;
Sex.text = sex;
Age.text = age;
Post.text = post;
WorkExp.text = work;
}
}




四、后言
边栏推荐
- HCIA experiment (09)
- 100W!
- Openstack Skyline 组件安装
- Learn NLP with Transformer (Chapter 3)
- Learn NLP with Transformer (Chapter 4)
- 爬虫基础一
- The integration of two in one has a long way to go
- mysql主从复制与读写分离
- AI system frontier dynamics issue 43: ONEFLOW V0.8.0 officially released; GPU finds human brain connections; AI doctoral online crowdfunding research topic
- NB-IOT控制液晶屏(日期的设置与读取)
猜你喜欢

Hcip experiment (02)

MySQL master-slave replication and read-write separation

mysql主从复制与读写分离

Flask框架——Flask-WTF表单:文件上传、验证码
Learn NLP with Transformer (Chapter 3)

Implementation of recommendation system collaborative filtering in spark

100W了!

SQL语言(一)

The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts

最详细的mysql索引解析(文末附赠思维导图)
随机推荐
Guys, flick CDC table API, Mysql to MySQL, an application that can
[high concurrency] deeply analyze the execution process of worker threads in the thread pool through the source code
Flask framework - flask WTF form: data validation, CSRF protection
Esp32c3 based on the example tutorial of esp32 Rainmaker development under Arduino framework
Redis sentry, high availability executor
B2B2C多商户系统功能丰富,极易二开!!!
【flask高级】结合源码解决flask经典报错:Working outside of application context
性能测试中TPS的计算【杭州多测师】【杭州多测师_王sir】
微信小程序版本更新如何通知用户?
shell-第四天作业
【高并发】如何实现亿级流量下的分布式限流?这些理论你必须掌握!!
学习周刊 - 总第 63 期 - 一款开源的本地代码片段管理工具
Code representation learning: introduction to codebert and other related models
Google Earth engine -- Statistics on the frequency of land classification year by year
MLX90640 红外热成像仪测温模块开发笔记(五)
redis 哨兵,高可用的执行者
Probe into Druid query timeout configuration → who is the querytimeout of datasource and jdbctemplate effective?
Flask框架——flask-caching缓存
BeautifulSoup的一些用法
HCIP(12)