Unity简单树状结构
樹狀結構。
預制體
TreeView掛載TreeView腳本。Game是空物體。ShowItem是一張圖片添加了Toggle組件,剩下的UGUI的Toggle物體。
TreeViewControl控制節點的創建、信息的顯示、坐標位置設置以及節點的父子關系。
TreeViewControl掛載在一個空物體上,為了顯示我們給空物體掛載一個布局腳本Vertical?Group?Layout。
每一個節點都帶有TreeView腳本。
TreeView腳本負責顯示和隱藏子物體。
隱藏/顯示子物體
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);
?? ??? ?}
?? ?}
}
本篇其實沒有用到UGUI建立的Toggle物體。該功能請自行研發。
以下為源碼:
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("教學樓1號樓", true);
? ? }
? ? /// <summary>
? ? /// 初始化樹狀結構
? ? /// </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號", "滅火器");
? ? ? ? GameObject room301_two = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room301_two, room301);
? ? ? ? room301_two.GetComponent<TreeView>().Init("滅火器2號", "滅火器");
? ? ? ? GameObject room301_three = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room301_three, room301);
? ? ? ? room301_three.GetComponent<TreeView>().Init("監控北三號", "高空瞭望攝像頭");
? ? ? ? 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號", "滅火器");
? ? ? ? GameObject room302_two = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room302_two, room302);
? ? ? ? room302_two.GetComponent<TreeView>().Init("滅火器2號", "滅火器");
? ? ? ? GameObject room302_three = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room302_three, room302);
? ? ? ? room302_three.GetComponent<TreeView>().Init("監控北三號", "高空瞭望攝像頭");
? ? ? ? GameObject room302_four = CreateTreeView(this.gameObject);
? ? ? ? SetParent(room302_four, room302);
? ? ? ? room302_four.GetComponent<TreeView>().Init("監控北三號", "高空瞭望攝像頭");
? ? }
}
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)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //判斷物體是否處于顯示狀態
? ? ? ? ? ? ? ? if (item.activeInHierarchy)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? item.SetActive(false);
? ? ? ? ? ? ? ? ? ? //將隱藏起來Toggle變為未選狀態。因為再次打開是應為未選狀態。
? ? ? ? ? ? ? ? ? ? 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);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?
總結
以上是生活随笔為你收集整理的Unity简单树状结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue 前端框架
- 下一篇: html模板原型,再也不用四处找原型模版