-
- BrandonReinhart
- Posts: 29
- Joined:
Correct pattern for initializing components in Unity?
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:
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?
Let's say I have a control that does this:
Code: Select all
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();
}
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
- Posts: 29
- Joined:
Re: Correct pattern for initializing components in Unity?
I also reported a couple minor issues with the noesis shader compiler on Linux:
https://www.noesisengine.com/bugs/view.php?id=4359
https://www.noesisengine.com/bugs/view.php?id=4359
Re: Correct pattern for initializing components in Unity?
We are using this pattern with #if UNITY_5_3_OR_NEWER for detecting Blend vs Noesis, as shown in our samples. For example:
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.
Code: Select all
#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
}
}
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.
Re: Correct pattern for initializing components in Unity?
Thank you for this!I also reported a couple minor issues with the noesis shader compiler on Linux:
Who is online
Users browsing this forum: Bing [Bot] and 8 guests