Skip to content

Commit

Permalink
added a better UI into the project settings view
Browse files Browse the repository at this point in the history
  • Loading branch information
McJones committed Jan 17, 2024
1 parent 8dcadfa commit 4528ee9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Added

- A Unity Project scoped settings that allows you to override some of the default behaviours of Yarn Spinner
- settings are saved to a file in `ProjectSettings\Packages\dev.yarnspinner\YarnSpinnerProjectSettings.json`
- these can be changed via `Edit -> Project Settings -> Yarn Spinner`
- currently supports two convenience features of Yarn Spinner:
- automatically associating assets with localisations
- automatically linking YarnCommand and YarnFunction attributed methods to the dialogue runner
- enabling/disabling C# linking will force an entire C# reimport
- enabling/disabling asset linking will force a reimport of all `yarnprojects`

### Changed

- Update the minimum Unity version to 2021.3.
Expand Down
90 changes: 80 additions & 10 deletions Editor/YarnSpinnerProjectSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,104 @@ namespace Yarn.Unity.Editor
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using UnityEditor.Compilation;

class YarnSpinnerProjectSettingsProvider : SettingsProvider
{
private YarnSpinnerProjectSettings baseSettings;
private YarnSpinnerProjectSettings unsavedSettings;

public YarnSpinnerProjectSettingsProvider(string path, SettingsScope scope = SettingsScope.Project) : base(path, scope) { }

public override void OnActivate(string searchContext, VisualElement rootElement)
{
// This function is called when the user clicks on the MyCustom element in the Settings window.
baseSettings = YarnSpinnerProjectSettings.GetOrCreateSettings();
unsavedSettings = YarnSpinnerProjectSettings.GetOrCreateSettings();
}

public override void OnGUI(string searchContext)
{
EditorGUILayout.LabelField("Automatic Recompilation and Asset Association");
using (var changeCheck = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.LabelField("Automatically update localised assets with Yarn Projects");
var localisedAssetUpdate = EditorGUILayout.Toggle(baseSettings.autoRefreshLocalisedAssets);
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.BeginHorizontal();

EditorGUILayout.LabelField("do codegen");
var linkingAttributedFuncs = EditorGUILayout.Toggle(baseSettings.automaticallyLinkAttributedYarnCommandsAndFunctions);
EditorGUILayout.LabelField("Update Localised Assets", GUILayout.Width(290), GUILayout.ExpandWidth(false));
var localisedAssetUpdate = EditorGUILayout.Toggle(unsavedSettings.autoRefreshLocalisedAssets, GUILayout.ExpandWidth(false));

if (changeCheck.changed)
{
baseSettings.autoRefreshLocalisedAssets = localisedAssetUpdate;
baseSettings.automaticallyLinkAttributedYarnCommandsAndFunctions = linkingAttributedFuncs;
baseSettings.WriteSettings();
EditorGUILayout.EndHorizontal();

EditorGUILayout.BeginHorizontal();

EditorGUILayout.LabelField("Generate linkings for Functions and Commands", GUILayout.Width(290), GUILayout.ExpandWidth(false));
var linkingAttributedFuncs = EditorGUILayout.Toggle(unsavedSettings.automaticallyLinkAttributedYarnCommandsAndFunctions, GUILayout.ExpandWidth(false));

EditorGUILayout.EndHorizontal();

if (changeCheck.changed)
{
unsavedSettings.autoRefreshLocalisedAssets = localisedAssetUpdate;
unsavedSettings.automaticallyLinkAttributedYarnCommandsAndFunctions = linkingAttributedFuncs;
}

bool disabledReimportButton = true;
if
(
unsavedSettings.automaticallyLinkAttributedYarnCommandsAndFunctions != baseSettings.automaticallyLinkAttributedYarnCommandsAndFunctions ||
unsavedSettings.autoRefreshLocalisedAssets != baseSettings.autoRefreshLocalisedAssets
)
{
disabledReimportButton = false;
}

EditorGUILayout.Space();
using (new EditorGUI.DisabledScope(disabledReimportButton))
{
if (GUILayout.Button("Apply Changes", GUILayout.Width(200)))
{
// we need to know which parts we will need to reimport
// we check and change the setting first
// because the settings are used in the reimports that are about to be run
// and only then can we do the appropriate reimports
bool needsCSharpRecompilation = false;
bool needsYarnProjectReimport = false;

if (baseSettings.autoRefreshLocalisedAssets != unsavedSettings.autoRefreshLocalisedAssets)
{
needsYarnProjectReimport = true;
}
if (baseSettings.automaticallyLinkAttributedYarnCommandsAndFunctions != unsavedSettings.automaticallyLinkAttributedYarnCommandsAndFunctions)
{
needsCSharpRecompilation = true;
}

// saving the changed settings out to disk
baseSettings.autoRefreshLocalisedAssets = unsavedSettings.autoRefreshLocalisedAssets;
baseSettings.automaticallyLinkAttributedYarnCommandsAndFunctions = unsavedSettings.automaticallyLinkAttributedYarnCommandsAndFunctions;
baseSettings.WriteSettings();

// now we can reimport
if (needsCSharpRecompilation)
{
// need to do a full build because if they have DISABLED this then we want to turf the already linked code
// this is BIG but I can't see any other way around this
// I am assuming people aren't doing this so often as to be a huge headache
CompilationPipeline.RequestScriptCompilation(RequestScriptCompilationOptions.CleanBuildCache);
}
if (needsYarnProjectReimport)
{
// here we just reimport the yarn projects
var yarnProjects = AssetDatabase.FindAssets($"t:{nameof(YarnProject)}");
foreach (var guid in yarnProjects)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
AssetDatabase.ImportAsset(path);
}
}
}
}
}
}
}
Expand Down

0 comments on commit 4528ee9

Please sign in to comment.