Skip to content

Commit ed89e55

Browse files
committed
Fix: ImageDrawer cannot parse relative paths
Fix: Sample Shaders
1 parent e0d4ba5 commit ed89e55

File tree

7 files changed

+72
-20
lines changed

7 files changed

+72
-20
lines changed

Editor/LWGUI.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"references": [
55
"LWGUI.Runtime",
66
"LWGUI.Timeline",
7-
"Unity.InternalAPIEditorBridge.020"
7+
"Unity.InternalAPIEditorBridge.020",
8+
"LWGUI.Runtime.Timeline"
89
],
910
"includePlatforms": [
1011
"Editor"

Editor/ShaderDrawer.cs

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,25 +1702,71 @@ public ImageDrawer(string group)
17021702

17031703
public override void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData)
17041704
{
1705-
var imagePath = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + inProp.displayName;
1706-
if (File.Exists(imagePath))
1705+
var inputPath = (inProp.displayName ?? string.Empty).Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
1706+
var projectRoot = Helper.ProjectPath;
1707+
string imagePath = null;
1708+
1709+
try
17071710
{
1708-
var fileData = File.ReadAllBytes(imagePath);
1709-
Texture2D texture = new Texture2D(2, 2);
1711+
// If the display path already starts with Assets (project-relative), resolve from project root
1712+
if (inputPath.StartsWith("Assets"))
1713+
{
1714+
imagePath = Path.Combine(projectRoot, inputPath);
1715+
}
1716+
// If it's an absolute path, use it directly
1717+
else if (Path.IsPathRooted(inputPath))
1718+
{
1719+
imagePath = inputPath;
1720+
}
1721+
else
1722+
{
1723+
// Try resolving relative to the shader file location
1724+
var shaderAssetPath = AssetDatabase.GetAssetPath(inShader);
1725+
if (!string.IsNullOrEmpty(shaderAssetPath))
1726+
{
1727+
var shaderFullPath = Path.Combine(projectRoot, shaderAssetPath);
1728+
var shaderDir = Path.GetDirectoryName(shaderFullPath);
1729+
if (!string.IsNullOrEmpty(shaderDir))
1730+
{
1731+
// Combine and normalize to resolve ../ segments
1732+
var candidate = Path.GetFullPath(Path.Combine(shaderDir, inputPath));
1733+
if (File.Exists(candidate))
1734+
{
1735+
imagePath = candidate;
1736+
}
1737+
}
1738+
}
1739+
// Fallback: try project-root relative resolution
1740+
if (imagePath == null)
1741+
{
1742+
var candidate2 = Path.Combine(projectRoot, inputPath);
1743+
if (File.Exists(candidate2)) imagePath = candidate2;
1744+
}
1745+
}
17101746

1711-
// LoadImage will auto-resize the texture dimensions
1712-
if (texture.LoadImage(fileData))
1747+
if (!string.IsNullOrEmpty(imagePath) && File.Exists(imagePath))
17131748
{
1714-
inoutPropertyStaticData.image = texture;
1749+
var fileData = File.ReadAllBytes(imagePath);
1750+
Texture2D texture = new Texture2D(2, 2);
1751+
1752+
// LoadImage will auto-resize the texture dimensions
1753+
if (texture.LoadImage(fileData))
1754+
{
1755+
inoutPropertyStaticData.image = texture;
1756+
}
1757+
else
1758+
{
1759+
Debug.LogError($"LWGUI: Failed to load image data into texture: { imagePath }");
1760+
}
17151761
}
17161762
else
17171763
{
1718-
Debug.LogError($"LWGUI: Failed to load image data into texture: { imagePath }");
1764+
Debug.LogError($"LWGUI: Image path not found: { inputPath }");
17191765
}
17201766
}
1721-
else
1767+
catch (Exception ex)
17221768
{
1723-
Debug.LogError($"LWGUI: Image path not found: { imagePath }");
1769+
Debug.LogError($"LWGUI: Exception while resolving image path '{inputPath}': {ex.Message}");
17241770
}
17251771
}
17261772

Editor/Timeline/Editor/LWGUI.Timeline.Editor.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"LWGUI.Timeline",
77
"LWGUI.Runtime.Timeline",
88
"Unity.Timeline",
9-
"Unity.Timeline.Editor"
9+
"Unity.Timeline.Editor",
10+
"LWGUI.Runtime",
11+
"Unity.InternalAPIEditorBridge.020"
1012
],
1113
"includePlatforms": [
1214
"Editor"

Test/LWGUI_RampAtlas.tga.meta

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Test/SampleDrawerA.shader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
[SubToggle(Preset)] _ZWrite ("ZWrite ", Float) = 1
6161
[SubEnum(Preset, UnityEngine.Rendering.CompareFunction)] _ZTest ("ZTest", Float) = 4 // 4 is LEqual
6262
[SubEnum(Preset, RGBA, 15, RGB, 14)] _ColorMask ("ColorMask", Float) = 15 // 15 is RGBA (binary 1111)
63-
[BitMask(Preset)] _Stencil ("Stencil", Integer) = 0
64-
[BitMask(Preset, Left, Bit6, Bit5, Bit4, Description, Bit2, Bit1, Right)] _StencilWithDescription ("Stencil With Description", Integer) = 0
63+
[BitMask(Preset)] _Stencil ("Stencil", Int) = 0
64+
[BitMask(Preset, Left, Bit6, Bit5, Bit4, Description, Bit2, Bit1, Right)] _StencilWithDescription ("Stencil With Description", Int) = 0
6565

6666
}
6767

Test/SampleDrawerB.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
[Advanced][Tex(Group2, _AdvancedColor0)] _AdvancedTex1 ("Advanced Tex 1", 2D) = "white" { }
3838
[Advanced][HideInInspector] _AdvancedColor0 ("Advanced Color 0", Color) = (1, 1, 1, 1)
3939
[AdvancedHeaderProperty][Sub(Group2)] _AdvancedFloat ("Advanced Image", float) = 0
40-
[Advanced][Image(Group2)] _AdvancedImage ("../image-20220828003810353.png", float) = 0
40+
[Advanced][Image(Group2)] _AdvancedImage ("../assets~/809c4a1c-ce80-48b1-b415-7e8d4bea716e-16616214059841.png", float) = 0
4141

4242
[Title(Channel Samples)]
4343
[Channel] _textureChannelMask ("Texture Channel Mask (Default G)", Vector) = (0, 1, 0, 0)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.jasonma.lwgui",
3-
"version": "1.27.1",
3+
"version": "1.27.2",
44
"displayName": "LWGUI",
55
"description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.",
66
"keywords": [

0 commit comments

Comments
 (0)