-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Fix automatic isntantiation
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
root = true | ||
|
||
[*] | ||
max_line_length = 100 | ||
indent_style = space | ||
indent_size = 4 | ||
charset = utf-8 | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
%YAML 1.1 | ||
%TAG !u! tag:unity3d.com,2011: | ||
--- !u!114 &11400000 | ||
MonoBehaviour: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
m_GameObject: {fileID: 0} | ||
m_Enabled: 1 | ||
m_EditorHideFlags: 0 | ||
m_Script: {fileID: 11500000, guid: 4f41518ec4cd48f0977f72ee2845e8ee, type: 3} | ||
m_Name: GameManagerSettings | ||
m_EditorClassIdentifier: | ||
profiles: | ||
- {fileID: 11400000, guid: 52ae552f66999ab44a599fe9a5a43d3f, type: 2} | ||
- {fileID: 11400000, guid: 35028f3c7e1cd994dadac07385689622, type: 2} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "CHARK.GameManagement.Editor", | ||
"rootNamespace": "CHARK.GameManagement", | ||
"references": [ | ||
"CHARK.GameManagement" | ||
], | ||
"includePlatforms": [ | ||
"Editor" | ||
], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": true, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using CHARK.GameManagement.Settings; | ||
using CHARK.GameManagement.Utilities; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace CHARK.GameManagement.Editor.Settings | ||
{ | ||
/// <summary> | ||
/// Creates game manager settings assets if they're missing and adds them to preloaded assets. | ||
/// By using this approach, <see cref="GameManagerSettings"/> can be loaded using | ||
/// <see cref="Resources.FindObjectsOfTypeAll{T}"/> during runtime (and in builds). | ||
/// </summary> | ||
internal static class GameManagerSettingsCreator | ||
{ | ||
private const string DefaultSettingsAssetPath = "Assets/Settings/GameManagerSettings.asset"; | ||
|
||
[InitializeOnLoadMethod] | ||
private static void OnInitializeEditor() | ||
{ | ||
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged; | ||
EditorApplication.playModeStateChanged += OnPlayModeStateChanged; | ||
|
||
UpdatePreloadedAssets(); | ||
} | ||
|
||
private static void OnPlayModeStateChanged(PlayModeStateChange state) | ||
{ | ||
if (state == PlayModeStateChange.ExitingEditMode) | ||
{ | ||
// We also need to update on play-mode change, otherwise assets added to preloaded | ||
// list after hitting the play button will not persist. | ||
UpdatePreloadedAssets(); | ||
} | ||
} | ||
|
||
private static void UpdatePreloadedAssets() | ||
{ | ||
if (IsSettingsAssetPreloaded()) | ||
{ | ||
return; | ||
} | ||
|
||
if (TrgGetSettingsAsset(out var existingAsset)) | ||
{ | ||
PreloadSettingsAsset(existingAsset); | ||
} | ||
else | ||
{ | ||
var newAsset = CreateSettingsAsset(); | ||
PreloadSettingsAsset(newAsset); | ||
} | ||
} | ||
|
||
private static bool IsSettingsAssetPreloaded() | ||
{ | ||
var preloadedAssets = PlayerSettings.GetPreloadedAssets(); | ||
foreach (var preloadedAsset in preloadedAssets) | ||
{ | ||
if (preloadedAsset && preloadedAsset is GameManagerSettings) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static bool TrgGetSettingsAsset(out GameManagerSettings asset) | ||
{ | ||
var existingAsset = AssetDatabase | ||
.FindAssets($"t:{nameof(GameManagerSettings)}") | ||
.Select(AssetDatabase.GUIDToAssetPath) | ||
.Select(AssetDatabase.LoadAssetAtPath<GameManagerSettings>) | ||
.FirstOrDefault(); | ||
|
||
if (existingAsset) | ||
{ | ||
asset = existingAsset; | ||
return true; | ||
} | ||
|
||
asset = default; | ||
return false; | ||
} | ||
|
||
private static void PreloadSettingsAsset(GameManagerSettings asset) | ||
{ | ||
var preloadedAssets = PlayerSettings.GetPreloadedAssets().ToList(); | ||
if (preloadedAssets.Contains(asset)) | ||
{ | ||
return; | ||
} | ||
|
||
preloadedAssets.Add(asset); | ||
|
||
PlayerSettings.SetPreloadedAssets(preloadedAssets.ToArray()); | ||
} | ||
|
||
private static GameManagerSettings CreateSettingsAsset() | ||
{ | ||
var settings = ScriptableObject.CreateInstance<GameManagerSettings>(); | ||
settings.AddProfiles(GetAvailableProfilesEditor()); | ||
|
||
var directoryName = Path.GetDirectoryName(DefaultSettingsAssetPath); | ||
if (string.IsNullOrWhiteSpace(directoryName) == false && Directory.Exists(directoryName) == false) | ||
{ | ||
Directory.CreateDirectory(directoryName); | ||
} | ||
|
||
AssetDatabase.CreateAsset(settings, DefaultSettingsAssetPath); | ||
AssetDatabase.SaveAssets(); | ||
AssetDatabase.Refresh(); | ||
|
||
Logging.LogDebug( | ||
"" | ||
+ $"Created new {nameof(GameManagerSettings)} at {DefaultSettingsAssetPath}," | ||
+ $" you can move this asset to a any directory and rename it as you'd like." | ||
+ $" <b>Make sure to add {nameof(GameManagerSettingsProfile)} to this asset!</b>", | ||
settings | ||
); | ||
|
||
return settings; | ||
} | ||
|
||
private static IEnumerable<GameManagerSettingsProfile> GetAvailableProfilesEditor() | ||
{ | ||
return AssetDatabase | ||
.FindAssets($"t:{nameof(GameManagerSettingsProfile)}") | ||
.Select(AssetDatabase.GUIDToAssetPath) | ||
.Select(AssetDatabase.LoadAssetAtPath<GameManagerSettingsProfile>); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using CHARK.GameManagement.Settings; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace CHARK.GameManagement.Editor.Settings | ||
{ | ||
[CustomPropertyDrawer(typeof(GameManagerSettingsProfile))] | ||
internal sealed class GameManagerSettingsProfilePropertyDrawer : PropertyDrawer | ||
{ | ||
private const float ToggleWidth = 16f; | ||
|
||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
var profile = property.objectReferenceValue as GameManagerSettingsProfile; | ||
EditorGUI.BeginProperty(position, label, property); | ||
|
||
var initialColor = GUI.backgroundColor; | ||
if (profile && profile.IsActiveProfile) | ||
{ | ||
GUI.backgroundColor = Color.green; | ||
} | ||
|
||
// Active toggle | ||
EditorGUI.BeginChangeCheck(); | ||
|
||
var togglePosition = position; | ||
togglePosition.width = ToggleWidth; | ||
|
||
var isActiveProfileNew = profile | ||
? EditorGUI.Toggle(togglePosition, profile.IsActiveProfile) | ||
: EditorGUI.Toggle(togglePosition, false); | ||
|
||
if (EditorGUI.EndChangeCheck() && profile) | ||
{ | ||
profile.IsActiveProfile = isActiveProfileNew; | ||
} | ||
|
||
// Profile object field | ||
var propertyPosition = position; | ||
propertyPosition.width -= ToggleWidth; | ||
propertyPosition.x += ToggleWidth; | ||
|
||
EditorGUI.PropertyField(propertyPosition, property, new GUIContent("Profile")); | ||
GUI.backgroundColor = initialColor; | ||
|
||
EditorGUI.EndProperty(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.