unity物体销毁时 Gizmos.DrawWireSphere函数报错ArgumentException: Gizmo drawing functions can only be used in OnDrawGizmos and OnDrawGizmosSelected.
UnityEngine.Gizmos.DrawWireSphere (UnityEngine.Vector3 center, System.Single radius)
朋友你好,根据你的描述,当你在Unity中销毁物体时,使用Gizmos.DrawWireSphere
函数会报错ArgumentException: Gizmo drawing functions can only be used in OnDrawGizmos and OnDrawGizmosSelected.
这个错误是因为你在不正确的地方调用了Gizmos绘图函数。
在Unity中,Gizmos绘图函数(如DrawWireSphere)应该在以下两个特定的函数中使用:
1.OnDrawGizmos
2. OnDrawGizmosSelected
这些函数通常位于MonoBehaviour类的派生类中。你应该在这两个函数中调用Gizmos绘图函数来绘制场景中的调试辅助图形。
如果你想在物体销毁时绘制WireSphere,你可以在MonoBehaviour派生类中的OnDestroy函数中手动绘制,或者在OnDrawGizmos和OnDrawGizmosSelected函数中进行条件判断,只在特定情况下绘制WireSphere。
以下是一个示例代码片段,展示了如何在OnDrawGizmosSelected函数中进行条件判断并绘制WireSphere:
using UnityEngine;
public class YourScript : MonoBehaviour
{
public float radius = 1f;
private void OnDrawGizmosSelected()
{
// 检查物体是否被选中
if (!Selection.Contains(gameObject))
return;
// 绘制WireSphere
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, radius);
}
}
确保将以上代码放置在正确的MonoBehaviour派生类中,并在Unity编辑器中选择该物体后,你应该能够在场景视图中看到WireSphere的绘制。
希望能帮到你,并且帮忙给回答点个采纳哦,谢谢啦!