首页 > 解决方案 > 购买播放器后切换角色不起作用

问题描述

我做了一个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");
    }
}

标签: c#unity3d

解决方案


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

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

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

一旦你将它存储在某个地方,你需要然后使用你已有Start()的方法从你的方法中选择它。SwitchAvatar()

开关脚本

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;
        }
    }
}

购买脚本

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");
    }
}

推荐阅读