Skip to content

Commit

Permalink
fix: Fixed MissingReferenceException sometimes being reported in Edit…
Browse files Browse the repository at this point in the history
…orIcons on MacOS
  • Loading branch information
SolidAlloy committed Sep 29, 2021
1 parent 91320a3 commit a363c70
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 47 deletions.
24 changes: 22 additions & 2 deletions Editor/Helpers/EditorIcons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,30 @@ public static class EditorIcons
public static readonly Texture2D Error = (Texture2D) EditorGUIUtility.Load("console.erroricon");

/// <summary>Triangle with one of the vertices looking to the right. Useful in foldout menus.</summary>
public static readonly EditorIcon TriangleRight = new EditorIcon(Database.TriangleRight);
public static readonly EditorIcon TriangleRight;

/// <summary>Triangle with one of the vertices looking to the bottom. Useful in foldout menus.</summary>
public static readonly EditorIcon TriangleDown = new EditorIcon(Database.TriangleRight.Rotate());
public static readonly EditorIcon TriangleDown;

static EditorIcons()
{
// The icon sometimes reports MissingReferenceException on MacOS for some reason.
if (Database.TriangleRight == null)
{
const string triangleRightGuid = "bb26edf4be136b0459bfdf7ff0c7455b";
string path = AssetDatabase.GUIDToAssetPath(triangleRightGuid);
Database.TriangleRight = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
}

TriangleRight = new EditorIcon(Database.TriangleRight);
TriangleDown = new EditorIcon(Database.TriangleRight.Rotate());

AssemblyReloadEvents.beforeAssemblyReload += () =>
{
TriangleRight.Dispose();
TriangleDown.Dispose();
};
}

private static EditorIconsDatabase GetDatabase()
{
Expand Down
102 changes: 57 additions & 45 deletions Editor/Helpers/EditorIconsRelated/EditorIcon.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,70 @@
namespace SolidUtilities.Editor.Helpers.EditorIconsRelated
{
using Helpers;
using JetBrains.Annotations;
using UnityEngine;

/// <summary>
/// Icon that can have different tints depending on its state: active, highlighted, etc. Useful for creating custom
/// inspectors and drawers.
/// </summary>
[PublicAPI]
public readonly struct EditorIcon
{
/// <summary>Icon with the default color.</summary>
public readonly Texture2D Default;

/// <summary>Icon with the active state tint.</summary>
public readonly Texture2D Active;

/// <summary>Icon with the highlighted state tint.</summary>
public readonly Texture2D Highlighted;

public EditorIcon(Texture2D icon)
using System;
using Helpers;
using JetBrains.Annotations;
using UnityEngine;
using Object = UnityEngine.Object;

/// <summary>
/// Icon that can have different tints depending on its state: active, highlighted, etc. Useful for creating custom
/// inspectors and drawers.
/// </summary>
[PublicAPI]
public readonly struct EditorIcon : IDisposable
{
Default = icon;
Highlighted = null;
Active = null;
/// <summary>Icon with the default color.</summary>
public readonly Texture2D Default;

Highlighted = GetIconWithMaterial(EditorIcons.Database.Highlighted);
Active = GetIconWithMaterial(EditorIcons.Database.Active);
}
/// <summary>Icon with the active state tint.</summary>
public readonly Texture2D Active;

private Texture2D GetIconWithMaterial(Material material)
{
Texture2D newTexture = null;
/// <summary>Icon with the highlighted state tint.</summary>
public readonly Texture2D Highlighted;

public EditorIcon(Texture2D icon)
{
// This way we can control the lifecycle of the textures ourselves, not fearing that the original texture will be destroyed.
Default = new Texture2D(icon.width, icon.height, icon.format, icon.mipmapCount != 1);
Graphics.CopyTexture(icon, Default);

Highlighted = null;
Active = null;

using (new TextureHelper.SRGBWriteScope(true))
{
using (var temporary = new TemporaryActiveTexture(Default.width, Default.height, 0))
Highlighted = GetIconWithMaterial(EditorIcons.Database.Highlighted);
Active = GetIconWithMaterial(EditorIcons.Database.Active);
}

private Texture2D GetIconWithMaterial(Material material)
{
GL.Clear(false, true, new Color(1f, 1f, 1f, 0f));
Graphics.Blit(Default, temporary, material);
Texture2D newTexture = null;

using (new TextureHelper.SRGBWriteScope(true))
{
using (var temporary = new TemporaryActiveTexture(Default.width, Default.height, 0))
{
GL.Clear(false, true, new Color(1f, 1f, 1f, 0f));
Graphics.Blit(Default, temporary, material);

newTexture = new Texture2D(Default.width, Default.width, TextureFormat.ARGB32, false, true)
{
filterMode = FilterMode.Bilinear
};
newTexture = new Texture2D(Default.width, Default.width, TextureFormat.ARGB32, false, true)
{
filterMode = FilterMode.Bilinear
};

newTexture.ReadPixels(new Rect(0.0f, 0.0f, Default.width, Default.width), 0, 0);
newTexture.alphaIsTransparency = true;
newTexture.Apply();
newTexture.ReadPixels(new Rect(0.0f, 0.0f, Default.width, Default.width), 0, 0);
newTexture.alphaIsTransparency = true;
newTexture.Apply();
}
}

return newTexture;
}
}

return newTexture;
public void Dispose()
{
Object.DestroyImmediate(Default);
Object.DestroyImmediate(Highlighted);
Object.DestroyImmediate(Active);
}
}
}
}

0 comments on commit a363c70

Please sign in to comment.