Skip to content

Commit

Permalink
Fix compile error and find material asset algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMa0012 committed Jul 11, 2024
1 parent 006e99c commit 149b971
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 62 deletions.
90 changes: 40 additions & 50 deletions Editor/Helper/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<Material>(paths[0]);
}
else
{
Selection.activeObject = AssetDatabase.LoadAssetAtPath<Material>(paths[0]);
Selection.activeObject = materialAsset;
}
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -682,20 +646,46 @@ void OnSwitchDisplayMode(object data, string[] options, int selectedIndex)
toolBarRect.xMin += 2;
}

public static Func<MeshRenderer, Material, Material> 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();

/// <returns>is has changed?</returns>
public static bool DrawSearchField(Rect rect, LWGUIMetaDatas metaDatas)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Having been validated through numerous large-scale commercial projects, employin
* [Contribution](#contribution)

<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
<!-- Added by: runner, at: Thu Jul 11 05:54:40 UTC 2024 -->
<!-- Added by: runner, at: Fri May 24 11:12:50 UTC 2024 -->

<!--te-->

Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* [Contribution](#contribution)

<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
<!-- Added by: runner, at: Thu Jul 11 05:54:41 UTC 2024 -->
<!-- Added by: runner, at: Fri May 24 11:12:51 UTC 2024 -->

<!--te-->

Expand Down
18 changes: 9 additions & 9 deletions Runtime/LwguiGradient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,23 @@ 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());
}
}

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;
}
}

Expand All @@ -126,12 +126,12 @@ public void SetRgbaCurves(List<AnimationCurve> 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);
}

}
Expand Down
18 changes: 18 additions & 0 deletions UnityEditorExtension/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeshRenderer> GetMeshRenderersByMaterialEditor(MaterialEditor materialEditor)
{
var outRenderers = new List<MeshRenderer>();

// 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<MeshRenderer>());
}
}

return outRenderers;
}

#endregion


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 149b971

Please sign in to comment.