Skip to content

Commit

Permalink
fix: rendering issues when playing with opening a prefab stage
Browse files Browse the repository at this point in the history
close #345
  • Loading branch information
mob-sakai committed Dec 18, 2024
1 parent 1a748f1 commit 95235a9
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Packages/src/Runtime/Internal/Extensions/SpriteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ internal static class SpriteExtensions
Type.GetType("UnityEditor.Experimental.U2D.SpriteEditorExtension, UnityEditor")
?? Type.GetType("UnityEditor.U2D.SpriteEditorExtension, UnityEditor");

private static readonly MethodInfo s_GetActiveAtlasTextureMethod = s_SpriteEditorExtensionType
.GetMethod("GetActiveAtlasTexture", BindingFlags.Static | BindingFlags.NonPublic);
private static readonly Func<Sprite, Texture2D> s_GetActiveAtlasTextureMethod =
(Func<Sprite, Texture2D>)Delegate.CreateDelegate(typeof(Func<Sprite, Texture2D>),
s_SpriteEditorExtensionType
.GetMethod("GetActiveAtlasTexture", BindingFlags.Static | BindingFlags.NonPublic));

private static readonly MethodInfo s_GetActiveAtlasMethod = s_SpriteEditorExtensionType
.GetMethod("GetActiveAtlas", BindingFlags.Static | BindingFlags.NonPublic);
private static readonly Func<Sprite, SpriteAtlas> s_GetActiveAtlasMethod =
(Func<Sprite, SpriteAtlas>)Delegate.CreateDelegate(typeof(Func<Sprite, SpriteAtlas>),
s_SpriteEditorExtensionType
.GetMethod("GetActiveAtlas", BindingFlags.Static | BindingFlags.NonPublic));

/// <summary>
/// Get the actual texture of a sprite in play mode or edit mode.
Expand All @@ -30,9 +34,7 @@ public static Texture2D GetActualTexture(this Sprite self)
{
if (!self) return null;

if (Application.isPlaying) return self.texture;

var ret = s_GetActiveAtlasTextureMethod.Invoke(null, new object[] { self }) as Texture2D;
var ret = s_GetActiveAtlasTextureMethod(self);
return ret ? ret : self.texture;
}

Expand All @@ -43,7 +45,7 @@ public static SpriteAtlas GetActiveAtlas(this Sprite self)
{
if (!self) return null;

return s_GetActiveAtlasMethod.Invoke(null, new object[] { self }) as SpriteAtlas;
return s_GetActiveAtlasMethod(self);
}
#else
/// <summary>
Expand Down

0 comments on commit 95235a9

Please sign in to comment.