BrandonReinhart
Topic Author
Posts: 29
Joined: 10 Jan 2025, 17:02

Correct pattern for initializing components in Unity?

28 Aug 2025, 18:10

I sometimes foot-gun myself with component initialization in the code-behind constructor on Unity. I'm curious if there's a better pattern documented that I missed.

Let's say I have a control that does this:
        public MaskedSprite()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            NoesisUnity.LoadComponent(this, "MaskedSprite");

#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
                return;
#endif

            _spriteRect = FindName("SpriteRect") as Rectangle;
            _imageBrush = FindName("SpriteBrush") as ImageBrush;
            _maskShader = FindName("MaskShader") as MaskedSpriteBrush;

            LoadAtlas();
            UpdateSprites();
        }
Because controls get constructed in the editor even when Preview Enabled is false, the rest of this code will run. LoadAtlas and UpdateSprites try to access various Addressables that aren't available when the game isn't loaded, creating all sorts of static havoc internal to Noesis. I've had it completely hose my system until I reboot. The problem is likely due to Noesis trying to bind some incorrectly setup texture - but it isn't super important because it shouldn't ever happen. It just isn't code I want to run when not in the game.

I have the UNITY_EDITOR define there to short circuit the init process, which fixes things. The foot-gun is if I write code that Noesis tries to run in the editor and I forget to add a define like this.

Is there a better component initialization pattern I can adopt?
 
BrandonReinhart
Topic Author
Posts: 29
Joined: 10 Jan 2025, 17:02

Re: Correct pattern for initializing components in Unity?

28 Aug 2025, 18:37

I also reported a couple minor issues with the noesis shader compiler on Linux:

https://www.noesisengine.com/bugs/view.php?id=4359
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Correct pattern for initializing components in Unity?

28 Aug 2025, 18:55

We are using this pattern with #if UNITY_5_3_OR_NEWER for detecting Blend vs Noesis, as shown in our samples. For example:
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System;
using System.Windows;
using System.Windows.Controls;
#endif

namespace Buttons
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : UserControl
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

#if NOESIS
        private void InitializeComponent()
        {
            NoesisUnity.LoadComponent(this);
        }
#endif
    }
}
Note that this will no longer be necessary once Studio is available.

Apart from that pattern, there isn’t a strict rule for how to handle other cases. The plugin (and Studio in the future) constructs your classes for previews, so you need to make sure this is always possible by excluding anything that should only run in-game.

If you have any suggestions on how this could be improved, I’d be glad to hear them.
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Correct pattern for initializing components in Unity?

28 Aug 2025, 18:55

I also reported a couple minor issues with the noesis shader compiler on Linux:
Thank you for this!

Who is online

Users browsing this forum: Bing [Bot] and 8 guests