c# - 购买播放器后切换角色不起作用
问题描述
我做了一个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");
}
}
解决方案
您已硬编码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");
}
}
推荐阅读
- python-3.x - 重新映射opencv拼接图像
- java - 在 TextView Android studio 上显示来自数据库的信息
- python - 为什么 python lxml etree xpath 返回多个元素?
- html - 使用 CSS 将元素放置在容器底部
- bluetooth-lowenergy - 通过 BLE 从另一个 Apple Watch 查找 Apple Watch 广告数据以计算距离
- java - 如何将 Kotlin 中的 UUID 缩短/散列到最大 20 长度的字符串?
- ruby-on-rails - 如何删除rails的postgres用户密码?
- python - 如何为 H5 配置 maxshape 参数并附加到文件?
- c# - 用户重新排序后,在服务器端获取 RadListBox(用于 ASP.NET AJAX 的 UI)中的所有项目的简单方法
- asp.net-core - 从 .net 核心中的现有数据库创建模型后无法获得 JSON 结果