- Home /
Saving or Reading text file works on Mac but not on iOS
The title says it all, the saving and/or the reading of a text file named "points.txt" doesn't work as expected on iOS but work normally when I test the game on my Mac. I've nothing more to says. `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class PlesaPointSystem : MonoBehaviour {
public InputField inputf;
public Button button;
public RectTransform messageTr;
public Text DebugInfo;
public List<Vector2> Points;
public List<Transform> PointsTransform;
public List<string> PointsName;
public GameObject OriginalPoint;
public AudioSource asource;
public AudioClip sucessSound;
int NearestPointIndex = 0;
byte LOADINTERVAL = 0;
byte SOUNDUPDATE = 0;
const float PixelToMap = 0.01953125f;
public GoogleMap gm;
public void EndPointCreation () {
if(string.IsNullOrEmpty(inputf.text)) {
inputf.gameObject.SetActive(false);
button.gameObject.SetActive(true);
} else {
Points.Add(new Vector2(Input.location.lastData.longitude,Input.location.lastData.latitude));
PointsTransform.Add(((GameObject)Instantiate(OriginalPoint, new Vector3(0,0,0), Quaternion.identity)).transform);
PointsName.Add(inputf.text.Replace(",",""));
float scale = 1 << gm.zoom;
Vector2 worldCoordinate; // = projection(new Vector2(0,0));
Vector2 pixelCoordinate;
Vector2 playerWorldCoordinate = projection(gm.LatLong);
Vector2 playerPixelCoordinate = new Vector2(Mathf.Floor(playerWorldCoordinate.x * scale),Mathf.Floor(playerWorldCoordinate.y * scale));
for(int i = 0; i < Points.Count; i++) {
worldCoordinate = projection(Points.ToArray()[i]);
pixelCoordinate = new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));
pixelCoordinate = new Vector2(pixelCoordinate.x-playerPixelCoordinate.x, pixelCoordinate.y-playerPixelCoordinate.y);
PointsTransform.ToArray()[i].position = new Vector3(pixelCoordinate.x*PixelToMap, pixelCoordinate.y*PixelToMap,-0.1f);
}
inputf.gameObject.SetActive(false);
button.gameObject.SetActive(true);
Save();
}
}
public void CreateAPoint () {
button.gameObject.SetActive(false);
inputf.gameObject.SetActive(true);
inputf.text = "";
}
public void Save () {
StreamWriter sw = null;
if(File.Exists(Application.persistentDataPath+"/points.txt")) {
sw = File.CreateText(Application.persistentDataPath+"/points.txt");
}
if(sw!=null) {
sw.Close();
}
string Value = "";
for(int i = 0; i < Points.Count; i++) {
if(i!=0) {
Value+=",";
}
Value += "(" + Points.ToArray()[i].x + ":" + Points.ToArray()[i].x + ");"+PointsName.ToArray()[i];
}
File.WriteAllText(Application.persistentDataPath+"/points.txt", Value);
}
public void Load () {
//DebugInfo.text = Application.persistentDataPath.ToString();
Points.Clear();
PointsName.Clear();
foreach(RectTransform rt in PointsTransform) {
Destroy(rt.gameObject);
}
PointsTransform.Clear();
float scale = 1 << gm.zoom;
Vector2 worldCoordinate; // = projection(new Vector2(0,0));
Vector2 pixelCoordinate; //= new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));
Vector2 playerWorldCoordinate = projection(gm.LatLong);
Vector2 playerPixelCoordinate = new Vector2(Mathf.Floor(playerWorldCoordinate.x * scale),Mathf.Floor(playerWorldCoordinate.y * scale));
if(File.Exists(Application.persistentDataPath+"/points.txt")) {
string Value = File.ReadAllText(Application.persistentDataPath+"/points.txt");
if(string.IsNullOrEmpty(Value)) {
return;
}
for(int i = 0; i < Value.Split(',').Length; i++) {
Points.Add(new Vector2(float.Parse(Value.Split(',')[i].Split(';')[0].Replace("(","").Replace(")","").Split(':')[0]),float.Parse(Value.Split(',')[0].Split(';')[0].Replace("(","").Replace(")","").Split(':')[1])));
PointsName.Add(Value.Split(',')[i].Split(';')[1]);
PointsTransform.Add(((GameObject)Instantiate(OriginalPoint, new Vector3(0,0,0), Quaternion.identity)).transform);
worldCoordinate = projection(Points.ToArray()[i]);
pixelCoordinate = new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));
pixelCoordinate = new Vector2(pixelCoordinate.x-playerPixelCoordinate.x, pixelCoordinate.y-playerPixelCoordinate.y);
PointsTransform.ToArray()[i].position = new Vector3(pixelCoordinate.x*PixelToMap, pixelCoordinate.y*PixelToMap,-0.1f);
}
}
}
// Use this for initialization
void Start () {
gm = gm.GetComponent<GoogleMap>();
asource = asource.GetComponent<AudioSource>();
Input.location.Start(1f,1f);
Points = new List<Vector2>();
Load();
}
void OnApplicationQuit() {
Save();
}
// Update is called once per frame
void Update () {
if(Mathf.Abs(Input.acceleration.x)+Mathf.Abs(Input.acceleration.y)+Mathf.Abs(Input.acceleration.z)>=5.1f && asource.pitch>2.85f || Input.GetKeyDown(KeyCode.K)) {
asource.PlayOneShot(sucessSound);
StartCoroutine(ShowMessage("Congratulation! You found this point: " + PointsName.ToArray()[NearestPointIndex]));
}
if(LOADINTERVAL == 50) {
float ClosestDistance = Mathf.Infinity;
int index = 0;
float scale = 1 << gm.zoom;
Vector2 worldCoordinate; // = projection(new Vector2(0,0));
Vector2 pixelCoordinate; //= new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));
Vector2 playerWorldCoordinate = projection(gm.LatLong);
Vector2 playerPixelCoordinate = new Vector2(Mathf.Floor(playerWorldCoordinate.x * scale),Mathf.Floor(playerWorldCoordinate.y * scale));
for(int i = 0; i < Points.Count; i++) {
worldCoordinate = projection(Points.ToArray()[i]);
pixelCoordinate = new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));
pixelCoordinate = new Vector2(pixelCoordinate.x-playerPixelCoordinate.x, pixelCoordinate.y-playerPixelCoordinate.y);
PointsTransform.ToArray()[i].position = new Vector3(pixelCoordinate.x*PixelToMap, pixelCoordinate.y*PixelToMap,-0.1f);
if(Vector2.Distance(new Vector2(Input.location.lastData.longitude,Input.location.lastData.latitude), Points.ToArray()[i]) < ClosestDistance) {
ClosestDistance = Vector2.Distance(new Vector2(Input.location.lastData.longitude,Input.location.lastData.latitude),Points.ToArray()[i]);
index = i;
}
}
NearestPointIndex = index;
LOADINTERVAL = 0;
}
LOADINTERVAL++;
if(SOUNDUPDATE == 20) {
if(Points.Count != 0) {
float Distance = Vector2.Distance(new Vector2(Input.location.lastData.longitude,Input.location.lastData.latitude), Points.ToArray()[NearestPointIndex]);
if(Distance < 0.00100f) {
asource.pitch = (0.00100f-Distance)*3000f;
} else {
asource.pitch = 0;
}
}
SOUNDUPDATE = 0;
}
SOUNDUPDATE++;
}
Vector2 projection(Vector2 latLong) {
float siny = Mathf.Sin(latLong.y * Mathf.PI / 180);
// Truncating to 0.9999 effectively limits latitude to 89.189. This is
// about a third of a tile past the edge of the world tile.
siny = Mathf.Min(Mathf.Max(siny, -0.9999f), 0.9999f);
return new Vector2(gm.size * (0.5f + latLong.x / 360),gm.size * (0.5f - Mathf.Log((1 + siny) / (1 - siny)) / (4 * Mathf.PI)));
}
IEnumerator ShowMessage (string Message) {
if(messageTr.gameObject.activeInHierarchy == false) {
messageTr.gameObject.SetActive(true);
messageTr.GetChild(0).GetComponent<Text>().text = Message;
yield return new WaitForSeconds(2.8f);
messageTr.gameObject.SetActive(false);
}
yield break;
}
}
Answer by NGC6543 · Mar 30, 2017 at 02:24 AM
On the device, there isn't any file right after the build. So...
if(File.Exists(Application.persistentDataPath+"/points.txt")) {
sw = File.CreateText(Application.persistentDataPath+"/points.txt");
}
on Save() has no effect. Make sure you create the file first. Like this :
if(!File.Exists(Application.persistentDataPath+"/points.txt")) {
sw = File.CreateText(Application.persistentDataPath+"/points.txt");
}
$$anonymous$$aybe OnApplication Quit dosen't work on iOS void OnApplicationQuit() { Save(); }
$$anonymous$$aybe you can explicitly save the data before ter$$anonymous$$ating the program. Also you can add Debug.log() to OnApplicationQuit() to find out wether it is executed or not.
You might find more efficient way of saving data using Serialization. I tried to use binary serialization, but unity doesn't support one so I ended up using X$$anonymous$$L serialization.
The data is automatically saved when adding a point... WHY DID I NEED ONAPPLICATIONQUIT?
The problem: Value += "(" + Points.ToArray()[i].x + ":" + Points.ToArray()[i].x + ");"+PointsName.ToArray()[i];
The solution: Value += "(" + Points.ToArray()[i].x + ":" + Points.ToArray()[i].y + ");"+PointsName.ToArray()[i];
Your answer
Follow this Question
Related Questions
JSON not saving game object list 0 Answers
Save and (later) read scriptable objects 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
serialization stream supports seeking but its length is 0 0 Answers
How to Save and Load a list of Scriptable Objects from a File? 2 Answers