如何编码脚本在赢得一轮后进入下一个场景

发布于 2025-01-21 12:53:00 字数 2660 浏览 0 评论 0原文

如何在赢得一轮脚步后将脚本编码到下一个场景? 当玩家赢得巡回赛而不是按下鼠标按钮时,该怎么做才能搬到下一个场景。我主要指的是这段话 这是我的场景切换代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelLoader : MonoBehaviour
{
    public Animator transition;

    public float transitionTime = 1f;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtDown(0))
        {
            LoadNextLevel();
        }
    }

    public void LoadNextLevel()
    {
        StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
    }

    IEnumerator LoadLevel(int levelIndex)
    {
        transition.SetTrigger("Start");

        yield return new WaitForSeconds(transitionTime);

        SceneManager.LoadScene(levelIndex);
    }

}

这是控制整个游戏的代码:

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;


[RequireComponent(typeof(GameUI))]
public class GameController : MonoBehaviour
{

 
    public static GameController Instance { get; private set; }

    [SerializeField]
    private int knifeCount;

    [Header("Knife Spawning")]
    [SerializeField]
    private Vector2 knifeSpawnPosition;
    [SerializeField]
    
    private GameObject knifeObject;

    
    public GameUI GameUI { get; private set; }

    private void Awake()
    {
        
        Instance = this;

        GameUI = GetComponent<GameUI>();
    }

    private void Start()
    {
        
        GameUI.SetInitialDisplayedKnifeCount(knifeCount);
        
        SpawnKnife();
    }

    
    public void OnSuccessfulKnifeHit()
    {
        if (knifeCount > 0)
        {
            SpawnKnife();
        }
        else
        {
            StartGameOverSequence(true);
        }
    }

    
    private void SpawnKnife()
    {
        knifeCount--;
        Instantiate(knifeObject, knifeSpawnPosition, Quaternion.identity);
    }

   
    public void StartGameOverSequence(bool win)
    {
        StartCoroutine("GameOverSequenceCoroutine", win);
    }

    
    private IEnumerator GameOverSequenceCoroutine(bool win)
    {
        if (win)
        {
            
            yield return new WaitForSecondsRealtime(0.3f);
            
        }
        else
        {
            GameUI.ShowRestartButton();
        }
    }

    public void RestartGame()
    {
        
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
    }
}

How to code a script to go to the next scene after winning a round?
what to do to move to the next scene when the player wins the round and not when the mouse button is pressed. I am mainly referring to this passage
This is my code for scene switching:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelLoader : MonoBehaviour
{
    public Animator transition;

    public float transitionTime = 1f;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtDown(0))
        {
            LoadNextLevel();
        }
    }

    public void LoadNextLevel()
    {
        StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
    }

    IEnumerator LoadLevel(int levelIndex)
    {
        transition.SetTrigger("Start");

        yield return new WaitForSeconds(transitionTime);

        SceneManager.LoadScene(levelIndex);
    }

}

And this is my code that controls the whole game:

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;


[RequireComponent(typeof(GameUI))]
public class GameController : MonoBehaviour
{

 
    public static GameController Instance { get; private set; }

    [SerializeField]
    private int knifeCount;

    [Header("Knife Spawning")]
    [SerializeField]
    private Vector2 knifeSpawnPosition;
    [SerializeField]
    
    private GameObject knifeObject;

    
    public GameUI GameUI { get; private set; }

    private void Awake()
    {
        
        Instance = this;

        GameUI = GetComponent<GameUI>();
    }

    private void Start()
    {
        
        GameUI.SetInitialDisplayedKnifeCount(knifeCount);
        
        SpawnKnife();
    }

    
    public void OnSuccessfulKnifeHit()
    {
        if (knifeCount > 0)
        {
            SpawnKnife();
        }
        else
        {
            StartGameOverSequence(true);
        }
    }

    
    private void SpawnKnife()
    {
        knifeCount--;
        Instantiate(knifeObject, knifeSpawnPosition, Quaternion.identity);
    }

   
    public void StartGameOverSequence(bool win)
    {
        StartCoroutine("GameOverSequenceCoroutine", win);
    }

    
    private IEnumerator GameOverSequenceCoroutine(bool win)
    {
        if (win)
        {
            
            yield return new WaitForSecondsRealtime(0.3f);
            
        }
        else
        {
            GameUI.ShowRestartButton();
        }
    }

    public void RestartGame()
    {
        
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷港 2025-01-28 12:53:00

您需要做的就是在满足条件以加载下一个级别时调用LoadNextlevel()。
我认为您需要在这里打电话:

if (win)
    {
        yield return new WaitForSecondsRealtime(0.3f);
        LoadNextLevel();
    }

All you need to do is call LoadNextLevel() when conditions are met to load next level.
I think you need to call it here:

if (win)
    {
        yield return new WaitForSecondsRealtime(0.3f);
        LoadNextLevel();
    }
南巷近海 2025-01-28 12:53:00
 public void OnCollisionEnter2D(Collision2D collision)
  {
    if (PlayerControl.showflag == 1) ;
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
  }
The following is to press the E key to switch the scene
        void Update()
    {
      if(Input.GetKeyDown(KeyCode.E))
       {
           SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);
    } 
}

我希望可以实施上述代码吗?

 public void OnCollisionEnter2D(Collision2D collision)
  {
    if (PlayerControl.showflag == 1) ;
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
  }
The following is to press the E key to switch the scene
        void Update()
    {
      if(Input.GetKeyDown(KeyCode.E))
       {
           SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);
    } 
}

Can the above code be implemented, I hope so?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文