Skip to content

Commit 24b35a4

Browse files
add a new [HelpURL(...)] feature
example use: add [HelpURL(github.com, JasonMa0012, LWGUI)] above a LWGUI main group in .shader file develop notes: - following cursor rules in LWGUI repo - request agent to only write minimum code change to make code review easier - Cursor2.5 - Opus4.6 fixed also a regex compile error in Unity2021.3.45f2 due to missing using in GUIStyles.cs
1 parent b85cf63 commit 24b35a4

File tree

9 files changed

+170
-0
lines changed

9 files changed

+170
-0
lines changed

Editor/Helper/GUIStyles.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.RegularExpressions;
12
using UnityEditor;
23
using UnityEngine;
34

Editor/Helper/Helper.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,45 @@ public static void DrawHelpbox(PropertyStaticData propertyStaticData, PropertyDy
316316
}
317317
}
318318

319+
public static readonly float helpURLButtonWidth = 18f;
320+
321+
private static GUIContent _helpURLIconCache;
322+
private static GUIContent _helpURLIcon => _helpURLIconCache ??= new GUIContent(EditorGUIUtility.IconContent("_Help")) { tooltip = string.Empty };
323+
324+
public static Rect GetHelpURLRect(Rect parentRect)
325+
{
326+
return new Rect(parentRect.xMax - helpURLButtonWidth - 4f,
327+
parentRect.yMax - (EditorGUIUtility.singleLineHeight + helpURLButtonWidth) * 0.5f - 4.5f,
328+
helpURLButtonWidth,
329+
helpURLButtonWidth);
330+
}
331+
332+
public static void HandleHelpURLClick(Rect parentRect, string helpURL)
333+
{
334+
if (string.IsNullOrEmpty(helpURL)) return;
335+
336+
var rect = GetHelpURLRect(parentRect);
337+
if (Event.current.type == EventType.MouseDown
338+
&& Event.current.button == 0
339+
&& rect.Contains(Event.current.mousePosition))
340+
{
341+
Application.OpenURL(helpURL);
342+
Event.current.Use();
343+
}
344+
}
345+
346+
public static void DrawHelpURLIcon(Rect parentRect, string helpURL)
347+
{
348+
if (string.IsNullOrEmpty(helpURL)) return;
349+
350+
var rect = GetHelpURLRect(parentRect);
351+
var content = new GUIContent(_helpURLIcon) { tooltip = helpURL };
352+
var enabled = GUI.enabled;
353+
GUI.enabled = true;
354+
GUI.Label(rect, content, GUIStyles.iconButton);
355+
GUI.enabled = enabled;
356+
}
357+
319358
private static Texture _logoCache;
320359
private static GUIContent _logoGuiContentCache;
321360
private static Texture _logo => _logoCache = _logoCache ?? AssetDatabase.LoadAssetAtPath<Texture>(AssetDatabase.GUIDToAssetPath("26b9d845eb7b1a747bf04dc84e5bcc2c"));

Editor/LWGUI.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ private void DrawProperty(MaterialProperty prop)
174174

175175
var revertButtonRect = RevertableHelper.SplitRevertButtonRect(ref rect);
176176

177+
var hasHelpURL = propStaticData.isMain && !string.IsNullOrEmpty(propStaticData.helpURL);
178+
if (hasHelpURL)
179+
Helper.HandleHelpURLClick(rect, propStaticData.helpURL);
180+
177181
var enabled = GUI.enabled;
178182
if (propStaticData.isReadOnly || !propDynamicData.isActive)
179183
GUI.enabled = false;
@@ -186,6 +190,9 @@ private void DrawProperty(MaterialProperty prop)
186190

187191
RevertableHelper.DrawRevertableProperty(revertButtonRect, prop, metaDatas, propStaticData.isMain || propStaticData.isAdvancedHeaderProperty);
188192
materialEditor.ShaderProperty(rect, prop, label);
193+
194+
if (hasHelpURL)
195+
Helper.DrawHelpURLIcon(rect, propStaticData.helpURL);
189196

190197
Helper.EndProperty(metaDatas, prop);
191198
GUI.enabled = enabled;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Jason Ma
2+
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace LWGUI
7+
{
8+
/// <summary>
9+
/// Display a Help URL button on the right side of the Group.
10+
/// Clicking the button opens the URL in the default browser.
11+
/// "https://" is automatically prepended.
12+
/// Each comma-separated parameter is joined with '/' to form the full URL path.
13+
///
14+
/// url: the document URL without "https://", each ',' = '/'. (Default: none)
15+
/// e.g. [HelpURL(github.com, JasonMa0012, LWGUI)] => https://github.com/JasonMa0012/LWGUI
16+
/// </summary>
17+
public class HelpURLDecorator : SubDrawer
18+
{
19+
private string _url;
20+
21+
private static string Join(params string[] segments) => string.Join("/", segments);
22+
23+
#region
24+
25+
public HelpURLDecorator(string url) { this._url = url; }
26+
public HelpURLDecorator(string s1, string s2) : this(Join(s1, s2)) { }
27+
public HelpURLDecorator(string s1, string s2, string s3) : this(Join(s1, s2, s3)) { }
28+
public HelpURLDecorator(string s1, string s2, string s3, string s4) : this(Join(s1, s2, s3, s4)) { }
29+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5) : this(Join(s1, s2, s3, s4, s5)) { }
30+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6) : this(Join(s1, s2, s3, s4, s5, s6)) { }
31+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7) : this(Join(s1, s2, s3, s4, s5, s6, s7)) { }
32+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8)) { }
33+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8, s9)) { }
34+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10)) { }
35+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10, string s11) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11)) { }
36+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10, string s11, string s12) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12)) { }
37+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10, string s11, string s12, string s13) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13)) { }
38+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10, string s11, string s12, string s13, string s14) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14)) { }
39+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10, string s11, string s12, string s13, string s14, string s15) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15)) { }
40+
public HelpURLDecorator(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8, string s9, string s10, string s11, string s12, string s13, string s14, string s15, string s16) : this(Join(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16)) { }
41+
42+
#endregion
43+
44+
45+
protected override float GetVisibleHeight(MaterialProperty prop) { return 0; }
46+
47+
public override void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData)
48+
{
49+
inoutPropertyStaticData.helpURL = "https://" + _url.Replace(" ", "");
50+
}
51+
52+
public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { }
53+
}
54+
}

Editor/ShaderDrawers/ExtraDecorators/Appearance/HelpURLDecorator.cs.meta

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

Editor/ShaderDrawers/ShaderDrawerBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public partial class PropertyStaticData
3333
public List<string> buttonCommands = new();
3434
public List<float> buttonDisplayNameWidths = new();
3535

36+
// HelpURL
37+
public string helpURL = string.Empty;
38+
3639
// You can add more data that is determined during the initialization of the Drawer as a cache here,
3740
// thereby avoiding the need to calculate it every frame in OnGUI().
3841
// >>>>>>>>>>>>>>>>>>>>>>>> Add new data here <<<<<<<<<<<<<<<<<<<<<<<

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,32 @@ Tips:
867867
public ReadOnlyDecorator()
868868
```
869869

870+
#### HelpURL
871+
872+
```c#
873+
/// Display a Help URL button on the right side of the Group.
874+
/// Clicking the button opens the URL in the default browser.
875+
/// "https://" is automatically prepended.
876+
/// Each comma-separated parameter is joined with '/' to form the full URL path.
877+
///
878+
/// url: the document URL without "https://", each ',' = '/'. (Default: none)
879+
/// e.g. [HelpURL(github.com, JasonMa0012, LWGUI)] => https://github.com/JasonMa0012/LWGUI
880+
public HelpURLDecorator(string url)
881+
public HelpURLDecorator(string s1, string s2)
882+
...
883+
public HelpURLDecorator(string s1, string s2, ..., string s16)
884+
```
885+
886+
Example:
887+
888+
```c#
889+
[HelpURL(github.com, JasonMa0012, LWGUI)]
890+
[Main(GroupName)] _group ("Group", float) = 0
891+
892+
[HelpURL(github.com, JasonMa0012, LWGUI, blob, 1.x, README.md)]
893+
[Main(GroupName2, _, on, off)] _group2 ("Group2", float) = 0
894+
```
895+
870896
### Logic
871897

872898
#### PassSwitch

README_CN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,32 @@ Tips:
864864
public ReadOnlyDecorator()
865865
```
866866

867+
#### HelpURL
868+
869+
```c#
870+
/// 在Group右侧显示一个帮助文档链接按钮.
871+
/// 点击后会在默认浏览器中打开指定URL.
872+
/// 自动添加"https://"前缀.
873+
/// 每个逗号分隔的参数以'/'连接, 组成完整URL路径.
874+
///
875+
/// url: 不含"https://"的文档URL, 每个','='/' (默认: 无)
876+
/// 例: [HelpURL(github.com, JasonMa0012, LWGUI)] => https://github.com/JasonMa0012/LWGUI
877+
public HelpURLDecorator(string url)
878+
public HelpURLDecorator(string s1, string s2)
879+
...
880+
public HelpURLDecorator(string s1, string s2, ..., string s16)
881+
```
882+
883+
示例:
884+
885+
```c#
886+
[HelpURL(github.com, JasonMa0012, LWGUI)]
887+
[Main(GroupName)] _group ("Group", float) = 0
888+
889+
[HelpURL(github.com, JasonMa0012, LWGUI, blob, 1.x, README.md)]
890+
[Main(GroupName2, _, on, off)] _group2 ("Group2", float) = 0
891+
```
892+
867893
### Logic
868894

869895
#### PassSwitch

Test/SampleDrawerA.shader

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
[Title(Title on Group)]
1717
// Create a folding group with name "g1"
18+
[HelpURL(github.com, JasonMa0012, LWGUI)]
1819
[Main(g1)] _group ("Group", float) = 0
1920
[Sub(g1)] _float ("float", float) = 2
2021

@@ -37,6 +38,7 @@
3738

3839

3940
// Create a drop-down menu that opens by default, without toggle
41+
[HelpURL(github.com, JasonMa0012, LWGUI, blob, 1.x, README.md)]
4042
[Main(g2, _KEYWORD, on, off)] _group2 ("group2 without toggle", float) = 1
4143
[Tooltip(Test Tooltip)]
4244
[Helpbox(Test Helpbox)]
@@ -52,6 +54,7 @@
5254
[RampAtlasIndexer(g2, _RampAtlas, Green, Linear, GA, 24)] _RampAtlasIndex2 ("Indexer Linear/Green/24", float) = 3
5355

5456

57+
[HelpURL(github.com, JasonMa0012, LWGUI, blob, 1.x, package.json)]
5558
[Main(Preset, _, on, off)] _PresetGroup ("Preset Samples", float) = 0
5659
[Preset(Preset, LWGUI_Preset_BlendMode)] _BlendMode ("Blend Mode Preset", float) = 0
5760
[SubEnum(Preset, UnityEngine.Rendering.CullMode)] _Cull ("Cull", Float) = 2

0 commit comments

Comments
 (0)