-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathMLAgentsSettingsManager.cs
90 lines (82 loc) · 2.29 KB
/
MLAgentsSettingsManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Unity.MLAgents
{
#if UNITY_EDITOR
[InitializeOnLoad]
#endif
internal static class MLAgentsSettingsManager
{
internal static event Action OnSettingsChange;
internal const string EditorBuildSettingsConfigKey = "com.unity.ml-agents.settings";
private static MLAgentsSettings s_Settings;
// setter will trigger callback for refreshing editor UI if using editor
public static MLAgentsSettings Settings
{
get
{
if (s_Settings == null)
{
Initialize();
}
return s_Settings;
}
set
{
Debug.Assert(value != null);
#if UNITY_EDITOR
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(value)))
{
EditorBuildSettings.AddConfigObject(EditorBuildSettingsConfigKey, value, true);
}
#endif
s_Settings = value;
ApplySettings();
}
}
static MLAgentsSettingsManager()
{
Initialize();
}
static void Initialize()
{
#if UNITY_EDITOR
InitializeInEditor();
#else
InitializeInPlayer();
#endif
}
#if UNITY_EDITOR
internal static void InitializeInEditor()
{
var settings = ScriptableObject.CreateInstance<MLAgentsSettings>();
if (EditorBuildSettings.TryGetConfigObject(EditorBuildSettingsConfigKey,
out MLAgentsSettings settingsAsset))
{
if (settingsAsset != null)
{
settings = settingsAsset;
}
}
Settings = settings;
}
#else
internal static void InitializeInPlayer()
{
Settings = Resources.FindObjectsOfTypeAll<MLAgentsSettings>().FirstOrDefault() ?? ScriptableObject.CreateInstance<MLAgentsSettings>();
}
#endif
internal static void ApplySettings()
{
OnSettingsChange?.Invoke();
}
internal static void Destroy()
{
s_Settings = null;
OnSettingsChange = null;
}
}
}