Skip to content

Commit 2abb49c

Browse files
committed
add sample scene
1 parent 2ba617e commit 2abb49c

81 files changed

Lines changed: 13784 additions & 99 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Assets/Scenes/SampleScene.unity

Lines changed: 503 additions & 97 deletions
Large diffs are not rendered by default.

Assets/Scripts/PlotRandom.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Python.Runtime;
2+
using System;
3+
using UnityEngine;
4+
using UnityEngine.UI;
5+
6+
namespace UnityPython
7+
{
8+
[DefaultExecutionOrder(-1)]
9+
public class PlotRandom : MonoBehaviour
10+
{
11+
[SerializeField] private RawImage _rawImage;
12+
[SerializeField] private Button _button;
13+
14+
private void OnEnable()
15+
{
16+
_button.onClick.AddListener(PlotFig);
17+
}
18+
19+
private void OnDisable()
20+
{
21+
_button.onClick.RemoveListener(PlotFig);
22+
}
23+
24+
private void PlotFig()
25+
{
26+
using (Py.GIL())
27+
{
28+
using dynamic plotRandom = Py.Import("plot_random");
29+
30+
// numpy配列のGCを防ぐためにC#側で保持して別途アドレス変換を呼び出す
31+
using var pixels = plotRandom.draw();
32+
using var ret = plotRandom.getarrayaddr(pixels);
33+
var (addr, w, h, s) = ((long)ret[0], (int)ret[1], (int)ret[2], (int)ret[3]);
34+
35+
var ptr = new IntPtr(addr);
36+
ReadOnlySpan<byte> span;
37+
unsafe
38+
{
39+
span = new ReadOnlySpan<byte>(ptr.ToPointer(), w*h*s);
40+
}
41+
LoadImage(span, w, h);
42+
}
43+
}
44+
45+
private void LoadImage(ReadOnlySpan<byte> rawData, int w, int h)
46+
{
47+
var tex = new Texture2D(w, h, TextureFormat.RGBA32, false)
48+
{
49+
filterMode = FilterMode.Point
50+
};
51+
tex.LoadRawTextureData(rawData.ToArray());
52+
tex.Apply();
53+
Destroy(_rawImage.texture);
54+
_rawImage.texture = tex;
55+
}
56+
57+
private void OnDestroy()
58+
{
59+
Destroy(_rawImage.texture);
60+
}
61+
}
62+
}

Assets/Scripts/PlotRandom.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/UnityPython.asmdef

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"references": [],
55
"includePlatforms": [],
66
"excludePlatforms": [],
7-
"allowUnsafeCode": false,
7+
"allowUnsafeCode": true,
88
"overrideReferences": true,
99
"precompiledReferences": [
1010
"Python.Runtime.dll"

Assets/StreamingAssets/myproject.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import ctypes
4+
5+
def draw():
6+
rand_array = np.random.rand(10000)
7+
8+
fig = plt.figure()
9+
ax = fig.add_subplot(1, 1, 1)
10+
ax.hist(rand_array, bins=20, ec="b")
11+
12+
canvas = plt.get_current_fig_manager().canvas
13+
canvas.draw()
14+
buf = canvas.buffer_rgba()
15+
width, height = canvas.get_width_height()
16+
pixels = np.frombuffer(buf, dtype=np.uint8).reshape(height, width, 4)
17+
return np.flipud(pixels).copy()
18+
19+
def getarrayaddr(pixels):
20+
h, w, s = pixels.shape
21+
c_addr = pixels.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8 * w * h * s)).contents
22+
return (ctypes.addressof(c_addr), w, h, s)
23+
24+
if __name__ == '__main__':
25+
array = draw()
26+
ptr, w, h, s = getarrayaddr(array)
27+
print(ptr, w, h, s)

Assets/StreamingAssets/myproject/plot_random.py.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/TextMesh Pro.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/TextMesh Pro/Documentation.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

0 commit comments

Comments
 (0)