Skip to content

Commit 1967ae1

Browse files
committed
Update
1 parent 62a81ab commit 1967ae1

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

Assets/CustomTextureRenderer/Runtime/CustomTextureRenderer.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public sealed class CustomTextureRenderer : IDisposable
1717
{
1818
UpdateRawTextureDataFunction _updateRawTextureDataFunction;
1919

20-
Texture _targetTexture;
20+
Texture2D _targetTexture;
2121
int _textureWidth;
2222
int _textureHeight;
2323
int _bytesPerPixel;
2424

2525
bool _disposed;
2626

27-
byte[] _buffer;
27+
uint[] _buffer;
2828
GCHandle _bufferHandle;
2929
IntPtr _bufferPtr;
3030

@@ -47,11 +47,19 @@ public sealed class CustomTextureRenderer : IDisposable
4747
/// <param name="bytesPerPixel"></param>
4848
/// <param name="autoDispose"></param>
4949
public CustomTextureRenderer(UpdateRawTextureDataFunction updateRawTextureDataFunction,
50-
Texture targetTexture, int bytesPerPixel = 4, bool autoDispose = true)
50+
Texture2D targetTexture, int bytesPerPixel = 4, bool autoDispose = true)
5151
{
5252
#if DEVELOPMENT_BUILD || UNITY_EDITOR
5353
_updateRawTextureDataFunctionSampler = CustomSampler.Create("UpdateRawTextureDataFunction");
5454
#endif
55+
56+
if (targetTexture.format != TextureFormat.RGBA32)
57+
{
58+
_disposed = true;
59+
DebugLogError($"[{nameof(NonBlockingCustomTextureRenderer)}] Unsupported texture format: {targetTexture.format}");
60+
return;
61+
}
62+
5563
if (autoDispose){ Application.quitting += Dispose; }
5664

5765
_updateRawTextureDataFunction = updateRawTextureDataFunction;
@@ -62,7 +70,7 @@ public CustomTextureRenderer(UpdateRawTextureDataFunction updateRawTextureDataFu
6270
_textureHeight = targetTexture.height;
6371
_bytesPerPixel = bytesPerPixel;
6472

65-
_buffer = new byte[_targetTexture.width * _targetTexture.height * bytesPerPixel];
73+
_buffer = new uint[_targetTexture.width * _targetTexture.height];
6674
_bufferHandle = GCHandle.Alloc(_buffer, GCHandleType.Pinned);
6775
_bufferPtr = _bufferHandle.AddrOfPinnedObject();
6876
}
@@ -141,5 +149,20 @@ void DebugLog(object message)
141149
{
142150
Debug.Log(message);
143151
}
152+
153+
/// <summary>
154+
/// Logs a message to the Unity Console
155+
/// only when DEVELOPMENT_BUILD or UNITY_EDITOR is defined.
156+
/// </summary>
157+
/// <param name="message"></param>
158+
/// <returns></returns>
159+
[
160+
System.Diagnostics.Conditional("DEVELOPMENT_BUILD"),
161+
System.Diagnostics.Conditional("UNITY_EDITOR"),
162+
]
163+
static void DebugLogError(object message)
164+
{
165+
UnityEngine.Debug.LogError(message);
166+
}
144167
}
145168
}

Assets/CustomTextureRenderer/Runtime/NonBlockingCustomTextureRenderer.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ public sealed class NonBlockingCustomTextureRenderer : IDisposable
1919
{
2020
UpdateRawTextureDataFunction _updateRawTextureDataFunction;
2121

22-
Texture _targetTexture;
22+
Texture2D _targetTexture;
2323
int _textureWidth;
2424
int _textureHeight;
2525
int _bytesPerPixel;
2626

2727
bool _disposed;
2828

29-
byte[] _currentBuffer;
29+
uint[] _currentBuffer;
3030
GCHandle _currentBufferHandle;
3131
IntPtr _currentBufferPtr;
3232

33-
byte[] _nextBuffer;
33+
uint[] _nextBuffer;
3434
GCHandle _nextBufferHandle;
3535
IntPtr _nextBufferPtr;
3636

@@ -58,12 +58,20 @@ public sealed class NonBlockingCustomTextureRenderer : IDisposable
5858
/// <param name="bytesPerPixel"></param>
5959
/// <param name="Dispose"></param>
6060
/// <param name="targetFrameTimeMilliseconds"></param>
61-
public NonBlockingCustomTextureRenderer(UpdateRawTextureDataFunction updateRawTextureDataFunction, Texture targetTexture,
61+
public NonBlockingCustomTextureRenderer(UpdateRawTextureDataFunction updateRawTextureDataFunction, Texture2D targetTexture,
6262
int bytesPerPixel = 4, bool autoDispose = true, int targetFrameTimeMilliseconds = 20)
6363
{
6464
#if DEVELOPMENT_BUILD || UNITY_EDITOR
6565
_updateRawTextureDataFunctionSampler = CustomSampler.Create("UpdateRawTextureDataFunction");
6666
#endif
67+
68+
if (targetTexture.format != TextureFormat.RGBA32)
69+
{
70+
_disposed = true;
71+
DebugLogError($"[{nameof(NonBlockingCustomTextureRenderer)}] Unsupported texture format: {targetTexture.format}");
72+
return;
73+
}
74+
6775
if (autoDispose){ Application.quitting += Dispose; }
6876

6977
_targetFrameTimeMilliseconds = targetFrameTimeMilliseconds;
@@ -76,11 +84,11 @@ public NonBlockingCustomTextureRenderer(UpdateRawTextureDataFunction updateRawTe
7684
_textureHeight = targetTexture.height;
7785
_bytesPerPixel = bytesPerPixel;
7886

79-
_currentBuffer = new byte[_targetTexture.width * _targetTexture.height * bytesPerPixel];
87+
_currentBuffer = new uint[_targetTexture.width * _targetTexture.height];
8088
_currentBufferHandle = GCHandle.Alloc(_currentBuffer, GCHandleType.Pinned);
8189
_currentBufferPtr = _currentBufferHandle.AddrOfPinnedObject();
8290

83-
_nextBuffer = new byte[_targetTexture.width * _targetTexture.height * bytesPerPixel];
91+
_nextBuffer = new uint[_targetTexture.width * _targetTexture.height];
8492
_nextBufferHandle = GCHandle.Alloc(_nextBuffer, GCHandleType.Pinned);
8593
_nextBufferPtr = _nextBufferHandle.AddrOfPinnedObject();
8694

@@ -201,5 +209,20 @@ static void DebugLog(object message)
201209
{
202210
UnityEngine.Debug.Log(message);
203211
}
212+
213+
/// <summary>
214+
/// Logs a message to the Unity Console
215+
/// only when DEVELOPMENT_BUILD or UNITY_EDITOR is defined.
216+
/// </summary>
217+
/// <param name="message"></param>
218+
/// <returns></returns>
219+
[
220+
System.Diagnostics.Conditional("DEVELOPMENT_BUILD"),
221+
System.Diagnostics.Conditional("UNITY_EDITOR"),
222+
]
223+
static void DebugLogError(object message)
224+
{
225+
UnityEngine.Debug.LogError(message);
226+
}
204227
}
205228
}

0 commit comments

Comments
 (0)