FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Unity-PythonNet/Assets/Scripts/PlotRandom.cs at main · shiena/Unity-PythonNet · GitHub
shiena
/
Unity-PythonNet
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
41
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Unity-PythonNet
/
Assets
/
Scripts
/
PlotRandom.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
106 lines (97 loc) · 3.27 KB
Breadcrumbs
Unity-PythonNet
/
Assets
/
Scripts
/
PlotRandom.cs
Copy path
File metadata and controls
106 lines (97 loc) · 3.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using
Python
.
Runtime
;
using
System
;
using
System
.
Threading
;
using
System
.
Threading
.
Tasks
;
using
UnityEngine
;
using
UnityEngine
.
UI
;
namespace
UnityPython
{
[
DefaultExecutionOrder
(
-
1
)
]
public
class
PlotRandom
:
MonoBehaviour
{
[
SerializeField
]
private
RawImage
_rawImage
;
[
SerializeField
]
private
Button
_button
;
private
SemaphoreSlim
_semaphoreSlim
;
private
(
Texture2D
tex
,
int
width
,
int
height
)
_tex
;
private
void
OnEnable
(
)
{
_button
.
onClick
.
AddListener
(
PlotFig
)
;
_semaphoreSlim
=
new
SemaphoreSlim
(
1
,
1
)
;
}
private
void
OnDisable
(
)
{
_button
.
onClick
.
RemoveListener
(
PlotFig
)
;
_semaphoreSlim
.
Dispose
(
)
;
}
private
void
Start
(
)
{
(
_tex
.
width
,
_tex
.
height
)
=
(
2
,
2
)
;
_tex
.
tex
=
new
Texture2D
(
_tex
.
width
,
_tex
.
height
,
TextureFormat
.
RGBA32
,
false
)
{
filterMode
=
FilterMode
.
Point
,
wrapMode
=
TextureWrapMode
.
Clamp
}
;
var
rawData
=
_tex
.
tex
.
GetRawTextureData
(
)
;
Array
.
Fill
(
rawData
,
(
byte
)
255
)
;
_tex
.
tex
.
LoadRawTextureData
(
rawData
)
;
_tex
.
tex
.
Apply
(
)
;
_rawImage
.
texture
=
_tex
.
tex
;
}
private
void
OnDestroy
(
)
{
Destroy
(
_tex
.
tex
)
;
}
private
async
void
PlotFig
(
)
{
IntPtr
?
state
=
null
;
try
{
await
_semaphoreSlim
.
WaitAsync
(
destroyCancellationToken
)
;
state
=
PythonEngine
.
BeginAllowThreads
(
)
;
var
(
bytes
,
w
,
h
)
=
await
Task
.
Run
(
Plot
,
destroyCancellationToken
)
;
LoadImage
(
bytes
,
w
,
h
)
;
}
catch
(
OperationCanceledException
e
)
when
(
e
.
CancellationToken
!=
destroyCancellationToken
)
{
throw
;
}
finally
{
if
(
state
.
HasValue
)
{
PythonEngine
.
EndAllowThreads
(
state
.
Value
)
;
}
_semaphoreSlim
.
Release
(
)
;
}
}
private
(
byte
[
]
,
int
,
int
)
Plot
(
)
{
using
(
Py
.
GIL
(
)
)
{
using
dynamic
plotRandom
=
Py
.
Import
(
"plot_random"
)
;
using
dynamic
ret
=
plotRandom
.
draw
(
)
;
using
(
ret
[
0
]
)
// To prevent GC of the numpy array, keep the byte array read from 'addr' on the C# side until it is fully loaded.
{
var
(
addr
,
w
,
h
,
s
)
=
(
(
long
)
ret
[
1
]
,
(
int
)
ret
[
2
]
,
(
int
)
ret
[
3
]
,
(
int
)
ret
[
4
]
)
;
var
ptr
=
new
IntPtr
(
addr
)
;
ReadOnlySpan
<
byte
>
span
;
unsafe
{
span
=
new
ReadOnlySpan
<
byte
>
(
ptr
.
ToPointer
(
)
,
w
*
h
*
s
)
;
}
return
(
span
.
ToArray
(
)
,
w
,
h
)
;
}
}
}
private
void
LoadImage
(
ReadOnlySpan
<
byte
>
rawData
,
int
w
,
int
h
)
{
if
(
_tex
.
width
!=
w
||
_tex
.
height
!=
h
)
{
(
_tex
.
width
,
_tex
.
height
)
=
(
w
,
h
)
;
_tex
.
tex
.
Reinitialize
(
_tex
.
width
,
_tex
.
height
)
;
}
_tex
.
tex
.
LoadRawTextureData
(
rawData
.
ToArray
(
)
)
;
_tex
.
tex
.
Apply
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL