当前位置:网站首页>Unity - by casting and cloning method dynamic control under various UGUI create and display
Unity - by casting and cloning method dynamic control under various UGUI create and display
2022-07-31 22:10:00 【Tiantian up UT】
Find,FindGameObjectWithTag,FindGameObjectsWithTag三种方式,推荐FindGameObjectsWithTag方式,性能最优.其次是FindGameObjectWithTag,最后是Find
推荐FindGameObjectsWithTagway returnedGameObject数组.FindGameObjectWithTag和Find返回的是GameObject.
FindGameObjectsWithTag和FindGameObjectsWithTagobtained in the following wayTag.
实现的功能:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//Find,FindGameObjectWithTag,FindGameObjectsWithTag总结
//Find,FindGameObjectWithTag,FindGameObjectsWithTag三种方式,推荐FindGameObjectsWithTag方式,性能最优.其次是FindGameObjectWithTag,最后是Find
//推荐FindGameObjectsWithTagway returnedGameObject数组.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();
//SetAsFirstSiblingis to move up the hierarchy in the editor.Displayed in the back of the picture.
//myGameObject.transform.SetAsFirstSibling();
//myGameObject2.transform.SetAsFirstSibling();
//myGameObject3.transform.SetAsFirstSibling();
//SetAsLastSiblingis to move down the hierarchy in the editor.displayed on the front of the screen.
//myGameObject.transform.SetAsLastSibling();
//myGameObject2.transform.SetAsLastSibling();
//myGameObject3.transform.SetAsLastSibling();
//SetSiblingIndexthe bigger the number,is the further down in the editor.displayed on the front of the screen.
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()
{
//Suppose multiple control names arePanel,Look for names from top to bottom asPanel的控件,get the name isPanleAt the top of the control,The following controls are ignored.
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()
{
//Assuming that multiple controls are settag都是TestPanel,Find from top to bottomtag为TestPanel的控件,获取的是tag为TestPanelAt the top of the control,The following controls are ignored.
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()
{
//Assuming that multiple controls are settag都是TestPanel,Find from top to bottomtag为TestPanel的控件,数组为0是最上面,The larger the value in the array,What is found is the control at the following level.
panel3 = GameObject.FindGameObjectsWithTag("TestPanel");
myGameObject = Instantiate(myGameObject, panel3[1].transform);
myGameObject2 = Instantiate(myGameObject2, panel3[1].transform);
myGameObject3 = Instantiate(myGameObject3, panel3[1].transform);
}
}
边栏推荐
- Pytest初体验
- Linux environment redis cluster to build "recommended collection"
- How to identify fake reptiles?
- Implementation of a sequence table
- c语言解析json字符串(json对象转化为字符串)
- 全网一触即发,自媒体人的内容分发全能助手——融媒宝
- Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
- leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
- NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
- What's wrong with the sql syntax in my sql
猜你喜欢
How to identify fake reptiles?
Short-circuit characteristics and protection of SiC MOSFETs
Implementing a Simple Framework for Managing Object Information Using Reflection
Pytest初体验
Chapter VII
Collation of knowledge points in Ningbo University NBU IT project management final exam
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
Socket回顾与I/0模型
全网一触即发,自媒体人的内容分发全能助手——融媒宝
关注!海泰方圆加入《个人信息保护自律公约》
随机推荐
Count characters in UTF-8 string function
【AcWing】The 62nd Weekly Match 【2022.07.30】
Unity-LineRenderer显示一条线
角色妆容的实现
The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
Implementing a Simple Framework for Managing Object Information Using Reflection
关注!海泰方圆加入《个人信息保护自律公约》
JS basic exercises
AI automatic code writing plugin Copilot (co-pilot)
Golang - from entry to abandonment
「APIO2010」巡逻 题解
Several methods for deleting specified elements in Golang slices
二叉树非递归遍历
Thymeleaf是什么?该如何使用。
server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none failed
Arduino框架下STM32全系列开发固件安装指南
Short-circuit characteristics and protection of SiC MOSFETs
Istio introduction
spark reports an error OutOfMemory "recommended collection"
PCB叠层设计