【发布时间】:2020-04-01 13:04:17
【问题描述】:
总的来说,我对 Unity 和 c# 非常陌生,但我目前有一个具有 2 个不同背景的游戏,我想知道如何在单击按钮时在 2 个背景之间切换 - LightBackground 和 NightBackground,默认背景为 LightBackground,当单击按钮时,将使用 NightBackground。我的想法是在单击按钮时更改 NightBackground 的层(sortingOrder)中的顺序,但在尝试很多事情时非常不成功。
目前我已经制作了一个 Night/Dark 脚本,我已将其放置在单击按钮中,它会更改 Nightbackground 的排序顺序,但我怎样才能让它同时更改 LightBackground 的排序顺序。此外,目前我有一个名为 Game Manager 的游戏对象,它链接到 Gameover 画布,显示我想要的按钮和重播按钮;但是一旦单击按钮,背景就会改变,但是一旦单击重放按钮,背景就会变回 LightBackground。
NightDark 脚本:
public class NightDark : MonoBehaviour
{
public GameObject NightBackground;
// Start is called before the first frame update
void Start()
{
NightBackground.GetComponent<SpriteRenderer>().sortingOrder++;
}
// Update is called once per frame
void Update()
{
}
}
游戏管理器脚本:
public class GameManager : MonoBehaviour
{
public GameObject gameOverCanvas;
private void Start()
{
Time.timeScale = 1;
}
public void GameOver()
{
gameOverCanvas.SetActive(true);
Time.timeScale = 0;
}
public void Replay()
{
SceneManager.LoadScene(0);
}
}
在这里查看Game Manager 和NightDark 可能更容易。
如果你们中的任何人能提供一点帮助,我将不胜感激,因为我现在真的很挣扎。再次感谢。
【问题讨论】:
-
这是
c#,而不是称为unityscript的已弃用JavaScript 风格。这也与VisualStudio没有直接关系 -
哦,真的,很抱歉没有意识到 unityscript 是不同的东西。我假设因为我使用了 Visual Studio,所以它可能适用于那个?。
-
请仔细阅读标签说明;)
Use this tag if you have a specific question about Visual Studio features and functionality. DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio. Consider tagging the exact technology area your question links to and also tagging a more specific version of Visual Studio. Please mention your exact VS version, edition, and update level in your question.