Skip to content

Commit

Permalink
Fixed UI textures getting destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Sep 7, 2019
1 parent e290947 commit 62377b7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
17 changes: 2 additions & 15 deletions RuntimeUnityEditor/RuntimeUnityEditorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class RuntimeUnityEditorCore

private CursorLockMode _previousCursorLockState;
private bool _previousCursorVisible;
private GUISkin _customSkin;

internal RuntimeUnityEditorCore(MonoBehaviour pluginObject, ILoggerWrapper logger, string configPath)
{
Expand Down Expand Up @@ -74,21 +73,8 @@ internal void OnGUI()
{
if (Show)
{
if (_customSkin == null)
{
try
{
_customSkin = InterfaceMaker.CreateSkin();
}
catch (Exception ex)
{
Logger.Log(LogLevel.Warning, "Could not load custom GUISkin - " + ex.Message);
_customSkin = GUI.skin;
}
}

var originalSkin = GUI.skin;
GUI.skin = _customSkin;
GUI.skin = InterfaceMaker.CustomSkin;

Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
Expand All @@ -97,6 +83,7 @@ internal void OnGUI()
TreeViewer.DisplayViewer();
Repl?.DisplayWindow();

// Restore old skin for maximum compatibility
GUI.skin = originalSkin;
}
}
Expand Down
42 changes: 36 additions & 6 deletions RuntimeUnityEditor/UI/InterfaceMaker.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
using RuntimeUnityEditor.Core.Utils;
using System;
using RuntimeUnityEditor.Core.Utils;
using UnityEngine;
using Object = UnityEngine.Object;

namespace RuntimeUnityEditor.Core.UI
{
public static class InterfaceMaker
{
// These all need to be held as static properties, including textures, to prevent UnloadUnusedAssets from destroying them
private static Texture2D _boxBackground;
private static Texture2D _winBackground;
private static GUISkin _customSkin;

public static void EatInputInRect(Rect eatRect)
{
if (eatRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))
Input.ResetInputAxes();
}

public static GUISkin CreateSkin()
public static GUISkin CustomSkin
{
get
{
if (_customSkin == null)
{
try
{
_customSkin = CreateSkin();
}
catch (Exception ex)
{
RuntimeUnityEditorCore.Logger.Log(LogLevel.Warning, "Could not load custom GUISkin - " + ex.Message);
_customSkin = GUI.skin;
}
}

return _customSkin;
}
}

private static GUISkin CreateSkin()
{
var newSkin = Object.Instantiate(GUI.skin);
Object.DontDestroyOnLoad(newSkin);

// Load the custom skin from resources
var texData = ResourceUtils.GetEmbeddedResource("guisharp-box.png");
var boxTex = UnityFeatureHelper.LoadTexture(texData);
_boxBackground = UnityFeatureHelper.LoadTexture(texData);
Object.DontDestroyOnLoad(_boxBackground);
newSkin.box.onNormal.background = null;
newSkin.box.normal.background = boxTex;
newSkin.box.normal.background = _boxBackground;
newSkin.box.normal.textColor = Color.white;

texData = ResourceUtils.GetEmbeddedResource("guisharp-window.png");
var winTex = UnityFeatureHelper.LoadTexture(texData);
_winBackground = UnityFeatureHelper.LoadTexture(texData);
Object.DontDestroyOnLoad(_winBackground);
newSkin.window.onNormal.background = null;
newSkin.window.normal.background = winTex;
newSkin.window.normal.background = _winBackground;
newSkin.window.padding = new RectOffset(6, 6, 22, 6);
newSkin.window.border = new RectOffset(10, 10, 20, 10);
newSkin.window.normal.textColor = Color.white;
Expand Down

0 comments on commit 62377b7

Please sign in to comment.