2017-05-03 12 views
0

私のアプリでは、私の3つのシーンからdatacontrollerというゲームオブジェクトを渡します。永続的なシーンは、空のシーン、menuscreenシーン、そしてゲームシーンです。私のアプリケーションは私のコンピュータとエディタモードでは完璧に動作しますが、私のアンドロイドタブレットにapkをダウンロードするともう機能しません!これは私のオブジェクトのために私のコードと関係があるかもしれませんが、私はエディタでしか動かないものを書いたとは思いません。unity android gameobjectが動作しない

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;アンドロイドのファイル構造が異なるため、アンドロイド上でファイルを取得できるかどうかは不明です。だから私はまずそれを削除し、ハードコードのファイルパスを並べ替えるか、System.IOクラスを使用してコードをコメントアウトしてから、apkを1つに再コンパイルし、動作するかどうかを確認します。 http://answers.unity3d.com/questions/1023391/systemio-dont-work-on-android.html

これがうまくいかなかった場合、私は機能をコメントアウトし、apkをコンパイルして、それがエラーである行またはコードを見つけるまでコードのコメントを残していないかどうかを確認しますアンドロイド。このメソッドは、トラブルシューティングや問題の原因となるコードの取得に時間がかかりますが、私にとってこれはこれまでに働いていました。

私はそれがあなたのPC上でクラスや何かを参照していることを推測していますが、それはアンドロイドでは利用できません。

コードのどの部分がそれをしているか把握している場合は、結果をお知らせください。私がそれをやるのを防ぐためにも知りたいと思うように。 :)

0

Androidおよび一般的にUnityでSystem.IOを使用しないでください。

使用「リソースは」代わりに、ただ次はこれが手順:

  • それに
  • 移動JSONファイル「リソース」というフォルダを作成し、.txtの
  • 使用このコードの中に名前を変更文字列を取得します。
var file = Resources.Load("filename_here") as TextAsset; 
Debug.Log(file.text) 
関連する問題