Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I get Resource path on iOS devices?

Discussion in 'Scripting' started by ChangJoo-Park, Jun 21, 2016.

  1. ChangJoo-Park

    ChangJoo-Park

    Joined:
    Jul 6, 2015
    Posts:
    2
    Hello I use xml for databse.
    xml files are in Assets/Resources/

    public static CourseContainer Load (string path)
    {
    TextAsset _xml = Resources.Load <TextAsset > (path);
    XmlSerializer serializer = new XmlSerializer (typeof(CourseContainer));
    StringReader reader = new StringReader (_xml.text);
    CourseContainer courses = serializer.Deserialize (reader) as CourseContainer;
    reader.Close ();
    return courses;
    }

    It works well, path is Assets/Resources/course.xml
    but save can't. I can't find Resource folder in iOS device

    public static void Save ()
    {
    CourseContainer courses = GlobalVariableManager.courseContainer;

    #if UNITY_IOS
    XmlSerializer serializer = new XmlSerializer (typeof(CourseContainer));
    string filePath = System.IO.Path.Combine(Application.persistentDataPath, "courses.xml");
    FileStream stream = new FileStream (filePath, FileMode.Truncate);
    serializer.Serialize (stream, courses);
    stream.Close ();
    #endif
    }

    I want know where is my xml file in iOS devices.
     
  2. ChangJoo-Park

    ChangJoo-Park

    Joined:
    Jul 6, 2015
    Posts:
    2
  3. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Use code tags when posting in the future.

    Your problem is most probably here:
    Code (csharp):
    1. FileStream stream = new FileStream (filePath, FileMode.Truncate);
    FileMode.Truncate tells FileStream to throw an exception if the file you're attempting to open doesn't already exist. Change it to FileMode.Create:
    Code (csharp):
    1. FileStream stream = new FileStream (filePath, FileMode.Create);
    I think you're issue is that you're making an incorrect assumption. You can read files from the Resources folder, but you can't write to it at runtime. You're reading the file from somewhere and writing to a completely different location.[/code]