Quantcast
Channel: Questions in topic: "ios"
Viewing all articles
Browse latest Browse all 4709

Saving or Reading text file works on Mac but not on iOS

$
0
0
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 Points; public List PointsTransform; public List 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(); asource = asource.GetComponent(); Input.location.Start(1f,1f); Points = new List(); 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 = Message; yield return new WaitForSeconds(2.8f); messageTr.gameObject.SetActive(false); } yield break; } }

Viewing all articles
Browse latest Browse all 4709

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>