繁体   English   中英

单击所有对象时自动更改场景 Unity

[英]Change scene automatically when all objects are clicked Unity

我正在 Unity 中制作一个新游戏,但我被困在我的脚本中。 我真的是 C# 脚本的菜鸟。 我已经在寻找所有信息,但没有运气。 游戏非常简单,它是2D游戏,只需点击气泡,点击时它们就会旋转。 我将描述我确切需要创建的内容。 单击所有对象时,我需要一个脚本,然后场景会自动更改为下一个级别 + 我需要它有一个时间线,例如,每个级别有 30 秒的时间来单击所有气泡,当时间结束时游戏结束并且会弹出一个带有“游戏结束”消息的窗口,您可以按回复和退出按钮。 我真的希望有人帮助我。 谢谢!

PS 这是我的游戏对象脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class pop : MonoBehaviour
{
    public AudioClip sound;
    AudioSource audio;


    // Start is called before the first frame update
    void Start()
    {
        audio = GetComponent<AudioSource>();
        

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    bool wasClicked = false;


    private void OnMouseDown()
    {
        if (!wasClicked)
        {
            wasClicked = true;
            transform.Rotate(0, 0, 180);
            audio.PlayOneShot(sound);
  
        }

    }


}

你应该把这些分开。

我会有一个场景变化的中央经理,例如

public class Manager : MonoBehaviour
{
    [SerializeField] private timer = 30;

    private void Awake ()
    {
        // Register to an event we will add which is fired everytime 
        // a pop was clicked
        pop.onClicked += PopClicked();
    }

    private void OnDestroy ()
    {
        // Don't forget to remove the callback as soon as not needed anymore
        pop.onClicked -= PopClicked();
    }

    private void PopClicked ()
    {
        // Check if there is no Unclicked pop remaining
        if(pop.UnClicked.Count == 0)
        {
            // if so go to the next scene
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
    }

    private void Update ()
    {
        // Every Frame reduce the timer by the time passed since the last frame
        timer -= Time.deltaTime;
        // Maybe also update a timer display text here

        if(timer <= 0)
        {
            // if it reaches 0 -> GameOver scene
            SceneManager.LoadScene("GameOver");
        }
    }
}

然后用一些额外的东西相应地修改你的Pop类:

public class pop : MonoBehaviour
{
    // Stores all Unclicked instances
    // As this is static it is "shared" between all instances or better said
    // it is part of the type itself 
    private static HashSet<pop> _unclicked = new HashSet<pop>();

    // Public readonly access
    public static HashSet<pop> Unclicked => new HashSet<pop>(_unclicked);

    // Event that is invoked everytime a pop is clicked
    public static event Action onClicked;

    public AudioClip sound;
    [SerializeField] AudioSource audio;

    void Awake()
    {
        if(!audio) audio = GetComponent<AudioSource>();
        
        // Add yourself to the collection of Unclicked instances
        _uncliked.Add(this);
    }

    private void OnDestroy ()
    {
        // Don't forget to also remove in case this is destroyed 
        // e.g. due to the scene change to GameOver
        if(_unclicked.Contains(this)) _unclicked.Remove(this);
    }

    private void OnMouseDown()
    {
        // Is this still Unclicked?
        if (!_unclicked.Contains(this)) return;
        
        transform.Rotate(0, 0, 180);
        audio.PlayOneShot(sound);
 
        // Remove yourself from the Unclicked instances
        _unclicked.Remove(this); 
        // Invoke the event
        onClicked?.Invoke();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM