首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Unity中将编号添加到对象?使用这个简单的技巧!

在节点All_PathPoint_Parent下有多个子节点对象, 在unity编辑器模式中, 为每个子节点对象显示一个编号, 从1开始.

可以使用Editor GUI Utility在编辑器窗口中为对象绘制GUI。在All_PathPoint_Parent节点的子对象上循环,为每个子对象绘制GUI,显示其编号。 下面是一个示例代码片段:

void OnSceneGUI()

{

GameObject allPathPointParent = GameObject.Find("All_PathPoint_Parent");

if (allPathPointParent != null)

{

Transform[] pathPoints = allPathPointParent.GetComponentsInChildren();

int count = 0;

foreach (Transform pathPoint in pathPoints)

{

if (pathPoint == allPathPointParent.transform)

continue;

count++;

Handles.Label(pathPoint.position, count.ToString());

}

}

}

这段代码可以放在Unity脚本(.cs)文件中。您可以将其添加到项目中的任何脚本文件中,只要它继承自MonoBehaviour或EditorWindow类,并确保脚本附加到场景中的对象上或打开Unity编辑器窗口。然后,将此脚本文件拖到场景中的任何对象上即可在编辑器模式下为对象显示编号。

如果在Unity中使用上述代码时找不到Handles,可能需要在脚本开头添加以下命名空间:

using UnityEditor;

以下是完整代码:

using UnityEditor;

using UnityEngine;

[ExecuteInEditMode]

public class DisplayNodeIndex : MonoBehaviour

{

void OnSceneGUI()

{

GameObject allPathPointParent = GameObject.Find("All_PathPoint_Parent");

if (allPathPointParent != null)

{

Transform[] pathPoints = allPathPointParent.GetComponentsInChildren();

int count = 0;

foreach (Transform pathPoint in pathPoints)

{

if (pathPoint == allPathPointParent.transform)

continue;

count++;

Handles.Label(pathPoint.position, count.ToString());

}

}

}

}

可以将此代码片段复制到您的Unity脚本文件中。确保脚本附加到场景中的对象上或打开Unity编辑器窗口。然后,将此脚本文件拖到场景中的任何对象上即可在编辑器模式下为对象显示编号。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20230306A05T0600?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券