diff --git a/Packages/src/Runtime/Internal/Utilities/CgincPathSync.cs b/Packages/src/Runtime/Internal/Utilities/CgincPathSync.cs new file mode 100644 index 0000000..d169fc9 --- /dev/null +++ b/Packages/src/Runtime/Internal/Utilities/CgincPathSync.cs @@ -0,0 +1,75 @@ +#if UNITY_EDITOR +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using UnityEditor; +using UnityEngine; + +namespace Coffee.UIEffectInternal +{ + public class CgincPathSync : AssetPostprocessor + { + private static string[] s_ShaderNames = null; + private static string s_CgincPattern = null; + + public static void RegisterShaders(string cgincPattern, string[] shaderNames) + { + s_CgincPattern = cgincPattern; + s_ShaderNames = shaderNames; + UpdateTmpCgincPath(); + } + + private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___, string[] ____) + { + UpdateTmpCgincPath(); + } + + private static void UpdateTmpCgincPath() + { + if (s_CgincPattern == null || s_ShaderNames == null || s_ShaderNames.Length == 0) return; + + AssetDatabase.StartAssetEditing(); + string[] cgincs = null; + foreach (var shaderName in s_ShaderNames) + { + var shader = Shader.Find(shaderName); + if (!shader) continue; + + var hasCgincError = ShaderUtil.GetShaderMessages(shader) + .Any(x => Regex.IsMatch(x.message, $"Couldn't open include file.*{s_CgincPattern}")); + if (!hasCgincError) continue; + + cgincs = cgincs ?? AssetDatabase.FindAssets("t:ShaderInclude") + .Select(AssetDatabase.GUIDToAssetPath) + .Where(x => Regex.IsMatch(x, s_CgincPattern)) + .ToArray(); + + UpdateTmpCgincPath(shader, cgincs); + } + + AssetDatabase.StopAssetEditing(); + } + + private static void UpdateTmpCgincPath(Shader shader, string[] cgincs) + { + var path = AssetDatabase.GetAssetPath(shader); + var text = File.ReadAllText(path); + var hash = text.GetHashCode(); + + foreach (var cginc in cgincs) + { + var file = Path.GetFileName(cginc); + text = Regex.Replace(text, $"#include.*\"[^\"]*{file}\"", $"#include \"{cginc}\"", + RegexOptions.Multiline); + } + + if (hash == text.GetHashCode()) return; + + Debug.Log($"Update Shader: '{path}'"); + File.WriteAllText(path, text, Encoding.UTF8); + AssetDatabase.ImportAsset(path); + } + } +} +#endif diff --git a/Packages/src/Runtime/Internal/Utilities/CgincPathSync.cs.meta b/Packages/src/Runtime/Internal/Utilities/CgincPathSync.cs.meta new file mode 100644 index 0000000..2fc0eaf --- /dev/null +++ b/Packages/src/Runtime/Internal/Utilities/CgincPathSync.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b39275b24ae5648d5a67a308a9127e81 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Runtime/UIEffectProjectSettings.cs b/Packages/src/Runtime/UIEffectProjectSettings.cs index b92c605..3d76098 100644 --- a/Packages/src/Runtime/UIEffectProjectSettings.cs +++ b/Packages/src/Runtime/UIEffectProjectSettings.cs @@ -128,6 +128,20 @@ namespace Coffee.UIEffects }); EditorApplication.update += ShaderSampleImporter.Update; + CgincPathSync.RegisterShaders("/(TMPro|TMPro_Properties).cginc", new[] + { + "Hidden/TextMeshPro/Distance Field (UIEffect)", + "Hidden/TextMeshPro/Mobile/Distance Field (UIEffect)", + "Hidden/TextMeshPro/Distance Field SSD (UIEffect)", + "Hidden/TextMeshPro/Mobile/Distance Field SSD (UIEffect)", + "Hidden/TextMeshPro/Distance Field Overlay (UIEffect)", + "Hidden/TextMeshPro/Mobile/Distance Field Overlay (UIEffect)", + "Hidden/TextMeshPro/Bitmap (UIEffect)", + "Hidden/TextMeshPro/Mobile/Bitmap (UIEffect)", + "Hidden/TextMeshPro/Distance Field (UIEffect)", + "Hidden/TextMeshPro/Mobile/Distance Field (UIEffect)" + }); + Selection.selectionChanged += () => { if (Application.isPlaying || Misc.isBatchOrBuilding) return;