所以在我的应用程序中,我通过一个名为datacontroller的游戏对象来完成我的三个场景。持久场景是一个空场景,菜单屏幕场景,然后是游戏场景。我的应用程序在我的计算机和编辑器模式下完美运行,但当我下载apk到我的Android平板电脑它不再有效!我读过这可能与我的对象代码有关,但我不认为我写的只能在编辑器中运行。
enter code here
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.IO; // The System.IO namespace contains functions related to loading and saving
files
public class DataController : MonoBehaviour
{
private RoundData[] allRoundData;
private PlayerProgress playerProgress;
private string gameDataFileName = "data.json";
void Start()
{
DontDestroyOnLoad(gameObject);
LoadGameData();
LoadPlayerProgress();
SceneManager.LoadScene("MenuScreen");
}
public RoundData GetCurrentRoundData()
{
// If we wanted to return different rounds, we could do that here
// We could store an int representing the current round index in PlayerProgress
return allRoundData[0];
}
public void SubmitNewPlayerScore(int newScore)
{
// If newScore is greater than playerProgress.highestScore, update playerProgress with the new value and call SavePlayerProgress()
if (newScore > playerProgress.highestScore)
{
playerProgress.highestScore = newScore;
SavePlayerProgress();
}
}
public int GetHighestPlayerScore()
{
return playerProgress.highestScore;
}
private void LoadGameData()
{
// Path.Combine combines strings into a file path
// Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build
string filePath = Path.Combine(Application.streamingAssetsPath, gameDataFileName);
if (File.Exists(filePath))
{
// Read the json from the file into a string
string dataAsJson = File.ReadAllText(filePath);
// Pass the json to JsonUtility, and tell it to create a GameData object from it
GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);
// Retrieve the allRoundData property of loadedData
allRoundData = loadedData.allRoundData;
}
else
{
Debug.LogError("Cannot load game data!");
}
}
// This function could be extended easily to handle any additional data we wanted to store in our PlayerProgress object
private void LoadPlayerProgress()
{
// Create a new PlayerProgress object
playerProgress = new PlayerProgress();
// If PlayerPrefs contains a key called "highestScore", set the value of playerProgress.highestScore using the value associated with that key
if (PlayerPrefs.HasKey("highestScore"))
{
playerProgress.highestScore = PlayerPrefs.GetInt("highestScore");
}
}
// This function could be extended easily to handle any additional data we wanted to store in our PlayerProgress object
private void SavePlayerProgress()
{
// Save the value playerProgress.highestScore to PlayerPrefs, with a key of "highestScore"
PlayerPrefs.SetInt("highestScore", playerProgress.highestScore);
}
}
答案 0 :(得分:0)
我开始自己通过统一教程,所以我不是专家。 :)
但我会尝试的是第一件事。 using System.IO;
不确定这是否可以在android上获取文件,因为android具有不同的文件结构。因此,我首先删除它并对文件路径进行硬编码或使用System.IO类注释掉代码,然后在统一中重新编译apk并检查它是否有效。我也看过这篇文章:http://answers.unity3d.com/questions/1023391/systemio-dont-work-on-android.html
如果这不起作用我会评论功能并编译apk并检查它是否工作,如果它没有评论更多的代码,直到你找到导致它在Android上的错误的行或代码。这种方法需要很长时间才能排除故障或导致代码导致问题,但对我来说这已经成功了。
我猜是因为它正在你的pc上工作它的类或其引用的东西在android中不可用。
如果您弄清楚代码的哪一部分,请分享您的发现。因为我也想知道阻止我这样做。 :)
答案 1 :(得分:0)
避免在Android上使用System.IO,一般在Unity上使用。
请改为使用“资源”,只需执行以下步骤:
var file = Resources.Load("filename_here") as TextAsset;
Debug.Log(file.text)