unity3d - 当我在 android mobile 上构建时,我的健康点未设置为正确的值
问题描述
我有这个项目一个 2D 横向卷轴游戏,我已将我的 HP 指示器设置为画布内的滑块,它是屏幕空间覆盖 UI 缩放模式是“按屏幕大小缩放”。现在,当我以统一方式测试游戏时,HP 值以正确的默认值 20 正确完成。但每当我将项目构建到手机上时,我的 HP 值总是设置为零。每当我将它构建到我的手机时,如何修复我的 HP 值。
在 Unity 上进行的此测试 - HP 值“贪婪”绿色滑块设置为正确的值。
这是在手机上测试 - 值变为零
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour {
//HUD Variables
public Slider healthSlider;
void Start () {
bulan_fullhp = PlayerPrefs.GetInt ("Bulan_hp");
currHP = bulan_fullhp;
Debug.Log ("Bulan HP is : " + currHP);
healthSlider.maxValue = bulan_fullhp;
healthSlider.value = bulan_fullhp;
}
HP的默认值为20
解决方案
这是因为你一开始没有设置它的值,
PlayerPrefs.GetInt("Bulan_hp");
如果没有为“Bulan_hp”键设置值,则默认返回0。因此请确保通过以下代码检查该键是否存在。如果找不到密钥,则分配它。
if !(PlayerPrefs.HasKey("Bulan_hp"))
{
// You can set this value to playerprefs
PlayerPrefs.SetInt("Bulan_hp", 20);
}
// Then call get value
bulan_fullhp = PlayerPrefs.GetInt("Bulan_hp");
或者您可以通过此分配默认值..
bulan_fullhp = PlayerPrefs.GetInt("Bulan_hp", 20);
所以它不会是第一次为 0。
推荐阅读
- java - 基于动态值用Java编写文件
- c++ - 无法导入 poppler.h
- language-agnostic - 科技雇主更喜欢广泛而深入的理论知识?或者他们只是想要拥有 5 年最新流行语经验的人?
- node.js - 如何检查数据的集合是否存在于mongodb中
- ios - 如何在 IOS 设备上的裸工作流中测试我的 expo 应用程序?
- javascript - particle.js 重启或运行多个实例?
- matlab - 线性代数:删除行/行并使用减少的雅可比进行项目等价于首先使用完整的雅可比进行项目并在行/行之后删除?
- python - 如何处理 xml 中的 nillable 值?
- tensorflow - 拟合 tensorflow.keras 模型的问题
- python - 抓取市场数据