diff --git a/Editor/Helper/Helper.cs b/Editor/Helper/Helper.cs index 5828ded..f6d4617 100644 --- a/Editor/Helper/Helper.cs +++ b/Editor/Helper/Helper.cs @@ -402,7 +402,6 @@ public static void DrawLogo() private static GUIContent _guiContentCollapse = new GUIContent("", _iconCollapse, "Collapse All Groups"); private static GUIContent _guiContentVisibility = new GUIContent("", _iconVisibility, "Display Mode"); - private static string[] _materialInstanceNameEnd = new[] { "_Instantiated (Instance)", " (Instance)", "_Instantiated" }; private enum CopyMaterialValueMask { @@ -547,44 +546,9 @@ public static void DrawToolbarButtons(ref Rect toolBarRect, LWGUIMetaDatas metaD } else { - // Get Material Asset name - var name = material.name; - foreach (var nameEnd in _materialInstanceNameEnd) + if (FindMaterialAssetByMaterialInstance(material, metaDatas, out var materialAsset)) { - if (name.EndsWith(nameEnd)) - { - name = name.Substring(0, name.Length - nameEnd.Length); - break; - } - } - - // Get path - var guids = AssetDatabase.FindAssets("t:Material " + name); - var paths = guids.Select(((guid, i) => - { - var filePath = AssetDatabase.GUIDToAssetPath(guid); - var fileName = System.IO.Path.GetFileNameWithoutExtension(filePath); - return (fileName == name && filePath.EndsWith(".mat")) ? filePath : null; - })).Where((s => !string.IsNullOrEmpty(s))).ToArray(); - - // Select Asset - if (paths.Length == 0) - { - Debug.LogError("LWGUI: Can not find Material Assets with name: " + name); - } - else if (paths.Length > 1) - { - var str = string.Empty; - foreach (string path in paths) - { - str += "\n" + path; - } - Debug.LogWarning("LWGUI: Multiple Material Assets with the same name have been found, select only the first one:" + str); - Selection.activeObject = AssetDatabase.LoadAssetAtPath(paths[0]); - } - else - { - Selection.activeObject = AssetDatabase.LoadAssetAtPath(paths[0]); + Selection.activeObject = materialAsset; } } } @@ -631,16 +595,16 @@ public static void DrawToolbarButtons(ref Rect toolBarRect, LWGUIMetaDatas metaD if (GUI.Button(buttonRect, _guiContentVisibility, Helper.guiStyles_IconButton)) { // Build Display Mode Menu Items - string[] displayModeMenus = new[] + var displayModeMenus = new[] { "Show All Advanced Properties (" + displayModeData.advancedCount + " of " + perShaderData.propStaticDatas.Count + ")", "Show All Hidden Properties (" + displayModeData.hiddenCount + " of " + perShaderData.propStaticDatas.Count + ")", "Show Only Modified Properties (" + perMaterialData.modifiedCount + " of " + perShaderData.propStaticDatas.Count + ")", "Show Only Modified Properties by Group (" + perMaterialData.modifiedCount + " of " + perShaderData.propStaticDatas.Count + ")", }; - bool[] enabled = new[] { true, true, true, true }; - bool[] separator = new bool[4]; - int[] selected = new[] + var enabled = new[] { true, true, true, true }; + var separator = new bool[4]; + var selected = new[] { displayModeData.showAllAdvancedProperties ? 0 : -1, displayModeData.showAllHiddenProperties ? 1 : -1, @@ -682,20 +646,46 @@ void OnSwitchDisplayMode(object data, string[] options, int selectedIndex) toolBarRect.xMin += 2; } + public static Func onFindMaterialAssetInRendererByMaterialInstance; + + private static bool FindMaterialAssetByMaterialInstance(Material material, LWGUIMetaDatas metaDatas, out Material materialAsset) + { + materialAsset = null; + + var renderers = ReflectionHelper.GetMeshRenderersByMaterialEditor(metaDatas.perInspectorData.materialEditor); + foreach (var renderer in renderers) + { + if (onFindMaterialAssetInRendererByMaterialInstance != null) + { + materialAsset = onFindMaterialAssetInRendererByMaterialInstance(renderer, material); + } + + if (materialAsset == null) + { + int index = renderer.materials.ToList().FindIndex(materialInstance => materialInstance == material); + if (index >= 0 && index < renderer.sharedMaterials.Length) + { + materialAsset = renderer.sharedMaterials[index]; + } + } + + if (materialAsset != null && AssetDatabase.Contains(materialAsset)) + return true; + } + + Debug.LogError("LWGUI: Can not find the Material Assets of: " + material.name); + + return false; + } + #endregion #region Search Field private static readonly int s_TextFieldHash = "EditorTextField".GetHashCode(); - private static readonly GUIContent[] _searchModeMenus = - (new GUIContent[(int)SearchMode.Num]).Select(((guiContent, i) => - { - if (i == (int)SearchMode.Num) - return null; - - return new GUIContent(((SearchMode)i).ToString()); - })).ToArray(); + private static readonly GUIContent[] _searchModeMenus = Enumerable.Range(0, (int)SearchMode.Num - 1).Select(i => + new GUIContent(((SearchMode)i).ToString())).ToArray(); /// is has changed? public static bool DrawSearchField(Rect rect, LWGUIMetaDatas metaDatas) diff --git a/README.md b/README.md index 4b2bc52..d03d32a 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Having been validated through numerous large-scale commercial projects, employin * [Contribution](#contribution) - + diff --git a/README_CN.md b/README_CN.md index bc7f620..80991ea 100644 --- a/README_CN.md +++ b/README_CN.md @@ -68,7 +68,7 @@ * [Contribution](#contribution) - + diff --git a/Runtime/LwguiGradient.cs b/Runtime/LwguiGradient.cs index c8248f8..177f67b 100644 --- a/Runtime/LwguiGradient.cs +++ b/Runtime/LwguiGradient.cs @@ -87,11 +87,11 @@ public LwguiGradient(params Keyframe[] keys) public void Clear(ChannelMask channelMask = ChannelMask.All) { - for (int i = 0; i < (int)Channel.Num; i++) + for (int c = 0; c < (int)Channel.Num; c++) { - if (!IsChannelIndexInMask(i, channelMask)) continue; + if (!IsChannelIndexInMask(c, channelMask)) continue; - if (_curves.Count > i) _curves[i].ClearKeys(); + if (_curves.Count > c) _curves[c].keys = new Keyframe[0]; else _curves.Add(new AnimationCurve()); } } @@ -99,11 +99,11 @@ public void Clear(ChannelMask channelMask = ChannelMask.All) public void SetCurve(AnimationCurve curve, ChannelMask channelMask) { curve ??= defaultCurve; - for (int i = 0; i < (int)Channel.Num; i++) + for (int c = 0; c < (int)Channel.Num; c++) { - if (!IsChannelIndexInMask(i, channelMask)) continue; + if (!IsChannelIndexInMask(c, channelMask)) continue; - _curves[i] = curve; + _curves[c] = curve; } } @@ -126,12 +126,12 @@ public void SetRgbaCurves(List inRgbaCurves) public void AddKey(Keyframe key, ChannelMask channelMask) { - for (int i = 0; i < (int)Channel.Num; i++) + for (int c = 0; c < (int)Channel.Num; c++) { - if (!IsChannelIndexInMask(i, channelMask)) + if (!IsChannelIndexInMask(c, channelMask)) continue; - _curves[i].AddKey(key); + _curves[c].AddKey(key); } } diff --git a/UnityEditorExtension/ReflectionHelper.cs b/UnityEditorExtension/ReflectionHelper.cs index 4c6b763..0ac736d 100644 --- a/UnityEditorExtension/ReflectionHelper.cs +++ b/UnityEditorExtension/ReflectionHelper.cs @@ -56,6 +56,24 @@ public static void DefaultShaderPropertyInternal(MaterialEditor editor, Rect pos MaterialEditor_DefaultShaderPropertyInternal_Method.Invoke(editor, new System.Object[] { position, prop, label }); } + public static List GetMeshRenderersByMaterialEditor(MaterialEditor materialEditor) + { + var outRenderers = new List(); + + // MaterialEditor.ShouldEditorBeHidden() + PropertyEditor property = materialEditor.propertyViewer as PropertyEditor; + if (property) + { + GameObject gameObject = property.tracker.activeEditors[0].target as GameObject; + if (gameObject) + { + outRenderers.AddRange(gameObject.GetComponents()); + } + } + + return outRenderers; + } + #endregion diff --git a/package.json b/package.json index 2b0aab3..443529e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.jasonma.lwgui", - "version": "2.0.0", + "version": "1.18.0", "displayName": "LWGUI", "description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.", "keywords": [