当前位置:网站首页>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);
}
}
边栏推荐
- [Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
- Niuke.com brush questions (1)
- 返回一个零长度的数组或者空的集合,不要返回null
- C language parsing json string (json object is converted to string)
- Transfer Learning - Domain Adaptation
- sqlite3简单操作
- LevelSequence source code analysis
- Architect 04 - Application Service Encryption Design and Practice
- 【愚公系列】2022年07月 Go教学课程 025-递归函数
- 【Yugong Series】July 2022 Go Teaching Course 025-Recursive Function
猜你喜欢
![[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement

Recognize anomalies (you will understand after reading this)

Socket回顾与I/0模型

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

Apache EventMesh distributed event-driven multi-runtime

架构实战营模块八作业

利用反射实现一个管理对象信息的简单框架

第七章

Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query

AI 自动写代码插件 Copilot(副驾驶员)
随机推荐
The article you worked so hard to write may not be your original
grep command written test questions
【AcWing】The 62nd Weekly Match 【2022.07.30】
Apache EventMesh distributed event-driven multi-runtime
Summary of the classic drawing method of histogram
Golang must know the Go Mod command
NVIDIA已经开始测试AD106和AD107 GPU核心的显卡产品
How to identify fake reptiles?
LevelSequence source code analysis
Go mode tidy reports an error go warning “all” matched no packages
架构实战营模块 8 作业
Socket Review and I/0 Model
Architecture Battalion Module 8 Homework
flowable workflow all business concepts
Bika LIMS open source LIMS set - use of SENAITE (detection process)
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
高效并发:Synchornized的锁优化详解
Daily practice——Randomly generate an integer between 1-100 and see how many times you can guess.Requirements: The number of guesses cannot exceed 7 times, and after each guess, it will prompt "bigger"
leetcode 665. Non-decreasing Array
What is Thymeleaf?How to use.