首页 > 解决方案 > 当我在 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

标签: unity3d

解决方案


这是因为你一开始没有设置它的值,

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。


推荐阅读