【问题标题】:Unity changing components of different assets when a button is clicked单击按钮时统一更改不同资产的组件
【发布时间】: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 ManagerNightDark 可能更容易。

如果你们中的任何人能提供一点帮助,我将不胜感激,因为我现在真的很挣扎。再次感谢。

【问题讨论】:

  • 这是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.

标签: c# unity3d


【解决方案1】:

反对拥有一个单一背景控制器的理由,而只是切换您在背景中显示的Sprite SpriteRenderer.sprite 属性

public class BackgroundController : MonoBehaviour
{
    // Drag all these in via the Inspector
    [Header("References")]
    [SerielizeField] private SpriteRenderer backgroundRenderer;

    [Header("Assets")]
    [SerielizeField] private Sprite daySprite;
    [SerielizeField] private Sprite nightSprite;    

    // This is static so it keeps its value session wide also after
    // reloading the scene
    private static bool _isDay;

    private void Start()
    {
        // inverts the _isDay -> starts as day the first time
        SwitchBackground();
    }

    // This you call when the button is clicked
    public void SwitchBackground()
    {
        // invert the flag
        _isDay = !_isDay;

        // chose the new sprite according to the flag
        backgroundRenderer.sprite = _isDay ? daySprite : nightSprite;
    }
}

【讨论】:

  • 我明白你的意思。谢谢,这似乎是编写脚本的更好方法。只是为了澄清,我应该把这个脚本放在哪个组件上?
  • 嗯,我应该在后台渲染器变量中放入什么?另外,我应该如何将它分配给按钮?
  • 对此有什么想法吗?
  • 这应该在场景中的任何对象上,但我更喜欢背景;)您只需要一个背景,所以要么保留浅色或深色背景对象,然后删除另一个。然后拖入附加了SpriteRenderer 的相应对象...
  • 那么对于按钮有多种方式和在线教程:例如Via the Inspectorvia code
猜你喜欢
  • 2013-06-15
  • 2019-07-06
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 2020-06-14
  • 2018-04-13
相关资源
最近更新 更多