我购买播放器后切换字符不起作用

时间:2019-05-10 16:02:08

标签: c# unity3d

我制作了2D游戏,在购买了玩家之后,不允许我停留在菜单中为玩家选择地图(我制作了3张地图)。游戏开始,并且只是我创建的第一个玩家。

这是脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwitchScript : MonoBehaviour
{
    public GameObject avatar1, avatar2;
    int wichAvatarIsOn = 1;
    // Start is called before the first frame update
    void Start()
    {
        avatar1.gameObject.SetActive(true);
        avatar2.gameObject.SetActive(false);            
    }

    public void SwitchAvatar()
    {
        switch (wichAvatarIsOn)
        {
            case 1:
                wichAvatarIsOn = 2;
                avatar1.gameObject.SetActive(false);
                avatar2.gameObject.SetActive(true);
                break;
            case 2:
                wichAvatarIsOn =  1;
                avatar1.gameObject.SetActive(true);
                avatar2.gameObject.SetActive(false);
                break;
        }
    }
}

这是购买脚本。我应该在BuyComplete写什么?我应该搜索另一个脚本吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PurchaseScript : MonoBehaviour
{
    public void BuyComplete(UnityEngine.Purchasing.Product product)
    {
        Application.LoadLevel("Scene/Room3");
    }

    public void BuyFailed(UnityEngine.Purchasing.Product product, UnityEngine.Purchasing.PurchaseFailureReason fa)
    {
        Debug.Log("PURCHASE FAILED");
    }
}

1 个答案:

答案 0 :(得分:0)

您已经对wichAvatarIsOn = 1进行了硬编码,因此它将始终使用相同的头像。

您需要存储刚购买的头像。

您可以使用PlayerPrefs在场景和游戏加载之间存储信息。

将其存储在某个位置后,需要使用已经拥有的Start()方法从SwitchAvatar()方法中选择它。

SwitchScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwitchScript : MonoBehaviour
{
    public GameObject avatar1, avatar2;
    int wichAvatarIsOn;
    void Start()
    {
        if(PlayerPrefs.HasKey("wichAvatarIsOn"))
        {
            wichAvatarIsOn = PlayerPrefs.GetInt("wichAvatarIsOn");
        }
        else
        {
            wichAvatarIsOn = 1;//Default to an avatarID you want as default
        }
        SwitchAvatar(wichAvatarIsOn);
    }

    public void SwitchAvatar(int avatarID)
    {
        switch (avatarID)
        {
            case 1:
                wichAvatarIsOn = 2;
                avatar1.gameObject.SetActive(false);
                avatar2.gameObject.SetActive(true);
                break;
            case 2:
                wichAvatarIsOn =  1;
                avatar1.gameObject.SetActive(true);
                avatar2.gameObject.SetActive(false);
                break;
            default:
                //Set a default avatar incase out of range and add a debug message
                Debug.Log("Avatar ID out of range: " + avatarID);
                wichAvatarIsOn = 2;
                avatar1.gameObject.SetActive(false);
                avatar2.gameObject.SetActive(true);
                break;
        }
    }
}

PurchaseScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PurchaseScript : MonoBehaviour
{
    public void BuyComplete(UnityEngine.Purchasing.Product product)
    {
        PlayerPrefs.SetInt("wichAvatarIsOn", product.id); //I added it as product.id for this example, you will need to decide how you will do it. 
        Application.LoadLevel("Scene/Room3");
    }

    public void BuyFailed(UnityEngine.Purchasing.Product product, UnityEngine.Purchasing.PurchaseFailureReason fa)
    {
        Debug.Log("PURCHASE FAILED");
    }
}