在实例化对象上切换
我创建了一个具有Unity UI的切换,并在其中添加了脚本。
因此,关键是我的游戏实例化了许多具有特定标签的对象。我想找到它们,然后切换到他们的网状渲染器。
我已经有了setActive。但是,它完全隐藏了对象,并且不对它存在问题。 很乐意获得任何帮助!
public class ToggleRaycast : MonoBehaviour
{
public Toggle m_Toggle;
public void OnMouseDown()
{
if (m_Toggle.isOn)
{
GameObject[] raycasters2 = GameObject.FindGameObjectsWithTag("Raycaster");
foreach (GameObject raycaster2 in raycasters2)
{
raycaster2.SetActive(true);
}
}
else
{
GameObject[] raycasters2 = GameObject.FindGameObjectsWithTag("Raycaster");
foreach (GameObject raycaster2 in raycasters2)
{
raycaster2.SetActive(false);
}
}
}
}
I have created a Toggle with Unity UI and added a script to it.
So the point is, that my game instantiated a number of objects with a specific tag. I want to find them and toggle on/off their mesh renderer.
I have come so far with SetActive. However, it hides the object completely and there is a problem with unhiding it.
Will be happy to receive any help!
public class ToggleRaycast : MonoBehaviour
{
public Toggle m_Toggle;
public void OnMouseDown()
{
if (m_Toggle.isOn)
{
GameObject[] raycasters2 = GameObject.FindGameObjectsWithTag("Raycaster");
foreach (GameObject raycaster2 in raycasters2)
{
raycaster2.SetActive(true);
}
}
else
{
GameObject[] raycasters2 = GameObject.FindGameObjectsWithTag("Raycaster");
foreach (GameObject raycaster2 in raycasters2)
{
raycaster2.SetActive(false);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那样?您可以从孩子那里得到渲染器,但请注意,它还将包括Trailrenderer,PreacteryStemredenrer等。Meshrenderer和SkinnedMeshrenderer没有明确的基类,因此您可能需要处理这两种情况。
另请注意,FindGameObjectswithTag和GetComponentsInchildren和非常昂贵的操作,您应该在游戏玩法中避免使用它们
您应该为渲染器制作字段,并在开始或清醒一次初始化它们
Like that? You can Get Renderers from children but note it will also include TrailRenderer, ParticleSystemRedenrer etc. There is no explicit base class for MeshRenderer and SkinnedMeshRenderer so you might need to handle both cases like that feg.
Also note that FindGameObjectsWithTag and GetComponentsInChildren and very expensive operations and you should avoid using them during gameplay
You should make field for renderers and initialize them once in Start or Awake