diff --git a/Packages/src/Editor/UIEffectProjectSettingsEditor.cs b/Packages/src/Editor/UIEffectProjectSettingsEditor.cs index aa7b3f8..e67bcc5 100644 --- a/Packages/src/Editor/UIEffectProjectSettingsEditor.cs +++ b/Packages/src/Editor/UIEffectProjectSettingsEditor.cs @@ -10,27 +10,24 @@ using UnityEditorInternal; namespace Coffee.UIEffects.Editors { [CustomEditor(typeof(UIEffectProjectSettings))] - public class UIEffectProjectSettingsEditor : Editor + internal class UIEffectProjectSettingsEditor : PreloadedProjectSettingsEditor { private ReorderableList _reorderableList; private SerializedProperty _runtimePresets; private SerializedProperty _runtimePresetsV2; private SerializedProperty _useHDRColorPicker; - private SerializedProperty _preLoadSettingsInBuild; - private bool _isInitialized; private ShaderVariantRegistryEditor _shaderVariantRegistryEditor; private UIEffect[] _legacyPresets; private bool _expandRuntimePresets; private bool _expandLegacyPrefabs; - private void InitializeIfNeeded() + protected override void OnEnable() { - if (_isInitialized) return; - + base.OnEnable(); + _legacyPresets = UIEffectProjectSettings.LoadEditorPresets(); _useHDRColorPicker = serializedObject.FindProperty("m_UseHDRColorPicker"); _runtimePresets = serializedObject.FindProperty("m_RuntimePresets"); _runtimePresetsV2 = serializedObject.FindProperty("m_RuntimePresetsV2"); - _preLoadSettingsInBuild = serializedObject.FindProperty("m_PreLoadSettingsInBuild"); _reorderableList = new ReorderableList(serializedObject, _runtimePresetsV2, true, true, true, true); _reorderableList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Runtime Presets"); @@ -51,24 +48,12 @@ namespace Coffee.UIEffects.Editors x => !UIEffectProjectSettings.instance.m_RuntimePresetsV2.Contains(x.preset), UIEffectProjectSettings.RegisterRuntimePreset); }; - - _isInitialized = true; - } - - private void OnEnable() - { - _legacyPresets = UIEffectProjectSettings.LoadEditorPresets(); - } - - private void OnDisable() - { - _isInitialized = false; } public override void OnInspectorGUI() { - serializedObject.Update(); - InitializeIfNeeded(); + EditorGUIUtility.labelWidth = 180; + base.OnInspectorGUI(); // Settings // Runtime Presets @@ -79,7 +64,6 @@ namespace Coffee.UIEffects.Editors // Editor // Use HDR color pickers. - EditorGUIUtility.labelWidth = 200; EditorGUILayout.PropertyField(_useHDRColorPicker); // Shader @@ -99,24 +83,8 @@ namespace Coffee.UIEffects.Editors _shaderVariantRegistryEditor.Draw(); - EditorGUILayout.Space(); - EditorGUILayout.LabelField("Advanced", EditorStyles.boldLabel); - EditorGUILayout.PropertyField(_preLoadSettingsInBuild); - if (!_preLoadSettingsInBuild.boolValue) - { - EditorGUILayout.BeginHorizontal(); - EditorGUILayout.HelpBox("UIEffectProjectSettings asset will not be built in." + - "please load manually from Resources, AssetBundle, or Addressables before using UIEffect.", - MessageType.Warning); - if (GUILayout.Button("Ping")) - { - EditorGUIUtility.PingObject(target); - } + DrawPreLoadSettingsInBuild("UIEffect"); - EditorGUILayout.EndHorizontal(); - } - - GUILayout.FlexibleSpace(); serializedObject.ApplyModifiedProperties(); } diff --git a/Packages/src/Editor/UIEffectProjectSettingsEditor.cs.meta b/Packages/src/Editor/UIEffectProjectSettingsEditor.cs.meta index 1b29185..0568817 100644 --- a/Packages/src/Editor/UIEffectProjectSettingsEditor.cs.meta +++ b/Packages/src/Editor/UIEffectProjectSettingsEditor.cs.meta @@ -1,8 +1,7 @@ fileFormatVersion: 2 guid: 1a9c54e67e8c9495fa77bfe119825877 -timeCreated: 1487152293 -licenseType: Pro MonoImporter: + externalObjects: {} serializedVersion: 2 defaultReferences: [] executionOrder: 0 diff --git a/Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs b/Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs index 4f24238..0955264 100644 --- a/Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs +++ b/Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs @@ -18,6 +18,8 @@ namespace Coffee.UIEffectInternal "When disable, you should load this settings via Resources, AssetBundles or " + "Addressables to use UIEffect.")] [SerializeField] + [Header("Advanced")] + [HideInInspector] private bool m_PreLoadSettingsInBuild = true; protected static bool s_BuildingPlayer; @@ -36,6 +38,7 @@ namespace Coffee.UIEffectInternal void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report) { + AssetDatabase.Refresh(); Initialize(); s_BuildingPlayer = true; @@ -69,9 +72,12 @@ namespace Coffee.UIEffectInternal var defaultSettings = GetDefaultSettings(t); if (defaultSettings == null) { - // When create a new instance, automatically set it as default settings. - defaultSettings = CreateInstance(t) as PreloadedProjectSettings; - if (!s_BuildingPlayer) 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) { @@ -103,7 +109,7 @@ namespace Coffee.UIEffectInternal 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); @@ -150,6 +156,35 @@ namespace Coffee.UIEffectInternal { } } + + 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 { } diff --git a/Packages/src/Runtime/Internal/Utilities/MaterialRepository.cs b/Packages/src/Runtime/Internal/Utilities/MaterialRepository.cs index 2991426..49f7595 100644 --- a/Packages/src/Runtime/Internal/Utilities/MaterialRepository.cs +++ b/Packages/src/Runtime/Internal/Utilities/MaterialRepository.cs @@ -13,6 +13,8 @@ namespace Coffee.UIEffectInternal 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.UIEffectInternal 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.UIEffectInternal 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 diff --git a/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs b/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs index 87dd520..d8a8ad4 100644 --- a/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs +++ b/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs @@ -188,6 +188,7 @@ namespace Coffee.UIEffectInternal /// public void RegisterOptionalShaders(Object owner) { + if (owner == null) return; var shaderPaths = ShaderUtil.GetAllShaderInfo() .Select(s => AssetDatabase.GetAssetPath(Shader.Find(s.name))) .Where(path => !string.IsNullOrEmpty(path) && path.EndsWith(".shader")) @@ -204,6 +205,7 @@ namespace Coffee.UIEffectInternal /// private void RegisterOptionalShaders(Object owner, string path) { + if (owner == null) return; if (!File.Exists(path) || !path.EndsWith(".shader")) return; var packageName = PackageInfo.FindForAssembly(typeof(ShaderVariantRegistry).Assembly)?.name; @@ -236,6 +238,7 @@ namespace Coffee.UIEffectInternal public void InitializeIfNeeded(Object owner) { + if (owner == null) return; Profiler.BeginSample("(EDITOR/COF)[ShaderVariantRegistry] InitializeIfNeeded"); if (!m_Asset && AssetDatabase.IsMainAsset(owner)) @@ -272,6 +275,7 @@ namespace Coffee.UIEffectInternal /// private void SyncRegisteredShaders(Object owner) { + if (owner == null) return; var so = new SerializedObject(m_Asset); var shaders = so.FindProperty("m_Shaders"); m_RegisteredShaders.Clear();