2017-05-03 62 views
0

因此,在我的应用程序中,我传递了一个游戏对象,称为datacontroller通过我的三个场景。持久的场景是一个空的场景,menuscreen场景,然后是游戏场景。我的应用程序在我的电脑和编辑器模式下完美工作,但是当我将apk下载到我的Android平板电脑时,它不再有效! iv'e阅读这可能与我的代码对我的对象,但我不认为我写了任何只能在编辑器中工作的代码。unity android gameobject not working

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

我开始通过统一的教程去自己,所以我不是专家。 :)

但我会尝试的是第一件事。 using System.IO;不知道这是否会在android上获取文件,因为android具有不同的文件结构。所以我首先将它移除并对文件路径进行硬编码,或使用System.IO类注释掉代码,然后重新编译apk并检查它是否有效。我也看到这个职位:http://answers.unity3d.com/questions/1023391/systemio-dont-work-on-android.html

如果这没有奏效,我会评论功能和编译apk,并检查它的工作,如果它没有评论更多的代码,直到你找到行或导致它错误的代码机器人。这种方法需要很长时间来解决问题或让你的代码导致问题,但对我来说这已经工作过。

我猜测,因为它正在处理你的电脑它的类或其引用,它不适用于Android的东西。

请分享你的发现,如果你找出代码的哪一部分做到这一点。正如我也想知道阻止我这样做。 :)

0

避免在Android上使用System.IO,一般情况下在Unity上使用。

使用“资源”来代替,只是跟着这个步骤:

  • 就可以创建一个名为“资源”
  • 移动JSON文件夹和文件后缀名为.txt
  • 使用此代码来重命名获取字符串:
var file = Resources.Load("filename_here") as TextAsset; 
Debug.Log(file.text)