-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixed MissingReferenceException sometimes being reported in Edit…
…orIcons on MacOS
- Loading branch information
1 parent
91320a3
commit a363c70
Showing
2 changed files
with
79 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |