日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity简单树状结构

發(fā)布時(shí)間:2023/12/29 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity简单树状结构 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

樹狀結(jié)構(gòu)。

預(yù)制體

TreeView掛載TreeView腳本。Game是空物體。ShowItem是一張圖片添加了Toggle組件,剩下的UGUI的Toggle物體。

TreeViewControl控制節(jié)點(diǎn)的創(chuàng)建、信息的顯示、坐標(biāo)位置設(shè)置以及節(jié)點(diǎn)的父子關(guān)系。

TreeViewControl掛載在一個(gè)空物體上,為了顯示我們給空物體掛載一個(gè)布局腳本Vertical?Group?Layout。

每一個(gè)節(jié)點(diǎn)都帶有TreeView腳本。
TreeView腳本負(fù)責(zé)顯示和隱藏子物體。

隱藏/顯示子物體
public void ShowChild(bool isOn)
{
?? ?if (isOn)
?? ?{
?? ??? ?GameObject.Find("ShowItem").transform.localRotation = Quaternion.Euler(0, 0, 0);
?? ??? ?foreach (var item in child)
?? ??? ?{
?? ??? ??? ?item.SetActive(true);
?? ??? ?}
?? ?}
?? ?else
?? ?{
?? ??? ?
?? ??? ?GameObject.Find("ShowItem").transform.localRotation = Quaternion.Euler(0, 0, 180);

?? ??? ?foreach (var item in child)
?? ??? ?{
?? ??? ??? ?//如果
?? ??? ??? ?if (item.activeInHierarchy)
?? ??? ??? ?{
?? ??? ??? ??? ?item.SetActive(false);
?? ??? ??? ??? ?item.transform.GetChild(0).GetChild(0).GetComponent<Toggle>().isOn = false;
?? ??? ??? ??? ?HideChild(item);
?? ??? ??? ?}
?? ??? ?}
?? ?}
}

遞歸隱藏,孫物體以及更多
private void HideChild(GameObject obj)
{
?? ?foreach (var item in obj.GetComponent<TreeView>().child)
?? ?{
?? ??? ?if (item.activeInHierarchy)
?? ??? ?{
?? ??? ??? ?item.SetActive(false);
?? ??? ??? ?item.transform.GetChild(0).GetChild(0).GetComponent<Toggle>().isOn = false;
?? ??? ??? ?HideChild(item);
?? ??? ?}
?? ?}
}

本篇其實(shí)沒有用到UGUI建立的Toggle物體。該功能請(qǐng)自行研發(fā)。

以下為源碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TreeViewControl : MonoBehaviour
{
? ? GameObject root;
? ? void Start()
? ? {
? ? ? ? InitTreeRoot();
? ? ? ? InitTreeView();
? ? }
? ? public GameObject CreateTreeView(GameObject parent)
? ? {
? ? ? ? GameObject treeView = Resources.Load("TreeView") as GameObject;
? ? ? ? treeView = Instantiate(treeView, parent.transform);
? ? ? ? return treeView;
? ? }
? ? public virtual void SetParent(GameObject child, GameObject parent)
? ? {
? ? ? ? child.transform.GetChild(0).localPosition = parent.transform.GetChild(0).localPosition + new Vector3(11, 0, 0);

? ? ? ? child.GetComponent<TreeView>().SetParent(parent);
? ? ? ? parent.GetComponent<TreeView>().SetChild(child);
? ? }

? ? public void InitTreeRoot()
? ? {
? ? ? ? root = CreateTreeView(this.gameObject);
? ? ? ? root.GetComponent<TreeView>().Init("教學(xué)樓1號(hào)樓", true);
? ? }
? ? /// <summary>
? ? /// 初始化樹狀結(jié)構(gòu)
? ? /// </summary>
? ? private void InitTreeView()
? ? {
? ? ? ? GameObject floorOne = CreateTreeView(this.gameObject);
? ? ? ? SetParent(floorOne, root);
? ? ? ? floorOne.GetComponent<TreeView>().Init("一樓", false);

? ? ? ? GameObject floorTwo = CreateTreeView(this.gameObject);
? ? ? ? SetParent(floorTwo, root);
? ? ? ? floorTwo.GetComponent<TreeView>().Init("二樓", false);

? ? ? ? GameObject floorThree = CreateTreeView(this.gameObject);
? ? ? ? SetParent(floorThree, root);
? ? ? ? floorThree.GetComponent<TreeView>().Init("三樓", true);

? ? ? ? GameObject room301 = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room301, floorThree);
? ? ? ? room301.GetComponent<TreeView>().Init("301房間", true);

? ? ? ? GameObject room301_one = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room301_one, room301);
? ? ? ? room301_one.GetComponent<TreeView>().Init("滅火器1號(hào)", "滅火器");

? ? ? ? GameObject room301_two = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room301_two, room301);
? ? ? ? room301_two.GetComponent<TreeView>().Init("滅火器2號(hào)", "滅火器");

? ? ? ? GameObject room301_three = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room301_three, room301);
? ? ? ? room301_three.GetComponent<TreeView>().Init("監(jiān)控北三號(hào)", "高空瞭望攝像頭");

? ? ? ? GameObject room302 = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room302, floorThree);
? ? ? ? room302.GetComponent<TreeView>().Init("302房間", true);

? ? ? ? GameObject room302_one = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room302_one, room302);
? ? ? ? room302_one.GetComponent<TreeView>().Init("滅火器1號(hào)", "滅火器");

? ? ? ? GameObject room302_two = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room302_two, room302);
? ? ? ? room302_two.GetComponent<TreeView>().Init("滅火器2號(hào)", "滅火器");

? ? ? ? GameObject room302_three = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room302_three, room302);
? ? ? ? room302_three.GetComponent<TreeView>().Init("監(jiān)控北三號(hào)", "高空瞭望攝像頭");

? ? ? ? GameObject room302_four = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room302_four, room302);
? ? ? ? room302_four.GetComponent<TreeView>().Init("監(jiān)控北三號(hào)", "高空瞭望攝像頭");
? ? }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TreeView : MonoBehaviour
{
? ? public GameObject _parent = null;
? ? public List<GameObject> child = new List<GameObject>();
? ? GameObject showItem;
? ? void Start()
? ? {
? ? ? ? showItem = this.transform.GetChild(0).transform.Find("ShowItem").gameObject;
? ? ? ? showItem.GetComponent<Toggle>().onValueChanged.AddListener((bool isOn) => {
? ? ? ? ? ? ShowChild(isOn);
? ? ? ? });
? ? }
? ? public void SetParent(GameObject parent)
? ? {
? ? ? ? _parent = parent;
? ? }
? ? public void SetChild(GameObject obj)
? ? {
? ? ? ? child.Add(obj);
? ? }

? ? public void Init(string msg, bool toggle)
? ? {
? ? ? ? showItem = this.transform.GetChild(0).transform.Find("ShowItem").gameObject;
? ? ? ? this.transform.GetChild(0).Find("ItemText").GetComponent<Text>().text = msg;
? ? ? ? showItem.GetComponent<Toggle>().isOn = toggle;
? ? ? ? ShowChild(toggle);
? ? }
? ? public void Init(string name, string type)
? ? {
? ? ? ? showItem = this.transform.GetChild(0).transform.Find("ShowItem").gameObject;
? ? ? ? showItem.gameObject.SetActive(false);
? ? ? ? this.transform.GetChild(0).Find("ItemText").GetComponent<Text>().text = name;
? ? ? ??
? ? }
? ? //隱藏/顯示子物體
? ? public void ShowChild(bool isOn)
? ? {
? ? ? ? if (isOn)
? ? ? ? {
? ? ? ? ? ? showItem.transform.localRotation = Quaternion.Euler(0, 0, 0);
? ? ? ? ? ? foreach (var item in child)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? item.SetActive(true);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? showItem.transform.localRotation = Quaternion.Euler(0, 0, 180);

? ? ? ? ? ? foreach (var item in child)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //判斷物體是否處于顯示狀態(tài)
? ? ? ? ? ? ? ? if (item.activeInHierarchy)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? item.SetActive(false);
? ? ? ? ? ? ? ? ? ? //將隱藏起來Toggle變?yōu)槲催x狀態(tài)。因?yàn)樵俅未蜷_是應(yīng)為未選狀態(tài)。
? ? ? ? ? ? ? ? ? ? item.transform.GetChild(0).GetComponent<Toggle>().isOn = false;
? ? ? ? ? ? ? ? ? ? HideChild(item);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? //遞歸隱藏孫物體以及更多
? ? private void HideChild(GameObject obj)
? ? {
? ? ? ? foreach (var item in obj.GetComponent<TreeView>().child)
? ? ? ? {
? ? ? ? ? ? if (item.activeInHierarchy)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? item.SetActive(false);
? ? ? ? ? ? ? ? item.transform.GetChild(0).GetComponent<Toggle>().isOn = false;
? ? ? ? ? ? ? ? HideChild(item);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?

總結(jié)

以上是生活随笔為你收集整理的Unity简单树状结构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。