当前位置:网站首页>Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
2022-07-31 21:34:00 【天添向上UT】
Find,FindGameObjectWithTag,FindGameObjectsWithTag三种方式,推荐FindGameObjectsWithTag方式,性能最优。其次是FindGameObjectWithTag,最后是Find
推荐FindGameObjectsWithTag方式返回的GameObject数组。FindGameObjectWithTag和Find返回的是GameObject。
FindGameObjectsWithTag和FindGameObjectsWithTag方式获取的如下的Tag。
实现的功能:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//Find,FindGameObjectWithTag,FindGameObjectsWithTag总结
//Find,FindGameObjectWithTag,FindGameObjectsWithTag三种方式,推荐FindGameObjectsWithTag方式,性能最优。其次是FindGameObjectWithTag,最后是Find
//推荐FindGameObjectsWithTag方式返回的GameObject数组。FindGameObjectWithTag和Find返回的是GameObject。
public class Demo : MonoBehaviour
{
public GameObject myGameObject;
public GameObject myGameObject2;
public GameObject myGameObject3;
GameObject panel;
GameObject panel2;
GameObject[] panel3;
GameObject[] panel4;
void Start()
{
myGameObject = Resources.Load<GameObject>("Prefabs/Test");
myGameObject2 = Resources.Load<GameObject>("Prefabs/Test2");
myGameObject3 = Resources.Load<GameObject>("Prefabs/Test3");
//FindWithTagsMethod();
//FindWithTagMethod2();
//FindMethod2();
FindMethod();
//SetAsFirstSibling是在编辑器里往上移动层级。显示在画面的后面。
//myGameObject.transform.SetAsFirstSibling();
//myGameObject2.transform.SetAsFirstSibling();
//myGameObject3.transform.SetAsFirstSibling();
//SetAsLastSibling是在编辑器里往下移动层级。显示在画面的前面。
//myGameObject.transform.SetAsLastSibling();
//myGameObject2.transform.SetAsLastSibling();
//myGameObject3.transform.SetAsLastSibling();
//SetSiblingIndex里的数字越大,是在编辑器里越往下。显示在画面的前面。
myGameObject.transform.SetSiblingIndex(0);
myGameObject2.transform.SetSiblingIndex(1);
myGameObject3.transform.SetSiblingIndex(2);
}
public void FindMethod()
{
panel = GameObject.Find("Panel");
panel2 = GameObject.Find("Panel2");
myGameObject = Instantiate(myGameObject, panel.transform);
myGameObject.transform.SetParent(panel2.transform);
myGameObject2 = Instantiate(myGameObject2, panel.transform);
myGameObject2.transform.SetParent(panel2.transform);
myGameObject3 = Instantiate(myGameObject3, panel.transform);
myGameObject3.transform.SetParent(panel2.transform);
}
public void FindMethod2()
{
//假设多个控件名字都是Panel,从上到下寻找名字为Panel的控件,获取的是名字是Panle最上面的控件,下面的控件就忽略掉了。
panel = GameObject.Find("Panel");
myGameObject = Instantiate(myGameObject, panel.transform);
myGameObject2 = Instantiate(myGameObject2, panel.transform);
myGameObject3 = Instantiate(myGameObject3, panel.transform);
}
public void FindWithTagMethod()
{
panel = GameObject.FindGameObjectWithTag("TestPanel");
panel2 = GameObject.FindGameObjectWithTag("TestPanel2");
myGameObject = Instantiate(myGameObject, panel.transform);
myGameObject.transform.SetParent(panel2.transform);
myGameObject2 = Instantiate(myGameObject2, panel.transform);
myGameObject2.transform.SetParent(panel2.transform);
myGameObject3 = Instantiate(myGameObject3, panel.transform);
myGameObject3.transform.SetParent(panel2.transform);
}
public void FindWithTagMethod2()
{
//假设多个控件设的tag都是TestPanel,从上到下寻找tag为TestPanel的控件,获取的是tag为TestPanel最上面的控件,下面的控件就忽略掉了。
panel = GameObject.FindGameObjectWithTag("TestPanel");
myGameObject = Instantiate(myGameObject, panel.transform);
myGameObject2 = Instantiate(myGameObject2, panel.transform);
myGameObject3 = Instantiate(myGameObject3, panel.transform);
}
public void FindWithTagsMethod()
{
panel3 = GameObject.FindGameObjectsWithTag("TestPanel");
panel4 = GameObject.FindGameObjectsWithTag("TestPanel2");
myGameObject = Instantiate(myGameObject, panel3[0].transform);
myGameObject.transform.SetParent(panel4[0].transform);
myGameObject2 = Instantiate(myGameObject2, panel3[0].transform);
myGameObject2.transform.SetParent(panel4[0].transform);
myGameObject3 = Instantiate(myGameObject3, panel3[0].transform);
myGameObject3.transform.SetParent(panel4[0].transform);
}
public void FindWithTagsMethod2()
{
//假设多个控件设的tag都是TestPanel,从上到下寻找tag为TestPanel的控件,数组为0是最上面,数组里的值越大,寻找到的是下面层级的控件。
panel3 = GameObject.FindGameObjectsWithTag("TestPanel");
myGameObject = Instantiate(myGameObject, panel3[1].transform);
myGameObject2 = Instantiate(myGameObject2, panel3[1].transform);
myGameObject3 = Instantiate(myGameObject3, panel3[1].transform);
}
}
边栏推荐
- UVM RAL model and built-in seq
- Go1.18 upgrade function - Fuzz test from scratch in Go language
- [Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
- NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
- Teach you how to deploy Nestjs projects
- Performance optimization: remember a tree search interface optimization idea
- 【论文精读】iNeRF
- Getting Started with Tkinter
- Collation of knowledge points in Ningbo University NBU IT project management final exam
- MATLAB program design and application 2.4 Common internal functions of MATLAB
猜你喜欢
Arduino框架下STM32全系列开发固件安装指南
useragent online lookup
Go1.18 upgrade function - Fuzz test from scratch in Go language
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
Teach you how to deploy Nestjs projects
高效并发:Synchornized的锁优化详解
[Intensive reading of the paper] iNeRF
The principle of ReentrantLock (to be continued)
Bionic caterpillar robot source code
第七章
随机推荐
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
A shortcut to search for specific character content in idea
SiC MOSFET的短路特性及保护
Write a database document management tool based on WPF repeating the wheel (1)
基于STM32 环形队列来实现串口接收数据
Socket回顾与I/0模型
Linux environment redis cluster to build "recommended collection"
统计UTF-8字符串中的字符函数
【论文精读】iNeRF
Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization
UVM RAL model and built-in seq
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
Judging decimal points and rounding of decimal operations in Golang
Bika LIMS open source LIMS set - use of SENAITE (detection process)
Istio introduction
Apache EventMesh distributed event-driven multi-runtime
Basic Grammar Introduction of Carbon Tutorial (Tutorial)
老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
财务盈利、偿债能力指标
linux view redis version command (linux view mysql version number)