[英]Change scene automatically when all objects are clicked Unity
I'm making a new game in Unity and I'm stuck in my script.我正在 Unity 中制作一个新游戏,但我被困在我的脚本中。 I'm really noob in C# scripting.我真的是 C# 脚本的菜鸟。 I'm already looking for all information but no luck.我已经在寻找所有信息,但没有运气。 The game is very simple, it is 2D game where need just click on bubbles and they are rotating when are clicked.游戏非常简单,它是2D游戏,只需点击气泡,点击时它们就会旋转。 I will describe what I exactly need to create.我将描述我确切需要创建的内容。 I need a script when all objects are clicked then scene automatically changes to the next level + I need it to have a timeline, for example, for each level have 30 seconds to click all bubbles, when the time is over the game is over and a window pops up with a message "Game Over" and you can press the Reply and Exit buttons.单击所有对象时,我需要一个脚本,然后场景会自动更改为下一个级别 + 我需要它有一个时间线,例如,每个级别有 30 秒的时间来单击所有气泡,当时间结束时游戏结束并且会弹出一个带有“游戏结束”消息的窗口,您可以按回复和退出按钮。 I really hope that someone helps me.我真的希望有人帮助我。 Thanks!谢谢!
PS This is my script now for my gameObjects: 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);
}
}
}
You should separate these.你应该把这些分开。
I would have a central manager for the scene change like eg我会有一个场景变化的中央经理,例如
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");
}
}
}
And then modify your Pop
class accordingly with some additional things:然后用一些额外的东西相应地修改你的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.