我遇到一个问题,当场景1中的一切完成后转到场景2。场景2中有一个按钮也有bgm,但它们都不起作用。
这是场景 1 的图像
这是场景 2 的图像
下面的代码附有按钮,以便当玩家单击“开始”时将开始游戏。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class CameraManager : MonoBehaviour
{
// Import button &
//public Camera camera;
public Camera[] cameras;
public GameObject[] detectors;
public Button button;
public GameObject divider;
public GameObject leaderboard;
public Animator animator;
public GameManager gameManager;
private LeaderboardManager leaderboardManager;
private int currentCameraIndex = 0;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
//camera = cameras[0];
cameras[0].enabled = true;
button.gameObject.SetActive(true);
Debug.Log("Button is Actived");
Debug.Log("Button is interactable: " + button.interactable);
}
// Update is called once per frame
void Update()
{
}
//if button onCLick() switch camera
public void buttonOnClick()
{
Debug.Log("pressed");
// Hide start button
button.gameObject.SetActive(false);
//show Leaderboad
leaderboard.gameObject.SetActive(true);
animator.SetTrigger("Start Anim");
divider.gameObject.SetActive(false);
switchToNextCamera();
}
public void switchToNextCamera()
{
// Disable the current camera
cameras[currentCameraIndex].enabled = false;
// Move to the next camera, looping back to the start if necessary
//currentCameraIndex = (currentCameraIndex + 1) % cameras.Length;
currentCameraIndex = currentCameraIndex + 1;
// Enable the new camera
cameras[currentCameraIndex].enabled = true;
Debug.Log("Switched to camera index: " + currentCameraIndex);
}
// Detect collision with a detector and switch to the corresponding camera
void OnTriggerEnter(Collider other)
{
Debug.Log("Something entered the trigger: " + other.gameObject.name);
}
public void switchToSpecificCamera(int index)
{
// Ensure the index is within bounds
if (index >= 0 && index < cameras.Length)
{
// Disable the current camera
cameras[currentCameraIndex].enabled = false;
// Switch to the specified camera
currentCameraIndex = index;
cameras[currentCameraIndex].enabled = true;
}
}
}
这里是有关 GameManager 的代码,可获取详细信息。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject); // Keep the GameManager alive across scenes
}
else
{
Destroy(gameObject); // If another GameManager exists, destroy this one
}
}
// Start is called before the first frame update
void Start()
{
getPlayerNames();
}
// Update is called once per frame
void Update()
{
}
public string[] getPlayerNames()
{
if (FileUploaderCrossPlatform.isUsingFile == true)
{
string[] playerName = FileUploaderCrossPlatform.PlayerName;//ReadData correct
return playerName;
}
else if (FileUploaderCrossPlatform.isUsingFile == false)
{
string[] playerName = MainMenu.Instance.readPlayerName();
return playerName;
}
else
{
Debug.LogWarning("Player names not loaded yet.");
return new string[0]; // Return an empty array if no data is available
}
}
public string[] getPlayerID()
{
if (FileUploaderCrossPlatform.isUsingFile == true)
{
string[] playerID = FileUploaderCrossPlatform.PlayerID;//Read data correct
return playerID;
}
else if(FileUploaderCrossPlatform.isUsingFile == false)
{
string[] playerID = MainMenu.Instance.readPlayerID();//Read data correct
return playerID;
}
else
{
Debug.LogWarning("Player ID not loaded yet.");
return new string[0]; // Return an empty array if no data is available
}
}
public int getNumberOfPlayer()
{
if (FileUploaderCrossPlatform.isUsingFile == true)
{
// Return the array of player
int numberOfPlayers = FileUploaderCrossPlatform.NumberOfPlayers; // Access static variable
Debug.Log("Number of players(File): " + numberOfPlayers);
return numberOfPlayers;
}
else if (FileUploaderCrossPlatform.isUsingFile == false)
{
//Empty file read input
int numberOfPlayers = MainMenu.Instance.readNumberOfPlayers();
Debug.Log("Number of players (Input): " + numberOfPlayers);
return numberOfPlayers;
}
else
{
Debug.LogWarning("Num of Player not loaded yet.");
return 0; // Return an empty array if no data is available
}
}
public void OnBallPassed()
{
Debug.Log("The ball has passed the trigger point!");
}
}
下面是检测输入并将详细信息保存到数组中的代码。这将在场景 1 下进行。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using System.IO;
public class MainMenu : MonoBehaviour
{
public TMP_InputField[] playerInputFields;
public static MainMenu Instance;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject); // If another MainMenu exists, destroy this one
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void loadnext()
{
//SceneManager.LoadScene("Track1");
SceneManager.LoadScene("Track2");
}
public int readNumberOfPlayers()
{
int numberOfPlayers = 0;
foreach (TMP_InputField inputField in playerInputFields)
{
if (!string.IsNullOrEmpty(inputField.text))
{
numberOfPlayers++;
}
}
return numberOfPlayers;
}
public string[] readPlayerName()
{
List<string> playerNames = new List<string>();
foreach (TMP_InputField inputField in playerInputFields)
{
if (!string.IsNullOrEmpty(inputField.text))
{
playerNames.Add(inputField.text); // Add the name if it's not empty
}
}
return playerNames.ToArray();
}
public string[] readPlayerID()
{
List<string> playerIDs = new List<string>();
int i = 0;
foreach (TMP_InputField inputField in playerInputFields)
{
if (!string.IsNullOrEmpty(inputField.text))
{
playerIDs.Add((i + 1).ToString());
i++;
}
}
return playerIDs.ToArray();
}
}
我想我看到了问题,如果按钮没有响应,很可能是因为没有与之绑定的事件侦听器。您需要向
buttonOnClick()
中的按钮添加事件侦听器,而不是仅仅 Start()
坐在那里。
尝试将此行添加到
Start()
以将按钮单击事件链接到 buttonOnClick()
方法:button.onClick.AddListener(buttonOnClick);