Skip to content

Commit

Permalink
Fix Select Material Button, and Add CustomMaterialAssetFinder Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMa0012 committed Nov 29, 2024
1 parent 87aad57 commit 92338d4
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Editor/CustomGUISample/CustomFooter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static void DoCustomFooter(LWGUI lwgui)
[InitializeOnLoadMethod]
private static void RegisterEvent()
{
LWGUI.onDrawCustomFooter += DoCustomFooter;
// Register Event
// LWGUI.onDrawCustomFooter += DoCustomFooter;
}
}
}
3 changes: 2 additions & 1 deletion Editor/CustomGUISample/CustomHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static void DoCustomHeader(LWGUI lwgui)
[InitializeOnLoadMethod]
private static void RegisterEvent()
{
LWGUI.onDrawCustomHeader += DoCustomHeader;
// Register Event
// LWGUI.onDrawCustomHeader += DoCustomHeader;
}
}
}
48 changes: 48 additions & 0 deletions Editor/CustomGUISample/CustomMaterialAssetFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Linq;
using UnityEngine;
using UnityEditor;

namespace LWGUI
{
public static class CustomMaterialAssetFinder
{
public static Material FindMaterialAssetInRendererByMaterialInstance(Renderer renderer, Material materialInstance)
{
Material materialAsset = null;

// Find the material asset by name
// if (materialAsset == null)
// {
// var name = materialInstance.name.Replace(" (Instance)", "");
// var guids = AssetDatabase.FindAssets("t:Material " + name, new[] { "Assets" }).Select(guid =>
// {
// var assetPath = AssetDatabase.GUIDToAssetPath(guid);
// if (string.IsNullOrEmpty(assetPath) || !assetPath.EndsWith(".mat"))
// return null;
// else
// return guid;
// }).ToArray();
//
// if (guids != null && guids.Length > 0)
// {
// var matPath = AssetDatabase.GUIDToAssetPath(guids[0]);
// Selection.activeObject = AssetDatabase.LoadAssetAtPath<Material>(matPath);
//
// if (guids.Length > 1)
// {
// Debug.LogWarning($"LWGUI: Multiple materials with the same name were found, and the first one was selected: { matPath }");
// }
// }
// }

return materialAsset;
}

[InitializeOnLoadMethod]
private static void RegisterEvent()
{
// Register Event
// Helper.onFindMaterialAssetInRendererByMaterialInstance = FindMaterialAssetInRendererByMaterialInstance;
}
}
}
3 changes: 3 additions & 0 deletions Editor/CustomGUISample/CustomMaterialAssetFinder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/Helper/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ void OnSwitchDisplayMode(object data, string[] options, int selectedIndex)
toolBarRect.xMin += 2;
}

public static Func<MeshRenderer, Material, Material> onFindMaterialAssetInRendererByMaterialInstance;
public static Func<Renderer, Material, Material> onFindMaterialAssetInRendererByMaterialInstance;

private static bool FindMaterialAssetByMaterialInstance(Material material, LWGUIMetaDatas metaDatas, out Material materialAsset)
{
Expand Down
5 changes: 3 additions & 2 deletions UnityEditorExtension/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public static void DefaultShaderPropertyInternal(this MaterialEditor editor, Rec
editor.DefaultShaderPropertyInternal(position, prop, label);
}

public static List<MeshRenderer> GetMeshRenderersByMaterialEditor(this MaterialEditor materialEditor)
public static List<Renderer> GetMeshRenderersByMaterialEditor(this MaterialEditor materialEditor)
{
var outRenderers = new List<MeshRenderer>();
var outRenderers = new List<Renderer>();

// MaterialEditor.ShouldEditorBeHidden()
PropertyEditor property = materialEditor.propertyViewer as PropertyEditor;
Expand All @@ -63,6 +63,7 @@ public static List<MeshRenderer> GetMeshRenderersByMaterialEditor(this MaterialE
if (gameObject)
{
outRenderers.AddRange(gameObject.GetComponents<MeshRenderer>());
outRenderers.AddRange(gameObject.GetComponents<SkinnedMeshRenderer>());
}
}

Expand Down
2 changes: 1 addition & 1 deletion UnityEditorExtension/UnityEditorExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Jason Ma
// Copyright (c) Jason Ma

using UnityEditor;
using UnityEngine;
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": "1.20.1",
"version": "1.20.2",
"displayName": "LWGUI",
"description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.",
"keywords": [
Expand Down

0 comments on commit 92338d4

Please sign in to comment.