Fix .NET unloading issue

main
copygirl 2 months ago
parent a2cca8165b
commit 3fa91b5954
  1. 2
      addons/terrain-editing/TerrainEditingControls+Editing.cs
  2. 26
      addons/terrain-editing/TerrainEditingControls.cs
  3. 19
      addons/terrain-editing/TerrainEditingPlugin.cs

@ -6,7 +6,7 @@ public partial class TerrainEditingControls
(TilePos, TilePos)? _selection = null; (TilePos, TilePos)? _selection = null;
Material _editToolMaterial; Material _editToolMaterial;
void OnEditingReady() public override void _EnterTree()
{ {
_editToolMaterial = new StandardMaterial3D { _editToolMaterial = new StandardMaterial3D {
VertexColorUseAsAlbedo = true, VertexColorUseAsAlbedo = true,

@ -5,12 +5,11 @@ public partial class TerrainEditingControls
public (ToolMode , Button)[] ToolModeButtons { get; private set; } public (ToolMode , Button)[] ToolModeButtons { get; private set; }
public (ToolShape, Button)[] ToolShapeButtons { get; private set; } public (ToolShape, Button)[] ToolShapeButtons { get; private set; }
public Button[] PaintTextureButtons { get; private set; } public Button[] PaintTextureButtons { get; private set; }
public Slider DrawSizeSlider { get; private set; }
public Slider DrawSizeSlider { get; private set; } ToolMode _toolMode = ToolMode.Height;
ToolShape _toolShape = ToolShape.Circle;
ToolMode _toolMode; int _texture = 1;
ToolShape _toolShape;
int _texture;
public ToolMode ToolMode { get => _toolMode ; set => SetToolMode (value); } public ToolMode ToolMode { get => _toolMode ; set => SetToolMode (value); }
public ToolShape ToolShape { get => _toolShape; set => SetToolShape(value); } public ToolShape ToolShape { get => _toolShape; set => SetToolShape(value); }
@ -52,16 +51,17 @@ public partial class TerrainEditingControls
DrawSizeSlider = GetNode<Slider>("SizeSlider"); DrawSizeSlider = GetNode<Slider>("SizeSlider");
DrawSizeSlider.ValueChanged += (_) => drawSizeLabel.Text = $"{DrawSize}"; DrawSizeSlider.ValueChanged += (_) => drawSizeLabel.Text = $"{DrawSize}";
SetToolMode(ToolMode.Height); SetToolMode(_toolMode);
SetToolShape(ToolShape.Circle); SetToolShape(_toolShape);
SetTexture(1); SetTexture(_texture);
OnEditingReady();
} }
void SetToolMode(ToolMode value) void SetToolMode(ToolMode value)
{ {
// Ignore if _Ready hasn't been called yet.
if (ToolModeButtons == null) return;
foreach (var (mode, button) in ToolModeButtons) foreach (var (mode, button) in ToolModeButtons)
button.Flat = button.ButtonPressed = value != mode; button.Flat = button.ButtonPressed = value != mode;
foreach (var button in PaintTextureButtons) foreach (var button in PaintTextureButtons)
@ -71,6 +71,9 @@ public partial class TerrainEditingControls
void SetToolShape(ToolShape value) void SetToolShape(ToolShape value)
{ {
// Ignore if _Ready hasn't been called yet.
if (ToolShapeButtons == null) return;
foreach (var (shape, button) in ToolShapeButtons) foreach (var (shape, button) in ToolShapeButtons)
button.Flat = button.ButtonPressed = value != shape; button.Flat = button.ButtonPressed = value != shape;
DrawSizeSlider.Editable = value != ToolShape.Corner; DrawSizeSlider.Editable = value != ToolShape.Corner;
@ -79,6 +82,9 @@ public partial class TerrainEditingControls
void SetTexture(int value) void SetTexture(int value)
{ {
// Ignore if _Ready hasn't been called yet.
if (PaintTextureButtons == null) return;
if ((value < 1) || (value > PaintTextureButtons.Length)) if ((value < 1) || (value > PaintTextureButtons.Length))
throw new ArgumentOutOfRangeException(nameof(value)); throw new ArgumentOutOfRangeException(nameof(value));
foreach (var (i, button) in PaintTextureButtons.Select((b, i) => (i, b))) foreach (var (i, button) in PaintTextureButtons.Select((b, i) => (i, b)))

@ -1,9 +1,13 @@
#if TOOLS #if TOOLS
[Tool] [Tool]
public partial class TerrainEditingPlugin public partial class TerrainEditingPlugin
: EditorPlugin : EditorPlugin
{ {
const string ScenePath = "res://addons/terrain-editing/TerrainEditingControls.tscn";
// The container to which the editing controls get added to when Terrain is selected.
const CustomControlContainer Container = CustomControlContainer.SpatialEditorSideRight;
TerrainEditingControls _controls; TerrainEditingControls _controls;
public override bool _Handles(GodotObject obj) public override bool _Handles(GodotObject obj)
@ -11,24 +15,25 @@ public partial class TerrainEditingPlugin
public override void _EnterTree() public override void _EnterTree()
{ {
var scene = GD.Load<PackedScene>("res://addons/terrain-editing/TerrainEditingControls.tscn"); var scene = GD.Load<PackedScene>(ScenePath);
_controls = scene.Instantiate<TerrainEditingControls>(); _controls = scene.Instantiate<TerrainEditingControls>();
} }
public override void _ExitTree() public override void _ExitTree()
{ {
if (_controls == null) return; if (_controls == null) return;
_controls.QueueFree(); if (_controls.GetParent() != null)
RemoveControlFromContainer(Container, _controls);
_controls.Free();
_controls = null; _controls = null;
} }
public override void _MakeVisible(bool visible) public override void _MakeVisible(bool visible)
{ {
var container = CustomControlContainer.SpatialEditorSideRight;
if (visible) if (visible)
AddControlToContainer(container, _controls); AddControlToContainer(Container, _controls);
else if (_controls != null) else if (_controls.GetParent() != null)
RemoveControlFromContainer(container, _controls); RemoveControlFromContainer(Container, _controls);
} }
} }
#endif #endif

Loading…
Cancel
Save