diff --git a/Runtime/AnimatableProperty.cs b/Runtime/AnimatableProperty.cs index b0b892a..bbcbe65 100644 --- a/Runtime/AnimatableProperty.cs +++ b/Runtime/AnimatableProperty.cs @@ -44,7 +44,7 @@ namespace Coffee.UIExtensions break; case ShaderPropertyType.Float: case ShaderPropertyType.Range: - material.SetFloat(id, mpb.GetFloat(id)); + material.SetFloat(id, mpb.GetFloat(id)); break; case ShaderPropertyType.Texture: material.SetTexture(id, mpb.GetTexture(id)); diff --git a/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs b/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs index fd09928..36e38ed 100644 --- a/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs +++ b/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs @@ -14,6 +14,15 @@ namespace Coffee.UIParticleInternal public abstract class PreloadedProjectSettings : ScriptableObject #if UNITY_EDITOR { + [Tooltip("When enabled, this settings asset will be added to PlayerSettings.preloadedAssets in build.\n\n" + + "When disable, you should load this settings via Resources, AssetBundles or Addressables to use.")] + [SerializeField] + [Header("Advanced")] + [HideInInspector] + private bool m_PreLoadSettingsInBuild = true; + + protected static bool s_BuildingPlayer; + private class Postprocessor : AssetPostprocessor { private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___, string[] ____) @@ -22,12 +31,35 @@ namespace Coffee.UIParticleInternal } } - private class PreprocessBuildWithReport : IPreprocessBuildWithReport + private class ExcludeFromBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport { int IOrderedCallback.callbackOrder => 0; void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report) { + AssetDatabase.Refresh(); + Initialize(); + s_BuildingPlayer = true; + + foreach (var t in TypeCache.GetTypesDerivedFrom(typeof(PreloadedProjectSettings<>))) + { + var settings = GetDefaultSettings(t); + if (!settings || settings.m_PreLoadSettingsInBuild) continue; + + PlayerSettings.SetPreloadedAssets( + PlayerSettings.GetPreloadedAssets() + .Where(x => x && x.GetType() != t) + .ToArray()); + + Debug.Log($"[PreloadedProjectSettings] Build started: removed '{settings.name}' " + + $"({t.Name}) from PreloadedAssets. " + + $"It will be restored after build completes."); + } + } + + void IPostprocessBuildWithReport.OnPostprocessBuild(BuildReport report) + { + s_BuildingPlayer = false; Initialize(); } } @@ -39,13 +71,16 @@ namespace Coffee.UIParticleInternal var defaultSettings = GetDefaultSettings(t); if (defaultSettings == null) { - // When create a new instance, automatically set it as default settings. - defaultSettings = CreateInstance(t) as PreloadedProjectSettings; - SetDefaultSettings(defaultSettings); + if (!s_BuildingPlayer) + { + // When create a new instance, automatically set it as default settings. + defaultSettings = CreateInstance(t) as PreloadedProjectSettings; + SetDefaultSettings(defaultSettings); + } } else if (GetPreloadedSettings(t).Length != 1) { - SetDefaultSettings(defaultSettings); + if (!s_BuildingPlayer) SetDefaultSettings(defaultSettings); } if (defaultSettings != null) @@ -73,7 +108,7 @@ namespace Coffee.UIParticleInternal protected static PreloadedProjectSettings GetDefaultSettings(Type type) { return GetPreloadedSettings(type).FirstOrDefault() as PreloadedProjectSettings - ?? AssetDatabase.FindAssets($"t:{nameof(PreloadedProjectSettings)}") + ?? AssetDatabase.FindAssets($"t:{type.Name}") .Select(AssetDatabase.GUIDToAssetPath) .Select(AssetDatabase.LoadAssetAtPath) .FirstOrDefault(x => x != null && x.GetType() == type); @@ -120,6 +155,35 @@ namespace Coffee.UIParticleInternal { } } + + internal abstract class PreloadedProjectSettingsEditor : Editor + { + private SerializedProperty _preLoadSettingsInBuild; + + protected virtual void OnEnable() + { + _preLoadSettingsInBuild = serializedObject.FindProperty("m_PreLoadSettingsInBuild"); + } + + protected void DrawPreLoadSettingsInBuild(string packageName) + { + EditorGUILayout.PropertyField(_preLoadSettingsInBuild); + if (!_preLoadSettingsInBuild.boolValue) + { + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.HelpBox( + $"{target.GetType().Name} asset will not be built in.\n" + + $"please load manually from Resources, AssetBundle, or Addressables before using {packageName}.", + MessageType.Warning); + if (GUILayout.Button("Ping")) + { + EditorGUIUtility.PingObject(target); + } + + EditorGUILayout.EndHorizontal(); + } + } + } #else { } @@ -151,7 +215,7 @@ namespace Coffee.UIParticleInternal return s_Instance; } - SetDefaultSettings(s_Instance); + if (!s_BuildingPlayer) SetDefaultSettings(s_Instance); return s_Instance; } } @@ -193,9 +257,12 @@ namespace Coffee.UIParticleInternal } EditorApplication.playModeStateChanged += OnPlayModeStateChanged; +#else + if (s_Instance && s_Instance != this) + { + Destroy(s_Instance); + } #endif - - if (s_Instance != null) return; s_Instance = this as T; } diff --git a/Runtime/Internal/Utilities/Logger.cs.meta b/Runtime/Internal/Utilities/Logger.cs.meta index 5ce2425..258f68f 100644 --- a/Runtime/Internal/Utilities/Logger.cs.meta +++ b/Runtime/Internal/Utilities/Logger.cs.meta @@ -1,2 +1,11 @@ fileFormatVersion: 2 -guid: 4f9f22bb079324476b1473030ad9fec3 \ No newline at end of file +guid: 4f9f22bb079324476b1473030ad9fec3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Internal/Utilities/MaterialRepository.cs b/Runtime/Internal/Utilities/MaterialRepository.cs index cd3c05e..b643581 100644 --- a/Runtime/Internal/Utilities/MaterialRepository.cs +++ b/Runtime/Internal/Utilities/MaterialRepository.cs @@ -13,6 +13,8 @@ namespace Coffee.UIParticleInternal public static int count => s_Repository.count; + public static Func onShaderFind = Shader.Find; + #if UNITY_EDITOR [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] public static void Clear() @@ -48,7 +50,7 @@ namespace Coffee.UIParticleInternal public static void Get(Hash128 hash, ref Material material, string shaderName) { Profiler.BeginSample("(COF)[MaterialRepository] Get"); - s_Repository.Get(hash, ref material, x => new Material(Shader.Find(x)) + s_Repository.Get(hash, ref material, x => new Material(onShaderFind(x)) { hideFlags = HideFlags.DontSave | HideFlags.NotEditable }, shaderName); @@ -61,7 +63,7 @@ namespace Coffee.UIParticleInternal public static void Get(Hash128 hash, ref Material material, string shaderName, string[] keywords) { Profiler.BeginSample("(COF)[MaterialRepository] Get"); - s_Repository.Get(hash, ref material, x => new Material(Shader.Find(x.shaderName)) + s_Repository.Get(hash, ref material, x => new Material(onShaderFind(x.shaderName)) { hideFlags = HideFlags.DontSave | HideFlags.NotEditable, shaderKeywords = x.keywords