当前位置:网站首页>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);
}
}
边栏推荐
- A few permanent free network transmission, convenient and simple (Intranet through tutorials)
- Commonly used security penetration testing tools (penetration testing tools)
- GateWay implements load balancing
- Structure of the actual combat battalion module eight operations
- 21. Support Vector Machine - Introduction to Kernel Functions
- 利用反射实现一个管理对象信息的简单框架
- C language parsing json string (json object is converted to string)
- BM3 flips the nodes in the linked list in groups of k
- Short-circuit characteristics and protection of SiC MOSFETs
- c语言解析json字符串(json对象转化为字符串)
猜你喜欢

Made with Flutter and Firebase!counter application

Teach you how to deploy Nestjs projects

【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用

Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization

C程序设计-方法与实践(清华大学出版社)习题解析

Apache EventMesh distributed event-driven multi-runtime

Socket Review and I/0 Model

idea中搜索具体的字符内容的快捷方式

What's wrong with the sql syntax in my sql

20. Support vector machine - knowledge of mathematical principles
随机推荐
Niuke.com brush questions (1)
In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
Transfer Learning - Domain Adaptation
uni-app中的renderjs使用
Audio alignment using cross-correlation
STM32 full series development firmware installation guide under Arduino framework
npm 更改为淘宝镜像的方法[通俗易懂]
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
A few permanent free network transmission, convenient and simple (Intranet through tutorials)
【AcWing】The 62nd Weekly Match 【2022.07.30】
ojdbc8 "Recommended Collection"How to change npm to Taobao mirror [easy to understand]
Judging decimal points and rounding of decimal operations in Golang
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
SiC MOSFET的短路特性及保护
第六章
财务盈利、偿债能力指标
Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic
c语言解析json字符串(json对象转化为字符串)