public void UpdateHeightLight(string id, bool berthingArea, bool notifyArea) { Debug.LogError(gameObjects.Count); foreach (GameObject go in gameObjects) { Debug.Log($"go: {go}"); if (id == "" || go.name.Contains(id)) { LineRenderer line = go.GetComponent<LineRenderer>(); Debug.Log($"id1233333: {id}"); if (line != null) { bool show = false; if (go.name.Contains("BerthingArea")) { show = berthingArea; Debug.Log($"berthingArea123: {berthingArea}"); } else if (go.name.Contains("NotifyArea")) { show = notifyArea; Debug.Log($"notifyArea123: {notifyArea}"); } line.TrySetActive(show); } } }第二个if语句没走进去,不知道是什么原因
时间: 2024-02-10 10:10:31 浏览: 133
根据给出的代码,第二个if语句指的是:
```
else if (go.name.Contains("NotifyArea"))
{
show = notifyArea;
Debug.Log($"notifyArea123: {notifyArea}");
}
```
因此,如果该if语句没有被执行,可能有以下几种原因:
1. `gameObjects`中没有包含`NotifyArea`的游戏对象。
2. `notifyArea`参数传入的值为`false`,导致`show`变量的值也为`false`,因此`line.TrySetActive(show)`方法没有将`LineRenderer`组件激活。
3. `go.name.Contains("NotifyArea")`条件判断出现问题,可以在代码中添加`Debug.Log(go.name)`语句,查看具体游戏对象的名称是否包含`NotifyArea`。
相关问题
public class DottedLine : MonoBehaviour { public List<GameObject> gameObjects; public List<string> wharfId; public bool showBerthingArea = true; public bool showNotifyArea = true; public string currentId = null; void Start() { Transform transforms = transform.GetComponentInChildren<Transform>(); foreach (Transform tf in transforms) { gameObjects.Add(tf.gameObject); } gameObjects.Remove(gameObject); } public void UpdateHeightLight(string id, bool berthingArea, bool notifyArea) { foreach (GameObject go in gameObjects) { if (id == null || go.name.Contains(id)) { LineRenderer line = go.GetComponent<LineRenderer>(); if (line != null) { bool show = false; if (go.name.Contains("BerthingArea")) { show = berthingArea; } else if (go.name.Contains("NotifyArea")) { show = notifyArea; } line.enabled = show; } } } } public void ShowAssignId(string id) { currentId = id; Debug.LogError($"current:{ currentId}"); UpdateHeightLight(id, showBerthingArea, showNotifyArea); } public void ShowAllHeightLight() { currentId = null; UpdateHeightLight(null, true, true); } public void HideAllHeightLight() { currentId = null; UpdateHeightLight(null, false, false); } public void ToggleBerthingArea() { showBerthingArea = !showBerthingArea; UpdateHeightLight(currentId, showBerthingArea, showNotifyArea); } public void ToggleNotifyArea() { showNotifyArea = !showNotifyArea; UpdateHeightLight(currentId, showNotifyArea, showNotifyArea); } 和public class LineRendererController : MonoBehaviour { public DottedLine dottedLine; void Start() { GameObject dock = GameObject.Find("604"); if (dock != null) { // 将游戏对象的名称作为id传递给dottedLine.ShowAssignId()方法 dottedLine.ShowAssignId(dock.name); // 显示该游戏对象的停靠区 dock.GetComponent<DottedLine>().showBerthingArea = true; // 隐藏该游戏对象的通知区 dock.GetComponent<DottedLine>().showNotifyArea = false; }这两段代码要想实现,unity中该怎么操作
这两段代码是 Unity 中的 C# 脚本,需要将它们添加到 Unity 的 GameObject 上,然后通过在代码中调用 GameObject 的属性和方法来操作它们。具体步骤如下:
1. 创建一个空的 GameObject,给它一个有意义的名称,例如 "DottedLineController" 或 "LineRendererController"。
2. 将 DottedLine 脚本和 LineRendererController 脚本添加到 GameObject 上,可以通过拖拽脚本文件到 Inspector 视图中的 Add Component 按钮来完成。
3. 在场景中找到需要操作的 GameObject,例如上面代码中的 "604"。
4. 将该 GameObject 上的 DottedLine 组件的 showBerthingArea 和 showNotifyArea 属性设置为需要的值,可以在 Inspector 视图中找到这两个属性。
5. 在 LineRendererController 脚本中,通过 GameObject.Find() 方法找到需要操作的 GameObject,并将它的名称传递给 dottedLine.ShowAssignId() 方法。
6. 在 Unity 编辑器中运行场景,可以通过调用 DottedLine 和 LineRendererController 脚本中的方法来显示、隐藏、切换停靠区和通知区。
public class DottedLine : MonoBehaviour { /// <summary> /// 存放虚线中的每个线段 /// </summary> public List<GameObject> gameObjects; /// <summary> /// 存放每个码头的名字 /// </summary> public List<string> wharfId; public bool showBerthingArea = true; public bool showNotifyArea = true; public string currentId=""; /// <summary> /// 将每个线段添加到List /// </summary> void Start() { Transform[] transforms = GetComponentsInChildren<Transform>();//获取当前游戏对象及其所有子对象上的Transform组件,并将它们存储在一个数据组中。 foreach (Transform tf in transforms) { GameObject go = tf.gameObject; if (go != gameObject)//将所有子对象的GameObject对象存储到一个列表中,并排除了当前游戏对对象本身。也就是说,如果go指向的是当前游戏对象本身,那么它不会被添加到gameObjects列表中。 { gameObjects.Add(go); } }public void UpdateHeightLight(string id, bool berthingArea=true, bool notifyArea = false) { 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 = berthingArea; } else if (go.name.Contains("NotifyArea")) { show = notifyArea; } line.TrySetActive(show); } } } } public void ShowAssignId(string id) { currentId = id; Debug.LogError($"current:{ currentId}"); UpdateHeightLight(id, showBerthingArea, showNotifyArea); }
这段代码定义了一个名为 DottedLine 的类,该类用于控制虚线的显示。这个类包含了一个名为 gameObjects 的公共 List,用于存放虚线中的每个线段的 GameObject 对象。此外,该类还包含了一个名为 wharfId 的公共 List,用于存放每个码头的名字。这个类还包含了一个名为 UpdateHeightLight 的公共方法,用于根据传入的 id 参数来更新虚线的高亮显示状态。该类还包含了一个名为 ShowAssignId 的公共方法,用于将当前的 id 值设置为传入的 id 参数,并更新虚线的高亮显示状态。
阅读全文