Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use static styles to prevent Unity leaks #3201

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions ksp_plugin_adapter/style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
namespace ksp_plugin_adapter {

internal static class Style {
static Style() {
horizontal_line_style_ =
new UnityEngine.GUIStyle(UnityEngine.GUI.skin.horizontalSlider);
horizontal_line_style_.fixedHeight /= 5;
horizontal_line_style_.normal.background = ultra_cool_grey_texture;
line_spacing_style_ = new UnityEngine.GUIStyle(UnityEngine.GUI.skin.label);
line_spacing_style_.fixedHeight /= 5;
}

public static UnityEngine.Color Tangent { get; } = XKCDColors.NeonYellow;
public static UnityEngine.Color Normal { get; } = XKCDColors.AquaBlue;
public static UnityEngine.Color Binormal { get; } = XKCDColors.PurplePink;
Expand Down Expand Up @@ -61,18 +70,11 @@ public static UnityEngine.GUIStyle Multiline(UnityEngine.GUIStyle style) {
}

public static void HorizontalLine() {
var horizontal_line_style =
new UnityEngine.GUIStyle(UnityEngine.GUI.skin.horizontalSlider);
horizontal_line_style.fixedHeight /= 5;
horizontal_line_style.normal.background = ultra_cool_grey_texture;
UnityEngine.GUILayout.Label("", horizontal_line_style);
UnityEngine.GUILayout.Label("", horizontal_line_style_);
}

public static void LineSpacing() {
var horizontal_line_style =
new UnityEngine.GUIStyle(UnityEngine.GUI.skin.label);
horizontal_line_style.fixedHeight /= 5;
UnityEngine.GUILayout.Label("", horizontal_line_style);
UnityEngine.GUILayout.Label("", line_spacing_style_);
}

private static UnityEngine.Texture2D ultra_cool_grey_texture {
Expand All @@ -90,6 +92,12 @@ private static UnityEngine.Texture2D ultra_cool_grey_texture {
// Close to XKCD "cool grey", but hue-neutral
private static readonly UnityEngine.Color ultra_cool_grey_ =
new UnityEngine.Color(0.64f, 0.64f, 0.64f);

// It is very important to use *static* styles for these elements. If we use
// local variables at the place of use, the stupid Unity ends up burning 9 kiB
// for each horizontal line we display. See #3064.
private static readonly UnityEngine.GUIStyle horizontal_line_style_;
private static readonly UnityEngine.GUIStyle line_spacing_style_;
}

} // namespace ksp_plugin_adapter
Expand Down