当前位置:网站首页>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);
}
}
边栏推荐
- Implementation of a sequence table
- Golang - from entry to abandonment
- 财务盈利、偿债能力指标
- Transfer Learning - Domain Adaptation
- 20. Support vector machine - knowledge of mathematical principles
- Niuke.com brush questions (1)
- Commonly used security penetration testing tools (penetration testing tools)
- PCB stackup design
- Summary of the classic drawing method of histogram
- Implementing a Simple Framework for Managing Object Information Using Reflection
猜你喜欢
Implementation of a sequence table
MATLAB program design and application 2.4 Common internal functions of MATLAB
一款国外开发的高质量WordPress下载站模板主题
Chapter VII
关注!海泰方圆加入《个人信息保护自律公约》
Architecture Battalion Module 8 Homework
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
Introduction to Audio Types and Encoding Formats in Unity
请问我的这段sql中sql语法哪里出了错
Shell 脚本 快速入门到实战 -02
随机推荐
ThreadLocal
高效并发:Synchornized的锁优化详解
A few permanent free network transmission, convenient and simple (Intranet through tutorials)
In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved
Returns a zero-length array or empty collection, do not return null
UserAgent resolution
PCB叠层设计
Batch (batch size, full batch, mini batch, online learning), iterations and epochs in deep learning
请问我的这段sql中sql语法哪里出了错
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
Basic configuration of OSPFv3
C程序设计-方法与实践(清华大学出版社)习题解析
SiC MOSFET的短路特性及保护
One thing to say, is outsourcing company worth it?
Bika LIMS open source LIMS set - use of SENAITE (detection process)
嵌入式开发没有激情了,正常吗?
Arduino框架下STM32全系列开发固件安装指南
Made with Flutter and Firebase!counter application
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
IDA PRO中汇编结构体识别