using UnityEngine;
using UnityEngine.UIElements;
namespace Uigenx
{
///
/// Runtime configuration for the package. Lives in a Resources folder so a player build can
/// load it without a scene reference. Never holds a token or any other secret.
///
[CreateAssetMenu(menuName = "UI Creator/Settings", fileName = "UgxSettings")]
public sealed class UgxSettings : ScriptableObject
{
public const string ResourcePath = "UgxSettings";
public const string DefaultDaemonUrl = "ws://127.0.0.1:47800";
public const float DefaultDataStreamHz = 20f;
[Header("Studio bridge")]
[Tooltip("Connect to the UI Creator daemon in the editor and in development builds.")]
[SerializeField] bool bridgeEnabled = true;
[SerializeField] string daemonUrl = DefaultDaemonUrl;
[Tooltip("Upper bound on live data updates sent to the studio, per second.")]
[Range(1f, 120f)]
[SerializeField] float dataStreamHz = DefaultDataStreamHz;
[Header("Panels")]
[SerializeField] PanelSettings defaultPanelSettings;
[SerializeField] UgxScreenRegistry screenRegistry;
static UgxSettings _instance;
static UgxSettings _fallback;
static bool _resolved;
public bool BridgeEnabled => bridgeEnabled;
public string DaemonUrl => string.IsNullOrEmpty(daemonUrl) ? DefaultDaemonUrl : daemonUrl;
public float DataStreamHz => dataStreamHz <= 0f ? DefaultDataStreamHz : dataStreamHz;
public PanelSettings DefaultPanelSettings => defaultPanelSettings;
public UgxScreenRegistry ScreenRegistry => screenRegistry;
/// Null when the project has no settings asset. Callers must handle that.
public static UgxSettings InstanceOrNull
{
get
{
if (_resolved) return _instance;
_resolved = true;
_instance = Resources.Load(ResourcePath);
return _instance;
}
}
///
/// Never null. Falls back to an in memory asset with the documented defaults, kept in its
/// own field so that asking for Instance never makes InstanceOrNull start returning
/// non null: callers branch on that null to decide whether the project is configured.
///
public static UgxSettings Instance
{
get
{
var found = InstanceOrNull;
if (found != null) return found;
if (_fallback == null)
{
_fallback = CreateInstance();
_fallback.name = "UgxSettings (defaults)";
_fallback.hideFlags = HideFlags.HideAndDontSave;
}
return _fallback;
}
}
/// Used by tests and by the editor after the asset is created or moved.
public static void Override(UgxSettings settings)
{
_instance = settings;
_resolved = true;
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetOnLoad()
{
_instance = null;
_fallback = null;
_resolved = false;
}
}
}