【发布时间】:2016-11-30 01:04:54
【问题描述】:
我有 WallCreator 脚本来在 Unity 中放置墙壁,还有一个 WallCreatorSwitcher 通过检查切换开关来打开/关闭 WallCreator。我还想使用 @987654321 @更改WallPrefab的层,因此,当检查切换(WallCreator On)时,层是“忽略的raycast”,并且它可以工作,并且如果不受控制(WallCreator Off)(WallCreator Off)层是“ DEFAULT”,则为“ default”。 - 但问题是,在这种情况下它并没有改变图层。
public class WallCreatorSwitcher : MonoBehaviour {
public Toggle toggle;
public Text text;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (toggle.isOn)
{
GetComponent<WallCreator>().enabled = true;
Debug.Log("Wall Creator is ON");
text.enabled = true;
GetComponent<WallCreator>().wallPrefab.layer = LayerMask.NameToLayer("Ignore Raycast");
}
else
{
GetComponent<WallCreator>().enabled = false;
Debug.Log("Wall Creator is OFF");
text.enabled = false;
GetComponent<WallCreator>().wallPrefab.layer = LayerMask.NameToLayer("Default");
}
}
}
【问题讨论】:
-
想必,您在这里面临的问题是
GetComponent<WallCreator>().enabled = false;完全停止了正在与之交互的组件,因此您可能必须打开它,更改图层,然后再打开它退出,或者在关闭之前进行图层切换 -
即使我删除了
GetComponent<WallCreator>().enabled = false;(因此即使未选中切换开关我也可以放置墙壁),图层仅针对新对象进行更改。我的意思是,例如,我将 wall1 放置为 ON - 它的图层是“Ignore Raycast”,然后我将切换切换到 OFF,放置 wall2 - 它的图层是“Default”,但 wall1 的图层不会改变 -
我没有意识到你的意思是之前放置的墙壁,这完全是一个不同的问题。较新的墙在更改后位于默认图层中 - 这是因为更改预制件会更改创建墙时放置的内容,而不是已经存在的内容。要更改所有墙的层,您可以执行以下操作(假设墙有标签
wall):GameObject[] walls = GameObject.FindGameObjectsWithTag("wall");然后您可以通过 foreach 循环操作它们 -
您还应该发布您的
WallCreator脚本并描述每个脚本的附加位置 -
@AlfieGoodacre 是的,我可能没有正确地提出我的问题。无论如何,您的回答很有帮助,现在可以使用了,谢谢