我正在制作一个成就系统。我的游戏中有2个场景。第一个场景是我的主菜单,另一个场景是我的游戏场景。我在主菜单场景中设计并编码了成就系统,并在我的游戏场景中添加了一个EarnCanvas。 EarnCanvas将显示玩家已解锁的成就。
我的代码只允许我将成就链接到主菜单上的EarnCanvas.i需要帮助修改代码或链接两个场景,以便我可以在游戏中的EarnCanvas中实例化解锁成就预制件现场。目前,当玩家解锁成就时,EarnCanvas中没有实例化或显示预制件。
主菜单场景
成就经理脚本:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class AchievementManager : MonoBehaviour {
public GameObject achievementPraf;
public Sprite[] sprites;
private AchievementButton activeButton;
public ScrollRect scrollRect;
public GameObject achievementMenu;
public GameObject visualAchievement;
public Dictionary<string, Achievement> achievements = new Dictionary<string, Achievement>();
public Sprite UnlockedSprite;
private static AchievementManager instance;
public static AchievementManager Instance
{
get {
if(instance == null)
{
instance = GameObject.FindObjectOfType<AchievementManager>();
}
return AchievementManager.instance;
}
}
// Use this for initialization
void Start ()
{
//remove before deploy
//PlayerPrefs.DeleteAll();
activeButton = GameObject.Find("Streakbtn").GetComponent<AchievementButton>();
CreateAchievement("Streak", "Press W", "Press W to unlock this achievement", 5, 0);
CreateAchievement("Streak", "Press R", "Press R to unlock this achievement", 5, 0);
CreateAchievement("Streak", "Press All Keys", "Press All Keys to unlock", 5, 0, new string[] { "Press W", "Press R" });
foreach (GameObject achievementList in GameObject.FindGameObjectsWithTag("achievementList"))
{
achievementList.SetActive(false);
}
activeButton.click();
achievementMenu.SetActive(false);
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.S))
{
achievementMenu.SetActive(!achievementMenu.activeSelf);
}
if(Input.GetKeyDown(KeyCode.W))
{
EarnAchievemnent("Press W");
}
if (Input.GetKeyDown(KeyCode.R))
{
EarnAchievemnent("Press R");
}
}
public void EarnAchievemnent(string title)
{
if(achievements[title].EarnAchievement())
{
GameObject achievement = (GameObject) Instantiate(visualAchievement);
StartCoroutine(HideAchievement(achievement));
//this is where is its giving the parent the gameobject name to find
SetAchievementInfo("EarnCanvas", achievement, title);
}
}
public IEnumerator HideAchievement(GameObject achievement)
{
yield return new WaitForSeconds(3);
Destroy(achievement);
}
public void CreateAchievement(string parent,string title,string description,int points,int spriteIndex,string[] dependencies = null)
{
GameObject achievement = (GameObject)Instantiate(achievementPraf);
Achievement newAchievement = new Achievement(name, description, points, spriteIndex,achievement);
achievements.Add(title, newAchievement);
SetAchievementInfo(parent, achievement,title);
if(dependencies != null)
{
foreach(string achievementTitle in dependencies)
{
Achievement dependency = achievements[achievementTitle];
dependency.Child = title;
newAchievement.AddDependency(dependency);
//Dependency = Press Space <-- Child = Press W
//NewAchievement = Press W --> Press Space
}
}
}
public void SetAchievementInfo(string parent, GameObject achievement, string title)
{
//this where its trying to find a gameobject-(EarnCanvas) to instantiate the unlocked achievement prefab in the Main Menu Scene
achievement.transform.SetParent(GameObject.Find(parent).transform);
achievement.transform.localScale = new Vector3(1, 1, 1);
achievement.transform.localPosition = new Vector3(0, 0, 0);
achievement.transform.GetChild(0).GetComponent<Text>().text = title;
achievement.transform.GetChild(1).GetComponent<Text>().text = achievements[title].Description;
achievement.transform.GetChild(2).GetComponent<Text>().text = achievements[title].Points.ToString();
achievement.transform.GetChild(3).GetComponent<Image>().sprite = sprites[achievements[title].SprintIndex];
}
public void ChangeCategory(GameObject button)
{
AchievementButton achievementButton = button.GetComponent<AchievementButton>();
scrollRect.content = achievementButton.achievementList.GetComponent<RectTransform>();
achievementButton.click();
activeButton.click();
activeButton = achievementButton;
}
}
主菜单场景
成就剧本:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class Achievement
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string description;
public string Description
{
get { return description; }
set { description = value; }
}
private bool unlocked;
public bool Unlocked
{
get { return unlocked; }
set { unlocked = value; }
}
private int points;
public int Points
{
get { return points; }
set { points = value; }
}
private int sprintIndex;
public int SprintIndex
{
get { return sprintIndex; }
set { sprintIndex = value; }
}
private GameObject achievementRef;
private List<Achievement> dependencies = new List<Achievement>();
private string child;
public string Child
{
get { return child; }
set { child = value; }
}
public Achievement(string name,string description, int points, int sprintIndex, GameObject achievementRef)
{
this.name = name;
this.description = description;
this.unlocked = false;
this.points = points;
this.sprintIndex = sprintIndex;
this.achievementRef = achievementRef;
LoadAchievement();
}
public void AddDependency(Achievement dependency)
{
dependencies.Add(dependency);
}
public bool EarnAchievement()
{
if(!unlocked && !dependencies.Exists(x=> x.unlocked == false))
{
achievementRef.GetComponent<Image>().sprite = AchievementManager.Instance.UnlockedSprite;
SaveAchievement(true);
if(child != null)
{
AchievementManager.Instance.EarnAchievemnent(child);
}
return true;
}
return false;
}
public void SaveAchievement(bool value)
{
unlocked = value;
PlayerPrefs.SetInt(name,value? 1 : 0);
PlayerPrefs.Save();
}
public void LoadAchievement()
{
unlocked = PlayerPrefs.GetInt(name) == 1 ? true : false;
if (unlocked)
{
achievementRef.GetComponent<Image>().sprite = AchievementManager.Instance.UnlockedSprite;
}
}
}
答案 0 :(得分:0)
您应该使用 DontDestroyOnLoad 方法,这是将游戏对象保留在场景更改上的简单方法,您只需将以下代码添加到AchievementManager即可。
void Awake ()
{
DontDestroyOnLoad (transform.gameObject);
}
您可以为任何不希望在更改场景时销毁的游戏对象添加此对象。