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
7
Question by adam michaels · Dec 20, 2009 at 07:33 AM · assetserializationxml

How to use XML Serialization to load custom asset types

We have custom assets that contain data that serializes very well to XML (string-based) using the standard .NET XmlSerialization methods. I can make this work very well manually, within Unity Script code (put into plugin assemblies that I have dropped into "Assets/Plugins")... but this is not making use of "Resources.Load(path, Type)". It seems that your examples for using this method only reference built-in Unity types (Texture2D, TextAsset, etc).

How do I register my own type (e.g. "XML") to work with this? I want to make sure this all works well in the Web Player, which isn't going to fly if I'm doing direct System.IO to the file system. I need my XML files to be stored under Resources and to be packaged with the Web Player deployment, and allow me to deserialize my XML files from in this mode of deployment.

Got any good advice?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by Lucas Meijer 1 · Dec 20, 2009 at 10:33 PM

I would approach this by making a

using UnityEngine;
public class XMLContains : ScriptableObject
{
    public string theXML;
}

By making your class enherit from ScriptableObject, it becomes an "Asset Type" to unity, in the sense that you can store them on disk, and use the Unity IDE interface to drag it from the assetsfolder to a behaviour or whatever. It also automatically plays nice with Unity's dependency tracking system that decides what does and what does not go into a webplayer. Going a bit further with this example, you could have let's say a TieFighter class, actually get some of its information (max health, max speed, acceleration), from your xml file, by making the TieFighter class look like this:

using UnityEngine; public class TieFighter : MonoBehaviour { public XMLContainer myXMLSettings;

  void Awake()
  {
     //parse myXMLSettings.theXML into reasonable data
  }

  void Update()
  {
     //use the reasonable data
  }

}

You could then attach the TieFighter script to a gameobject, and then drag your xml-asset from your assetsfolder onto the myXMLSettings public field.

Please note that this example is much better at showing how you could use a ScriptableObject than how to properly implement a TieFighter, as I would never really have that parse an actual xml file on runtime. Tiefighters are too cool for xml anyway.

Comment
Add comment · 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
1

Answer by $$anonymous$$ · Feb 28, 2011 at 07:17 PM

You can start by this method and make adaptations as needed. (Link)

Comment
Add comment · 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
0

Answer by Ashkan_gc · Dec 20, 2009 at 12:08 PM

this problem is something that should be fixed in future versions of unity i think. you have two methods: create asset bundles with your XML files and load them with www class. www can get file system paths like this "file://path". the other way is to put the XML in a URL and load it with WWW class. also you can put the xml file as a text asset in your unity application and then easily work with it.

Comment
Add comment · 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

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

[SCREENSHOTS]Save scenes using XML/json descriptor 0 Answers

ScriptableObject+XmlSerializer=sad 2 Answers

Questions about XML serialization of Class Properties 1 Answer

[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer

URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer


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