Page 1 of 1

Resource lookup process in Unity

Posted: 04 Jun 2021, 05:56
by KeldorKatarn
In WPF whenever a static resource is used, the process is like this:
  • The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.
  • The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.
  • Next, application resources are checked. Application resources are those resources within the resource dictionary that is defined by the Application object for your WPF application.
For the samples I'm writing I'd love to be able to have different Application Resources for each of my Scenes. Is there such a thing in Unity?
Setting the resources in the NoesisView.Content doesn't have the same behavior, as that wouldn't get inherited down into different UserControls as the logical tree traverse doesn't work like that, and also ContextMenues etc wouldn't inherit it. It's basically the same as setting the resources on the main window in a WPF app. It's not the behavior I'm looking for.

Is there such a thing as an additional NoesisView scoped lookup dictiionary and i've missed it? If not.. it's something I'd love to have...

Re: Resource lookup process in Unity

Posted: 04 Jun 2021, 11:01
by sfernandez
In Noesis we follow the same resource lookup that ends in the Application Resources dictionary.

But that dictionary is unique for the application and can only be set once in Unity. Inside the editor the Application Resources dictionary is always loaded to correctly find resources when importing other xamls.

This is something we've been revisiting many times to allow having a different application dictionary for each scene, although we always find problems that lead us to the current approach. We will be glad to hear some opinions and ideas on this.

Re: Resource lookup process in Unity

Posted: 04 Jun 2021, 17:26
by KeldorKatarn
What about a property on the Noesis view where I can attach a ResourceDictionary.xaml, and those resources will be merged into the application resources on scene load, but also removed from it again on scene unload? Would that work?

In a final application I think I could live with just one, although a per scene one would still be immensely useful. But especially for demonstration sample scenes it's a bit of a pain in the butt since yyou can always only have one default style for things. Sure I can override those with different default styles in a UserControl but those don't penetrate into other ControlTemplates like ApplicationResources do.

Unity is just very different from WPF. in WPF you only have one app, in Unity you have multiple setups for Noesis including in the case of my framework multiple bootstrappers

Re: Resource lookup process in Unity

Posted: 08 Jun 2021, 05:56
by KeldorKatarn
Actually, come to think of it.... couldn't I make a serialized field on my bootstrapper monobehavior that hold a NoesisXAML ResourceDictionary (like your settings asset for the application resources), then, when the boostrapper starts up, add that dictionary to the applicationresources.mergeddictionaries collection and when the Boostrapper is shut down remove that dictionary again? I've never actually done that, will that cause issues or will the lookup process be handled smoothly? Is adding to a resourcedictionary causing dynamic resources etc to be re-evaluated? In that case wouldn't this also be a kind of way to localize things? Just switch out dictionaries in the mergeddictionaries of the applicationresources?

Also.. do I need to load the file by hand to accomplish this with XamlReader.Load, or can I somehow use the NoesisXaml asset to add the dictionary to the resources?

Re: Resource lookup process in Unity

Posted: 08 Jun 2021, 11:07
by sfernandez
The problem is with the xamls that rely on static resources provided by those scene dictionaries. When the NoesisPostProcessor parses the xaml it will generate errors because of missing resources.
Also.. do I need to load the file by hand to accomplish this with XamlReader.Load, or can I somehow use the NoesisXaml asset to add the dictionary to the resources?
You can use the NoesisXaml to load itself, it provides a public Load() method for that. Then you can add it to the application resources:
NoesisXaml dict = ...;
ResourceDictionary app = Noesis.GUI.GetApplicationResources();
app.MergedDictionaries.Add((ResourceDictionary)dict.Load());

Re: Resource lookup process in Unity

Posted: 08 Jun 2021, 11:17
by jsantos
I definitely agree that each View should have a resource dictionary merged when the View is loaded. We need to find a mechanism to properly solve this, because as Sergio said, right now, each XAML is independently imported and checked against errors.

We could, for example, avoid parsing XAMLs at edit time and just do it when loading the xaml. If there are errors in the XAML you will see them at play time instead.

What do you think?

Re: Resource lookup process in Unity

Posted: 08 Jun 2021, 12:52
by Faerdan
If adding XAML parsing at load time (which I like in concept), please make it an option (in Noesis Settings). I don't want to add XAML parsing to UI loading times on mobile.

Re: Resource lookup process in Unity

Posted: 08 Jun 2021, 17:27
by KeldorKatarn
Couldn't we add something to the settings somehow to indicate which Xamls depend on such a dictionary or something?
I mean right now the global applicationdictionary XAML from the settings is obviously used to verify the XAML staticresources.

Couldn't I we specify the other scene dictionaries there as well and somehow map xamls to each dictionary, or alternatively just use each of those dictionaries as a possible verification and if one works it's fine, and if during playtime the wrong one is loaded you see the errors in playtime.

Maybe something along those lines...

Re: Resource lookup process in Unity

Posted: 09 Jun 2021, 14:30
by jsantos
More thoughts, in Unity we provide previews for every XAML, those previews are happening outside any view. If we have dictionaries in the View, the previews won't work correctly.

So maybe having a way to specify a dictionary per folder, similar to what you are proposing @KeldorKatarn could work. But I see a lot of pain in the implementation. We need to track when folders are started to be used to merge their dictionaries. Merging dictionaries from different folders to the global dictionary can create conflicts so we need a way to unload them.

Re: Resource lookup process in Unity

Posted: 10 Jun 2021, 06:45
by KeldorKatarn
Hmm. Not sure if that's a great idea with the folders. That would force a folder structure. Here's a suggestion though. What about an extension in XAML so we can provide a resource dictionary dependency in XAML. WPF would ignore it while you can parse it for both the preview and the correctness check. It's up to the user to make sure that resource dicitonary is later also used.