Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by StuwuStudio · Mar 30, 2017 at 01:37 AM · iossave dataloading filefilestreamtextfile

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;
     }
 
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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");
          }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image StuwuStudio · Mar 30, 2017 at 11:36 AM 0
Share

Oh! Thank you! I didn't notice the error I made!

avatar image StuwuStudio · Mar 30, 2017 at 11:58 AM 0
Share

Eh what? It didn't work.

avatar image StuwuStudio · Mar 30, 2017 at 11:59 AM 0
Share

$$anonymous$$aybe OnApplication Quit dosen't work on iOS void OnApplicationQuit() { Save(); }

avatar image NGC6543 StuwuStudio · Mar 31, 2017 at 03:20 AM 0
Share

$$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.

avatar image StuwuStudio NGC6543 · Mar 31, 2017 at 11:39 AM 0
Share

The data is automatically saved when adding a point... WHY DID I NEED ONAPPLICATIONQUIT?

Show more comments
avatar image StuwuStudio StuwuStudio · Apr 01, 2017 at 12:08 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

85 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges