-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yarn Projects now reimport themselves when localisation assets change.
This behaviour can be controlled via the new project settings if it is irksome. Needs work on the settings UI, but is functionally working. Fixes #259
- Loading branch information
Showing
5 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace Yarn.Unity.Editor | ||
{ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// An asset post processor that forwards any asset database changes to all YarnProjectImporter for them to verify if they need to update their locale assets. | ||
/// </summary> | ||
class YarnProjectAssetReimport : AssetPostprocessor | ||
{ | ||
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload) | ||
{ | ||
if (!YarnSpinnerProjectSettings.GetOrCreateSettings().autoRefreshLocalisedAssets) | ||
{ | ||
return; | ||
} | ||
var modifiedAssets = new List<string>(); | ||
modifiedAssets.AddRange(importedAssets); | ||
modifiedAssets.AddRange(deletedAssets); | ||
modifiedAssets.AddRange(movedAssets); | ||
modifiedAssets.AddRange(movedFromAssetPaths); | ||
|
||
var yarnProjects = AssetDatabase.FindAssets($"t:{nameof(YarnProject)}"); | ||
foreach (var guid in yarnProjects) | ||
{ | ||
var path = AssetDatabase.GUIDToAssetPath(guid); | ||
var importer = AssetImporter.GetAtPath(path) as YarnProjectImporter; | ||
if (importer != null) | ||
{ | ||
importer.CheckUpdatedAssetsRequireReimport(modifiedAssets); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
namespace Yarn.Unity.Editor | ||
{ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
using UnityEditor.UIElements; | ||
|
||
/// <summary> | ||
/// Basic data class of unity settings that impact Yarn Spinner. | ||
/// </summary> | ||
/// <remarks> | ||
/// Currently this only supports disabling the automatic reimport of Yarn Projects when locale assets change, but other settings will eventually end up here. | ||
/// </remarks> | ||
class YarnSpinnerProjectSettings | ||
{ | ||
public static string YarnSpinnerProjectSettingsPath => Path.Combine("ProjectSettings", "Packages", "dev.yarnspinner", "YarnSpinnerProjectSettings.json"); | ||
|
||
[SerializeField] internal bool autoRefreshLocalisedAssets = true; | ||
|
||
internal static YarnSpinnerProjectSettings GetOrCreateSettings() | ||
{ | ||
YarnSpinnerProjectSettings settings = new YarnSpinnerProjectSettings(); | ||
if (File.Exists(YarnSpinnerProjectSettingsPath)) | ||
{ | ||
try | ||
{ | ||
var settingsData = File.ReadAllText(YarnSpinnerProjectSettingsPath); | ||
EditorJsonUtility.FromJsonOverwrite(settingsData, settings); | ||
|
||
return settings; | ||
} | ||
catch (System.Exception e) | ||
{ | ||
Debug.LogWarning($"Failed to load Yarn Spinner project settings at {YarnSpinnerProjectSettingsPath}: {e.Message}"); | ||
} | ||
} | ||
|
||
settings.autoRefreshLocalisedAssets = true; | ||
settings.WriteSettings(); | ||
|
||
return settings; | ||
} | ||
|
||
internal void WriteSettings() | ||
{ | ||
var jsonValue = EditorJsonUtility.ToJson(this); | ||
|
||
var folder = Path.GetDirectoryName(YarnSpinnerProjectSettingsPath); | ||
if (!Directory.Exists(folder)) | ||
{ | ||
Directory.CreateDirectory(folder); | ||
} | ||
|
||
try | ||
{ | ||
File.WriteAllText(YarnSpinnerProjectSettingsPath, jsonValue); | ||
} | ||
catch (System.Exception e) | ||
{ | ||
Debug.LogError($"Failed to save Yarn Spinner project settings to {YarnSpinnerProjectSettingsPath}: {e.Message}"); | ||
} | ||
} | ||
} | ||
|
||
class YarnSpinnerProjectSettingsProvider : SettingsProvider | ||
{ | ||
private YarnSpinnerProjectSettings baseSettings; | ||
|
||
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(); | ||
} | ||
|
||
public override void OnGUI(string searchContext) | ||
{ | ||
// Use IMGUI to display UI: | ||
EditorGUILayout.LabelField("Automatically update localised assets with Yarn Projects"); | ||
|
||
using (var changeCheck = new EditorGUI.ChangeCheckScope()) | ||
{ | ||
var result = EditorGUILayout.Toggle(baseSettings.autoRefreshLocalisedAssets); | ||
|
||
if (changeCheck.changed) | ||
{ | ||
baseSettings.autoRefreshLocalisedAssets = result; | ||
baseSettings.WriteSettings(); | ||
} | ||
} | ||
} | ||
|
||
// Register the SettingsProvider | ||
[SettingsProvider] | ||
public static SettingsProvider CreateYarnSpinnerProjectSettingsProvider() | ||
{ | ||
var provider = new YarnSpinnerProjectSettingsProvider("Project/Yarn Spinner", SettingsScope.Project); | ||
|
||
var keywords = new List<string>() { "yarn", "spinner", "localisation" }; | ||
provider.keywords = keywords; | ||
return provider; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.