Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Matt 16 · Jan 20, 2011 at 10:38 AM · webplayerfile-io

Accessing arbitrary files within a web build

Is it possible to access files within the unity3d package from scripts using standard file IO? Specifically what I'm looking to do is open an xml settings file and deserialize an object from it. I do realize it's possible to dump the xml on the server with the unity3d file, but I would much prefer to have it stored within the package.

Essentially I'm looking to do the following, with the file access being the missing piece:

        Stream stream = null;
        MyObject myObj = null;
        try
        {
            stream = DoSomeUnityMagic();
            myObj = serializer.Deserialize(stream);
        } 
        catch (Exception)
        {
            Debug.Log("Failed to load object");
        }
        finally
        {
            if (null != stream)
            {
                stream.Close();
            }
        }

For anyone that comes hunting, the full solution is something like this:

    MyObject myObj = null;
    string resourcePath = "path/to/resource"; // Excluding extension, as usual
    try
    {
        TextAsset myAsset = (TextAsset)Resources.Load(resourcePath, typeof(TextAsset));
        Byte[] bytes = Encoding.UTF8.GetBytes(myAsset .text);
        using (MemoryStream stream = new MemoryStream(bytes))
        {
            myObj = (MyObject)(new XmlSerializer(typeof(MyObject))).Deserialize(stream);
        }
    }
    catch (Exception ex)
    {
        Debug.Log("Failed to deserialize data from resource " + resourcePath + " (" + ex.Message + ")");
        Debug.Log(ex.StackTrace);
    }

Also worth noting is that the web player doesn't support the default encoding which .Net serialization spits out, so when serializing an object it's important to set the encoding:

    SerializeTestClass test = new SerializeTestClass();
    test.Text = "Booyah from xml";
    string path = "xmlfile.xml";
    try
    {
        using (XmlTextWriter writer = new XmlTextWriter(File.Create(path), Encoding.UTF8))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(SerializeTestClass));
            serializer.Serialize(writer, test);
        }
    }
    catch (Exception) { } 

Comment
Add comment · Show 2
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 yoyo · Jan 20, 2011 at 06:59 PM 0
Share

If you need the data to change over time (you called it a "settings file") then you should look at PlayerPrefs. http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

avatar image Matt 16 · Jan 21, 2011 at 11:04 AM 0
Share

Actually Eric's answer was exactly what I needed, these aren't user settings, but trained AI settings that change often enough to be a pain to update in the inspector, but won't change within the build itself. Thanks for the suggestion though.

1 Reply

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

Answer by Eric5h5 · Jan 20, 2011 at 10:50 AM

There's no file I/O, but you can use a TextAsset.

Comment
Add comment · Show 1 · 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 Matt 16 · Jan 20, 2011 at 01:40 PM 0
Share

Awesome, thanks this was just what I needed.

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

No one has followed this question yet.

Related Questions

File manipulations in the unity web player. 1 Answer

WebPlayer call a server php script issue 1 Answer

File I/O access from Chrome Native Client 3 Answers

Read and Write to text file Online 4 Answers

How do I let the user select a local file for my application to use? 5 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