Sybaris
Topic Author
Posts: 23
Joined: 21 Feb 2019, 10:13

Using LoadXaml() to load non-noesis objects

15 May 2021, 17:58

Hi everybody,

from WPF I'm used to using XamlReader.Load() to load files that contain non-UI elements, like for example configuration files. The xaml format enables one to write pretty complex configuration files.
I tried this with LoadXaml() and it crashes Unity. Sometimes i get an error message saying that the "XAML file is empty" (which is not the case) and then it produces that message endlessly.
So it's obviously not possible to load XAMLs that do not contain UI elements, right?

Günther
 
User avatar
sfernandez
Site Admin
Posts: 2220
Joined: 22 Dec 2011, 19:20

Re: Using LoadXaml() to load non-noesis objects

17 May 2021, 10:31

You should be able to load any kind of data defined in your XAML files as long as it uses classes defined in the Unity project.

Do you have those xaml assets referenced in your Unity scene somehow? Because if the assets are not part of the scene they are not included in the game and cannot be loaded. As we mentioned in our Unity tutorial you can manually add Xaml.Dependencies to another XAML if you want, or you can reference those configuration files (xaml assets) in any component of your scene.

Please let me know if that was the cause of your problem?
 
Sybaris
Topic Author
Posts: 23
Joined: 21 Feb 2019, 10:13

Re: Using LoadXaml() to load non-noesis objects

17 May 2021, 10:37

Hi Fernandez,

yes, that was probably one cause of the problem. The other problem was that a file with only a root tag and two attributes didn't load and made Unity crash, although it loaded fine under WPF.
We cannot preload the file because it is a settings file that should be editable by the user before app start.
I switched to a simple XML file - that should do it for now.
But thanks for your response!

Günther
 
User avatar
sfernandez
Site Admin
Posts: 2220
Joined: 22 Dec 2011, 19:20

Re: Using LoadXaml() to load non-noesis objects

17 May 2021, 11:44

The other problem was that a file with only a root tag and two attributes didn't load and made Unity crash, although it loaded fine under WPF.
Could you please attach an example of this, we shouldn't crash Unity editor never and we want to fix it.
We cannot preload the file because it is a settings file that should be editable by the user before app start.
Understood. But in that case you will probably be loading the file using a simple File.Open, right? Then another option could be to use XamlReader.Parse() to parse the contents of the file.
 
Sybaris
Topic Author
Posts: 23
Joined: 21 Feb 2019, 10:13

Re: Using LoadXaml() to load non-noesis objects

17 May 2021, 11:49

I'll see if I can build a small example of the crashing file. I don't have it anymore because I deleted it... ;-)

We are using the XmlSerializer at the moment:
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            T newObject;

            using (var reader = File.OpenText(path))
            {
                newObject = (T)serializer.Deserialize(reader);
            }
I wasn't aware that ther is a XamlReader.Parse() method - maybe I'll try that.

Who is online

Users browsing this forum: No registered users and 2 guests