当前位置:网站首页>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);
}
}
边栏推荐
- AI automatic code writing plugin Copilot (co-pilot)
- How to change npm to Taobao mirror [easy to understand]
- Architect 04 - Application Service Encryption Design and Practice
- 角色妆容的实现
- leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
- -xms -xmx(information value)
- Short-circuit characteristics and protection of SiC MOSFETs
- 老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
- 21. Support Vector Machine - Introduction to Kernel Functions
- 【Yugong Series】July 2022 Go Teaching Course 025-Recursive Function
猜你喜欢

21. Support Vector Machine - Introduction to Kernel Functions

Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios

Realization of character makeup

Short-circuit characteristics and protection of SiC MOSFETs

Architecture Battalion Module 8 Homework

Summary of the classic drawing method of histogram

ReentrantLock原理(未完待续)

IDA PRO中汇编结构体识别

flowable workflow all business concepts

Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization
随机推荐
GAC Honda Safety Experience Camp: "Danger" is the best teacher
BM5 merge k sorted linked lists
How to change npm to Taobao mirror [easy to understand]
Financial profitability and solvency indicators
Write a database document management tool based on WPF repeating the wheel (1)
【愚公系列】2022年07月 Go教学课程 025-递归函数
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
20. Support vector machine - knowledge of mathematical principles
Golang must know the Go Mod command
IDA PRO中汇编结构体识别
ReentrantLock原理(未完待续)
MATLAB program design and application 2.4 Common internal functions of MATLAB
linux view redis version command (linux view mysql version number)
Implementing a Simple Framework for Managing Object Information Using Reflection
Getting Started with Tkinter
renderjs usage in uni-app
flowable workflow all business concepts
【AcWing】The 62nd Weekly Match 【2022.07.30】
SiC MOSFET的短路特性及保护
Transfer Learning - Domain Adaptation