Skip to content

Commit

Permalink
fix: If not configured as a preloaded asset, the project settings ass…
Browse files Browse the repository at this point in the history
…et will be regenerated

close #200, close #212
  • Loading branch information
mob-sakai committed Nov 13, 2024
1 parent c3f2eb1 commit cca61d7
Showing 1 changed file with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
Expand All @@ -14,6 +14,14 @@ namespace Coffee.UISoftMaskInternal
public abstract class PreloadedProjectSettings : ScriptableObject
#if UNITY_EDITOR
{
private class Postprocessor : AssetPostprocessor
{
private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___, string[] ____)
{
Initialize();
}
}

private class PreprocessBuildWithReport : IPreprocessBuildWithReport
{
int IOrderedCallback.callbackOrder => 0;
Expand All @@ -24,32 +32,32 @@ void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report)
}
}

[InitializeOnLoadMethod]
[InitializeOnEnterPlayMode]
private static void Initialize()
{
const BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
foreach (var t in TypeCache.GetTypesDerivedFrom(typeof(PreloadedProjectSettings<>)))
{
var defaultSettings = GetDefaultSettings(t);
if (!defaultSettings)
{
// When create a new instance, automatically set it as default settings.
defaultSettings = t.GetProperty("instance", flags)
?.GetValue(null, null) as PreloadedProjectSettings;
defaultSettings = CreateInstance(t) as PreloadedProjectSettings;
SetDefaultSettings(defaultSettings);
}
else if (GetPreloadedSettings(t).Length != 1)
{
SetDefaultSettings(defaultSettings);
}
}

EditorApplication.QueuePlayerLoopUpdate();
if (defaultSettings)
{
defaultSettings.OnInitialize();
}
}
}

protected static string GetDefaultName(Type type, bool nicify)
{
var typeName = type.Name.Replace("ProjectSettings", "");
var typeName = type.Name;
return nicify
? ObjectNames.NicifyVariableName(typeName)
: typeName;
Expand All @@ -74,6 +82,7 @@ protected static PreloadedProjectSettings GetDefaultSettings(Type type)
protected static void SetDefaultSettings(PreloadedProjectSettings asset)
{
if (!asset) return;

var type = asset.GetType();
if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(asset)))
{
Expand All @@ -84,7 +93,11 @@ protected static void SetDefaultSettings(PreloadedProjectSettings asset)

var assetPath = $"Assets/ProjectSettings/{GetDefaultName(type, false)}.asset";
assetPath = AssetDatabase.GenerateUniqueAssetPath(assetPath);
AssetDatabase.CreateAsset(asset, assetPath);
if (!File.Exists(assetPath))
{
AssetDatabase.CreateAsset(asset, assetPath);
asset.OnCreateAsset();
}
}

var preloadedAssets = PlayerSettings.GetPreloadedAssets();
Expand All @@ -98,6 +111,14 @@ protected static void SetDefaultSettings(PreloadedProjectSettings asset)

AssetDatabase.Refresh();
}

protected virtual void OnCreateAsset()
{
}

protected virtual void OnInitialize()
{
}
}
#else
{
Expand Down

0 comments on commit cca61d7

Please sign in to comment.