当前位置:网站首页>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);
}
}
边栏推荐
- 「SDOI2016」征途 题解
- Basic configuration of OSPFv3
- 高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
- 嵌入式开发没有激情了,正常吗?
- 基于STM32 环形队列来实现串口接收数据
- grep command written test questions
- 【Yugong Series】July 2022 Go Teaching Course 025-Recursive Function
- Write a database document management tool based on WPF repeating the wheel (1)
- Short-circuit characteristics and protection of SiC MOSFETs
- A solution to the server encountered an internal error that prevented it from fulfilling this request [easy to understand]
猜你喜欢

The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days

Pytest初体验

NVIDIA已经开始测试AD106和AD107 GPU核心的显卡产品

SiC MOSFET的短路特性及保护

Basic configuration of OSPFv3

Chapter Six

Architect 04 - Application Service Encryption Design and Practice

角色妆容的实现

PCB stackup design

架构实战营模块八作业
随机推荐
sqlite3 simple operation
How to get useragent
统计UTF-8字符串中的字符函数
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
The old music player WinAmp released version 5.9 RC1: migrated to VS 2019, completely rebuilt, compatible with Win11
Structure of the actual combat battalion module eight operations
Chapter Six
useragent online lookup
renderjs usage in uni-app
Istio introduction
What's wrong with the sql syntax in my sql
顺序表的实现
求n以内的素数
Golang - from entry to abandonment
STM32 full series development firmware installation guide under Arduino framework
角色妆容的实现
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
-xms -xmx(information value)
Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization
Audio alignment using cross-correlation