GetComponentsInChildren与GetComponentInChildren区别

GetComponentsInChildren:

获取对应组件,返回对应组件的数组(多个

范围:从自己开始直到子子孙孙

注意:与组件激活、失活无关,失活的游戏对象无法作为查找对象

GetComponentInChildren

获取对应组件,按层级返回最先包含的对应组件(单一

范围:从自己开始直到子子孙孙

注意:与组件激活、失活无关,失活的游戏对象无法作为查找对象

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是修改后的代码: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class DottedLine : MonoBehaviour { public List<GameObject> gameObjects; public List<string> ids; // 存储所有密码头的id public bool showBerthingArea = true; // 是否显示停靠区域 public bool showNotifyArea = true; // 是否显示通知区域 public string currentId = ""; // 当前选择的密码头id private void Start() { Transform[] transforms = GetComponentsInChildren<Transform>(); foreach (Transform tf in transforms) { GameObject go = tf.gameObject; if (go != gameObject) { gameObjects.Add(go); } } } // 根据id和显示选项,更新高亮状态 private void UpdateHighlight(string id, bool showBerthing, bool showNotify) { foreach (GameObject go in gameObjects) { if (id == "" || go.name.Contains(id)) { LineRenderer line = go.GetComponent<LineRenderer>(); if (line != null) { bool show = false; if (go.name.Contains("BerthingArea")) { show = showBerthing; } else if (go.name.Contains("NotifyArea")) { show = showNotify; } line.enabled = show; } } } } // 显示指定id的高亮 public void ShowById(string id) { currentId = id; UpdateHighlight(id, showBerthingArea, showNotifyArea); } // 显示所有id的高亮 public void ShowAll() { currentId = ""; UpdateHighlight("", showBerthingArea, showNotifyArea); } // 隐藏所有id的高亮 public void HideAll() { currentId = ""; UpdateHighlight("", false, false); } // 显示或隐藏停靠区域 public void ToggleBerthingArea() { showBerthingArea = !showBerthingArea; UpdateHighlight(currentId, showBerthingArea, showNotifyArea); } // 显示或隐藏通知区域 public void ToggleNotifyArea() { showNotifyArea = !showNotifyArea; UpdateHighlight(currentId, showBerthingArea, showNotifyArea); } } ``` 在这个脚本中,我们添加了一些新的变量和方法来实现新的需求: - `ids`:存储所有密码头的id,可以在Inspector中进行设置。 - `showBerthingArea`和`showNotifyArea`:控制是否显示停靠区域和通知区域。 - `currentId`:保存当前选择的密码头id。 - `UpdateHighlight`方法:根据id和显示选项,更新高亮状态。 - `ShowById`方法:显示指定id的高亮。 - `ShowAll`方法:显示所有id的高亮。 - `HideAll`方法:隐藏所有id的高亮。 - `ToggleBerthingArea`和`ToggleNotifyArea`方法:显示或隐藏停靠区域和通知区域。 在Inspector中,可以添加所有密码头的id和需要高亮的LineRenderer: ![Inspector界面](https://img-blog.csdnimg.cn/20210723182136275.png) 接下来,可以在代码中调用这些方法来实现不同的显示效果: ```csharp public class Example : MonoBehaviour { public DottedLine dottedLine; void Start() { // 显示指定id的停靠区域和通知区域 dottedLine.ShowById("001"); dottedLine.showBerthingArea = true; dottedLine.showNotifyArea = true; // 显示所有id的通知区域 dottedLine.ShowAll(); dottedLine.showBerthingArea = false; dottedLine.showNotifyArea = true; // 隐藏所有id的高亮 dottedLine.HideAll(); } } ``` 这样就可以根据需求控制每个密码头的高亮了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值