我有一个游戏,名称是测验游戏RPG,但我有1个问题,问题是为什么在按到城镇(这是主菜单)并更改角色后,再次玩该角色,不是当前字符,但前一个字符,即返回0的索引。
我尝试在开始和更改索引上添加playerprefs,但未进行任何更改。在那之后仍然回到0。
字符选择脚本:
private void Start()
{
characterSelect = FindObjectOfType<_CharacterSelect>();
audioManager = FindObjectOfType<AudioManager>();
selectedCharIndex = PlayerPrefs.GetInt("Player",0);
UpdateCharacterUI();
character = charactersList[selectedCharIndex].Character;
isSold = PlayerPrefs.GetInt(selectedCharIndex + "IsSold", 0);
if (selectedCharIndex == 0)
{
if (isSold == 0)
{
PlayerPrefs.SetInt(selectedCharIndex + "IsSold", 1);
}
}
CheckStatus();
CallPlayer();
}
public void CallPlayer()
{
currentCharacter = Instantiate(character, new Vector3(-2.403f, -0.6f, 0f), Quaternion.identity);
}
private void Update()
{
StartCoroutine(SaveCharacter());
}
void CheckStatus()
{
moneyAmount = (int)PlayerPrefs.GetFloat("MoneyAmount");
isSold = PlayerPrefs.GetInt(selectedCharIndex + "IsSold", 0);
if (moneyAmount >= 0)
{
buyButton.interactable = true;
}
else
{
buyButton.interactable = false;
}
if (isSold == 1)
{
buyButton.gameObject.SetActive(false);
}
else
{
buyButton.gameObject.SetActive(true);
}
Debug.Log(moneyAmount);
Debug.Log(isSold);
}
public void LeftArrow()
{
moneyAmountText.text = moneyAmount.ToString() + "$";
audioManager.play("SFX");
selectedCharIndex--;
if (selectedCharIndex < 0)
{
selectedCharIndex = charactersList.Count - 1;
}
CheckStatus();
UpdateCharacterUI();
}
public void RightArrow()
{
moneyAmountText.text = moneyAmount.ToString() + "$";
audioManager.play("SFX");
selectedCharIndex++;
if (selectedCharIndex == charactersList.Count)
{
selectedCharIndex = 0;
}
CheckStatus();
UpdateCharacterUI();
}
public void Buy()
{
audioManager.play("Unlocked");
moneyAmount -= 0;
PlayerPrefs.SetInt(selectedCharIndex + "IsSold", 1);
buyButton.gameObject.SetActive(false);
PlayerPrefs.SetFloat("MoneyAmount", moneyAmount);
moneyAmountText.text = moneyAmount.ToString() + "$";
}
IEnumerator transition()
{
yield return new WaitForSeconds(2f);
Price.text = "Sold!";
}
public void Confirm()
{
audioManager.play("Confirm");
PlayerPrefs.SetInt("Player", selectedCharIndex);
Debug.Log(string.Format("Character{0}:{1} has been choosen", selectedCharIndex, charactersList[selectedCharIndex].CharName));
character = charactersList[selectedCharIndex].Character;
ChangeCharacter();
}
void ChangeCharacter()
{
Vector3 currentPosition = currentCharacter.transform.position;
Destroy(currentCharacter);
currentCharacter = Instantiate(character, currentPosition, Quaternion.identity);
cameraFollow = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraFollow>();
cameraFollow.GetPlayer(currentCharacter);
}
IEnumerator SaveCharacter()
{
if (saveChar)
{
saveChar = false;
yield return new WaitForSeconds(0.05f);
currentCharacter = Instantiate(character, new Vector3(-2.47699f, 0.169f, 0), Quaternion.identity);
cameraFollow = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraFollow>();
cameraFollow.GetPlayer(currentCharacter);
}
}
}
对于levelManager:
prvoid Start()
{
_CharacterSelect = FindObjectOfType<_CharacterSelect>();
audioManager = FindObjectOfType<AudioManager>();
selectLevel = FindObjectOfType<SelecLevel>();
gameManager = FindObjectOfType<GameManager>();
FillList();
}
void FillList()
{
Index = 0;
for (int i = 0; i < selectLevel.Levels; i++)
{
LevelButton levelButton = Instantiate(TheButton, Spacer).GetComponent<LevelButton>();
levelButton.LevelIndex = Index;
levelButton.button.onClick.AddListener(() => SelectLevel(levelButton.LevelIndex));
Index++;
}
}
void DeleteAll()
{
PlayerPrefs.DeleteAll();
Application.LoadLevel(0);
}
void SelectLevel(int index)
{
_CharacterSelect.saveChar = true;
selectLevel.levelIndex = index;
Application.LoadLevel("Level " + (index + 1));
switch (selectLevel.levelIndex)
{
case 1:
audioManager.StopPlay("BGM");
audioManager.play("BGM1");
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
public void ReseData()
{
DeleteAll();
}
}
最后一个GameManager:
private void Start()
{
audioManager = FindObjectOfType<AudioManager>();
//questionManager = FindObjectOfType<_questionManager>();
LevelSelect = FindObjectOfType<SelecLevel>();
Select = FindObjectOfType<_CharacterSelect>();
sliderChanges = FindObjectOfType<SliderChanges>();
onlepel = LevelSelect.levelIndex + 1;
IndexLevel = LevelSelect.levelIndex;
getPlayerData();
Timecount = GameObject.FindGameObjectWithTag("TimeCount");
category = FindObjectOfType<_questionManager>().category;
moneyAmount = PlayerPrefs.GetFloat("MoneyAmount");
if (unansweredQuestion == null || unansweredQuestion.Count == 0)
{
thisQuestions = category[IndexLevel].questions;
unansweredQuestion = new List<Question>(thisQuestions);
//TrueAnswerText.text = "CORRECT";
//FalseAnswerText.text = "WRONG!";
//unansweredQuestion = new List<Question>(questions);
}
TrueCount = 0;
if (FactText != null)
setCurrentQuestion();
}
private void Update()
{
moneyText.text = moneyAmount.ToString() + "$";
}
#region MainQuestion
public void getPlayerData()
{
levelindexPlayerPrefs = PlayerPrefs.GetInt("LevelIndex", 0);
}
public void updateLevel(int Index)
{
if (levelindexPlayerPrefs < Index)
{
PlayerPrefs.SetInt("LevelIndex", Index);
levelindexPlayerPrefs = PlayerPrefs.GetInt("LevelIndex");
}
}
public void setCurrentQuestion()
{
Scoretext.text = TrueCount.ToString() + " / " + thisQuestions.Length.ToString();
RandomQuestionIndex = Random.Range(0, unansweredQuestion.Count);
currentQuestion = unansweredQuestion[RandomQuestionIndex];
FactText.text = currentQuestion.fact;
//IsQuestionAvailable = true;
sliderChanges.TimeRemaining = sliderChanges.TimeMax;
}
public IEnumerator TransitionToNextQuestion()
{
unansweredQuestion.Remove(currentQuestion);
answerQuestion = false;
if (unansweredQuestion.Count == 0 || unansweredQuestion == null)
{
count = thisQuestions.Length;
changeLevel = false;
thisQuestions = category[IndexLevel].questions;
unansweredQuestion = new List<Question>(thisQuestions);
//unansweredQuestion = new List<Question>(questions);
count++;
if (count == thisQuestions.Length || CurrentTrueCount == thisQuestions.Length) ;
{
Scoretext.text = TrueCount.ToString() + " / " + thisQuestions.Length.ToString();
changeLevel = true;
//Objective.SetActive(true);
StartCoroutine(EndGame());
Timecount.SetActive(false);
StartCoroutine(EveryChapter());
}
}
yield return new WaitForSeconds(.5f);
setCurrentQuestion();
}
public IEnumerator EndGame()
{
int Currentstar = PlayerPrefs.GetInt("Level" + (IndexLevel + 1));
PlayerPrefs.SetFloat("MoneyAmount", moneyAmount);
if (TrueCount < thisQuestions.Length || TrueCount == thisQuestions.Length - 5)
{
if (Currentstar < 1)
PlayerPrefs.SetInt("Level" + (IndexLevel + 1), 1);
changeLevel = true;
yield return new WaitForSeconds(1f);
Objective.SetActive(true);
yield return new WaitForSeconds(.3f);
audioManager.play("Stars");
Star1.SetActive(true);
}
if (TrueCount > thisQuestions.Length - 4 || CurrentTrueCount <= thisQuestions.Length - 3)
{
if (Currentstar < 2)
PlayerPrefs.SetInt("Level" + (IndexLevel + 1), 2);
changeLevel = true;
yield return new WaitForSeconds(1f);
Objective.SetActive(true);
yield return new WaitForSeconds(.3f);
audioManager.play("Stars");
Star2.SetActive(true);
}
if (TrueCount > thisQuestions.Length - 2 || TrueCount == thisQuestions.Length)
{
if (Currentstar < 3)
PlayerPrefs.SetInt("Level" + (IndexLevel + 1), 3);
changeLevel = true;
yield return new WaitForSeconds(1f);
Objective.SetActive(true);
yield return new WaitForSeconds(.3f);
audioManager.play("Stars");
Star1.SetActive(true);
audioManager.play("Stars");
Star3.SetActive(true);
}
updateLevel(IndexLevel + 1);
getPlayerData();
Debug.Log(levelindexPlayerPrefs);
}
IEnumerator EveryChapter()
{
Scoretext.text = TrueCount.ToString() + " / " + thisQuestions.Length.ToString();
yield return new WaitForSeconds(7f);
}
public void HandleAnswerButton(bool answer)
{
//IsQuestionAvailable = false;
enemyGenerator.SpawnEnemy();
sliderChanges.TimeRemaining = 0;
FactText.text = string.Empty;
if (answer)
{
animator[0].SetTrigger("click");
if (currentQuestion.isTrue)
{
TrueAnswerText.text = "CORRECT";
moneyAmount += .5f;
answerQuestion = true;
TrueCount++;
Isbattle = true;
//Scoretext.text = TrueCount.ToString() + " / " + thisQuestions.Length.ToString();
}
else
{
TrueAnswerText.text = "WRONG!";
Debug.Log("Wrong!");
}
}
if (!answer)
{
animator[1].SetTrigger("Clack");
if (!currentQuestion.isTrue)
{
FalseAnswerText.text = "CORRECT";
moneyAmount += .5f;
Isbattle = false;
answerQuestion = true;
TrueCount++;
Debug.Log("Correct");
Debug.Log(TrueCount);
//Scoretext.text = TrueCount.ToString() + " / " + thisQuestions.Length.ToString();
}
else
{
FalseAnswerText.text = "WRONG!";
Debug.Log("Wrong!");
}
}
Scoretext.text = TrueCount.ToString() + " / " + thisQuestions.Length.ToString();
//StartCoroutine(TransitionToNextQuestion());
}
#endregion
#region Panels
public void Paused()
{
StartCoroutine(Pause());
PausePanel.SetActive(true);
}
IEnumerator Pause()
{
audioManager.play("Confirm");
yield return new WaitForSeconds(.3f);
Time.timeScale = 0f;
}
public void Resume()
{
audioManager.play("Close");
Time.timeScale = 1f;
PausePanel.SetActive(false);
}
public void Back()
{
audioManager.play("Close");
}
public void Confirm()
{
audioManager.play("Confirm");
}
public void Dead()
{
sliderChanges.gameObject.SetActive(false);
DeadPanel.SetActive(true);
HealthBar.SetActive(false);
PauseButton.SetActive(false);
Scoretext.gameObject.SetActive(false);
GoldField.SetActive(false);
Select.saveChar = true;
}
public void TheOptionPanel()
{
audioManager.play("Confirm");
OptionPanel.SetActive(true);
}
public void PG_MainMenu()
{
audioManager.play("Confirm");
PlayGame.SetActive(true);
}
public void ExitGame()
{
audioManager.play("Confirm");
Application.Quit();
}
#endregion
#region TransitionToNextScene
public void ToTheTown()
{
audioManager.StopPlay("BGM1");
audioManager.play("BGM");
audioManager.play("Confirm");
SceneManager.LoadScene(0);
Time.timeScale = 1f;
}
public void NExtLevel()
{
LevelSelect.levelIndex++;
Select.saveChar = true;
audioManager.play("Confirm");
onlepel += 1;
SceneManager.LoadScene("Level " + onlepel);
switch (LevelSelect.levelIndex)
{
case 1:
audioManager.StopPlay("BGM");
audioManager.play("BGM1");
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
public void restart()
{
audioManager.play("Confirm");
Select.saveChar = true;
Time.timeScale = 1f;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
#endregion
答案 0 :(得分:0)
解决了, 整夜都让我困惑
:'D
但这是因为我没有在角色选择脚本的Savecharacter行设置playerprefs
selectedCharIndex = PlayerPrefs.GetInt("Player");
character = charactersList[selectedCharIndex].Character;