Allow viewing/editing VM globals in Unity Editor

This commit is contained in:
Nahuel Rocchetti 2024-07-09 22:53:29 -03:00
parent 25763a9bb9
commit 406c3f27b9
2 changed files with 39 additions and 9 deletions

View file

@ -1,6 +1,7 @@
using OpenTS2.Game;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -12,6 +13,8 @@ namespace OpenTS2
[CustomEditor(typeof(Simulator))]
public class SimulatorEditor : Editor
{
private bool _showGlobals = false;
private bool _showEntities = false;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
@ -20,19 +23,45 @@ namespace OpenTS2
if (vm == null) return;
var entities = vm.Entities;
if (entities == null) return;
GUILayout.Label("Entities");
_showGlobals = EditorGUILayout.Foldout(_showGlobals, "Globals");
if (_showGlobals)
{
EditorGUI.indentLevel++;
for (var i = 0; i < vm.GlobalState.Length; i++)
{
GUILayout.BeginVertical("box");
var globalName = ((SimAntics.VMGlobals)i).ToString();
GUILayout.BeginHorizontal();
var editedGlobal = EditorGUILayout.TextField(globalName, vm.GlobalState[i].ToString(CultureInfo.InvariantCulture));
if (short.TryParse(editedGlobal,NumberStyles.Integer, CultureInfo.InvariantCulture, out var res))
{
if (res != vm.GlobalState[i])
vm.SetGlobal((ushort)i, res);
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
EditorGUI.indentLevel--;
}
_showEntities = EditorGUILayout.Foldout(_showEntities, "Entities");
if (_showEntities)
{
EditorGUI.indentLevel++;
foreach (var entity in entities)
{
GUILayout.BeginVertical("box");
GUILayout.Label($"{entity.ID} - {entity.ObjectDefinition.FileName}");
GUILayout.EndVertical();
EditorGUILayout.Separator();
if (GUILayout.Button("Kill"))
{
entity.Delete();
}
}
GUILayout.EndVertical();
}
EditorGUI.indentLevel--;
}
}
}
}

View file

@ -57,6 +57,7 @@ namespace OpenTS2.SimAntics
SetGlobal(VMGlobals.GameEditionFlags2, epFlags2);
var globals = GameGlobals.Instance;
SetGlobal(VMGlobals.CurrentLanguage, (short)globals.Language);
SetGlobal(VMGlobals.Year, 1997);
}
/// <summary>