Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Commit 90debb2

Browse files
author
DaZombieKiller
committed
Added more extension methods
1 parent 1077fb3 commit 90debb2

4 files changed

Lines changed: 67 additions & 6 deletions

File tree

Build/Projects/PluginManager.definition

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
<Files>
2424
<Compile Include="PluginManager.cs" />
2525
<Compile Include="Plugin/Attributes.cs" />
26-
<Compile Include="Plugin/Extensions/Extensions.cs" />
26+
<Compile Include="Plugin/Extensions/*.cs" />
2727
</Files>
2828
</Project>

Source/PluginManager/Plugin/Extensions/Extensions.cs renamed to Source/PluginManager/Plugin/Extensions/GameObjectExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
namespace PluginManager.Plugin.Extensions
55
{
6-
/// <summary></summary>
7-
public static class Extensions
6+
/// <summary>Extension methods for the GameObject class.</summary>
7+
public static class GameObjectExtensions
88
{
9-
/// <summary></summary>
10-
/// <param name="self"></param>
11-
/// <param name="component"></param>
9+
/// <summary>Removes a component from a GameObject.</summary>
10+
/// <param name="self">The GameObject.</param>
11+
/// <param name="component">The component type.</param>
1212
public static void RemoveComponent(this GameObject self, Type component)
1313
{
1414
UnityEngine.Object.Destroy(self.GetComponent(component));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using UnityEngine;
2+
3+
namespace PluginManager.Plugin.Extensions
4+
{
5+
/// <summary>Extension methods for the Texture2D class.</summary>
6+
public static class Texture2DExtensions
7+
{
8+
/// <summary>Returns a readable version of a non-readable texture.</summary>
9+
/// <param name="self">The non-readable texture.</param>
10+
/// <returns>A readable texture.</returns>
11+
public static Texture2D GetReadable(this Texture2D self)
12+
{
13+
var newTexture = new Texture2D(self.width, self.height);
14+
var prevRender = RenderTexture.active;
15+
16+
var newRender = RenderTexture.GetTemporary(self.width, self.height, 0,
17+
self.format.GetRenderTextureFormat(),
18+
RenderTextureReadWrite.Linear);
19+
20+
Graphics.Blit(self, newRender);
21+
RenderTexture.active = newRender;
22+
23+
newTexture.ReadPixels(new Rect(0, 0, self.width, self.height), 0, 0);
24+
newTexture.Apply();
25+
26+
RenderTexture.active = prevRender;
27+
RenderTexture.ReleaseTemporary(newRender);
28+
29+
return newTexture;
30+
}
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using UnityEngine;
2+
3+
namespace PluginManager.Plugin.Extensions
4+
{
5+
/// <summary>Extension methods for the TextureFormat enum.</summary>
6+
public static class TextureFormatExtensions
7+
{
8+
/// <summary>Converts a TextureFormat to a RenderTextureFormat.</summary>
9+
/// <param name="self">The TextureFormat.</param>
10+
/// <returns>A RenderTextureFormat.</returns>
11+
public static RenderTextureFormat GetRenderTextureFormat(this TextureFormat self)
12+
{
13+
switch (self)
14+
{
15+
case TextureFormat.ARGB32: return RenderTextureFormat.ARGB32;
16+
case TextureFormat.RGB565: return RenderTextureFormat.RGB565;
17+
case TextureFormat.ARGB4444: return RenderTextureFormat.ARGB4444;
18+
case TextureFormat.RGFloat: return RenderTextureFormat.RGFloat;
19+
case TextureFormat.RGHalf: return RenderTextureFormat.RGHalf;
20+
case TextureFormat.RFloat: return RenderTextureFormat.RFloat;
21+
case TextureFormat.RHalf: return RenderTextureFormat.RHalf;
22+
case TextureFormat.R8: return RenderTextureFormat.R8;
23+
case TextureFormat.BGRA32: return RenderTextureFormat.BGRA32;
24+
case TextureFormat.RG16: return RenderTextureFormat.RG16;
25+
default: return RenderTextureFormat.Default;
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)