diff --git a/Assets/Editor/PycResetter.cs b/Assets/Editor/PycResetter.cs index c3c4b416..0a49183c 100644 --- a/Assets/Editor/PycResetter.cs +++ b/Assets/Editor/PycResetter.cs @@ -79,6 +79,7 @@ private static void GitClean(string cwd) var targetFolders = new[] { "Assets/StreamingAssets/python-3.11.3-embed-amd64/*", + "Assets/StreamingAssets/new_folder/*", "Assets/StreamingAssets/myproject/*", "Assets/StreamingAssets/test_package/*" }; diff --git a/Assets/Scripts/PlotRandom.cs b/Assets/Scripts/PlotRandom.cs index 332df137..13047006 100644 --- a/Assets/Scripts/PlotRandom.cs +++ b/Assets/Scripts/PlotRandom.cs @@ -75,7 +75,7 @@ private async void PlotFig() { using (Py.GIL()) { - using dynamic plotRandom = Py.Import("plot_random"); + using dynamic plotRandom = Py.Import("plot_random2"); 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. diff --git a/Assets/Scripts/PythonLifeCycle.cs b/Assets/Scripts/PythonLifeCycle.cs index 5113a0b3..e483847a 100644 --- a/Assets/Scripts/PythonLifeCycle.cs +++ b/Assets/Scripts/PythonLifeCycle.cs @@ -32,6 +32,7 @@ public static void Initialize() var testProject = $"{Application.streamingAssetsPath}/{TestProject}"; var pythonPath = string.Join(";", $"{myProject}", + $"{Application.streamingAssetsPath}/new_folder", #if UNITY_EDITOR $"{testProject}", #endif diff --git a/Assets/Scripts/UIEventHandler.cs b/Assets/Scripts/UIEventHandler.cs index 663816fc..b1d587f4 100644 --- a/Assets/Scripts/UIEventHandler.cs +++ b/Assets/Scripts/UIEventHandler.cs @@ -84,7 +84,7 @@ private async void PlotFig() { using (Py.GIL()) { - using dynamic plotRandom = Py.Import("plot_random"); + using dynamic plotRandom = Py.Import("plot_random2"); 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. diff --git a/Assets/StreamingAssets/new_folder/plot_random2.py b/Assets/StreamingAssets/new_folder/plot_random2.py new file mode 100644 index 00000000..238e899b --- /dev/null +++ b/Assets/StreamingAssets/new_folder/plot_random2.py @@ -0,0 +1,28 @@ +import numpy as np +import matplotlib.pyplot as plt +import ctypes + +def draw(): + rand_array = np.random.rand(10000) + + fig = plt.figure() + ax = fig.add_subplot(1, 1, 1) + ax.hist(rand_array, bins=20, ec="b", color="tomato") + + canvas = plt.get_current_fig_manager().canvas + canvas.draw() + buf = canvas.buffer_rgba() + width, height = canvas.get_width_height() + pixels = np.array(buf, dtype=np.uint8).reshape(height, width, 4) + bytes = np.flipud(pixels).copy() + addr, w, h, s = getarrayaddr(bytes) + return (bytes, addr, w, h, s) + +def getarrayaddr(pixels): + h, w, s = pixels.shape + c_ptr = pixels.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8 * w * h * s)).contents + return (ctypes.addressof(c_ptr), w, h, s) + +if __name__ == '__main__': + bytes, addr, w, h, s = draw() + print(addr, w, h, s) \ No newline at end of file diff --git a/Assets/StreamingAssets/new_folder/plot_random2.py.meta b/Assets/StreamingAssets/new_folder/plot_random2.py.meta new file mode 100644 index 00000000..e64034ed --- /dev/null +++ b/Assets/StreamingAssets/new_folder/plot_random2.py.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: ac8d1e275c9e35345a4ee3c174dc6da6 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: