From 610635b93a95cd5e1644817dbf4593134678e3d4 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 24 Jun 2023 21:28:16 +0800 Subject: [PATCH] ... --- docs/unity/console.md | 2 +- docs/unity/examples.md | 167 +++++++++++++++++++++++++++++++++++++++++ docs/unity/index.yml | 2 +- docs/unity/vm.md | 2 +- 4 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 docs/unity/examples.md diff --git a/docs/unity/console.md b/docs/unity/console.md index 9d576098..362747ce 100644 --- a/docs/unity/console.md +++ b/docs/unity/console.md @@ -1,5 +1,5 @@ --- -label: Python Console +label: Python console icon: dot order: 5 --- diff --git a/docs/unity/examples.md b/docs/unity/examples.md new file mode 100644 index 00000000..d74d7e4d --- /dev/null +++ b/docs/unity/examples.md @@ -0,0 +1,167 @@ +--- +label: Examples +icon: dot +order: 4 +--- + +See `Assets/PocketPython/Examples` after you import the plugin. + + + +## Primes Example + +```csharp +using UnityEngine; + +namespace PocketPython +{ + + /// + /// Example of using PocketPython to find prime numbers. + /// + public class PrimesExample : MonoBehaviour + { + // Start is called before the first frame update + void Start() + { + var vm = new VM(); + const string source = @" +def is_prime(x): + if x < 2: + return False + for i in range(2, x): + if x % i == 0: + return False + return True + +primes = [i for i in range(2, 20) if is_prime(i)] +print(primes) +"; + CodeObject code = vm.Compile(source, "main.py", CompileMode.EXEC_MODE); + vm.Exec(code); // [2, 3, 5, 7, 11, 13, 17, 19] + } + } + +} +``` + + + +### Vector2 Example + +```csharp +using UnityEngine; + +namespace PocketPython +{ + + /// + /// Example of making UnityEngine.Vector2 available to Python. + /// + public class Vector2Example : MonoBehaviour + { + // Start is called before the first frame update + void Start() + { + var vm = new VM(); + // register UnityEngine.Vector2 type into the builtins module + vm.RegisterAutoType(vm.builtins); + + vm.Exec("print(Vector2)", "main.py"); // + vm.Exec("v = Vector2(1, 2)", "main.py"); + vm.Exec("print(v)", "main.py"); // (1.0, 2.0) + vm.Exec("print(v.x)", "main.py"); // 1.0 + vm.Exec("print(v.y)", "main.py"); // 2.0 + vm.Exec("print(v.magnitude)", "main.py"); // 2.236068 + vm.Exec("print(v.normalized)", "main.py"); // (0.4472136, 0.8944272) + vm.Exec("print(Vector2.Dot(v, v))", "main.py"); // 5.0 + vm.Exec("print(Vector2.get_up())", "main.py"); // (0.0, 1.0) + + Vector2 v = (Vector2)vm.Eval("Vector2(3, 4) + v"); + Debug.Log(v); // (4.0, 6.0) + } + } + +} +``` + + + +### MyClass Example + +```csharp +using UnityEngine; +using System; + +namespace PocketPython +{ + + public class MyClass + { + public string title; + public string msg; + + public void Print() + { + Debug.Log(title + ": " + msg); + } + } + + public class PyMyclassType : PyTypeObject + { + public override string Name => "my_class"; + public override Type CSType => typeof(MyClass); + + [PythonBinding] + public object __new__(PyTypeObject cls) + { + return new MyClass(); + } + + [PythonBinding(BindingType.Getter)] + public string title(MyClass value) => value.title; + + [PythonBinding(BindingType.Getter)] + public string msg(MyClass value) => value.msg; + + [PythonBinding(BindingType.Setter)] + public void title(MyClass value, string title) => value.title = title; + + [PythonBinding(BindingType.Setter)] + public void msg(MyClass value, string msg) => value.msg = msg; + + [PythonBinding] + public void print(MyClass value) => value.Print(); + } + + + /// + /// Example of binding a custom C# class to Python. + /// + public class MyClassExample : MonoBehaviour + { + // Start is called before the first frame update + void Start() + { + var vm = new VM(); + + // register MyClass type into the builtins module + vm.RegisterType(new PyMyclassType(), vm.builtins); + + vm.Exec("print(my_class)", "main.py"); // + vm.Exec("c = my_class()", "main.py"); + vm.Exec("c.title = 'Greeting'", "main.py"); + vm.Exec("c.msg = 'Hello, world!'", "main.py"); + + string title = vm.Eval("c.title").ToString(); + string msg = vm.Eval("c.msg").ToString(); + + Debug.Log(title + ": " + msg); // Greeting: Hello, world! + + vm.Exec("c.print()", "main.py"); // Greeting: Hello, world! + } + } + +} +``` + diff --git a/docs/unity/index.yml b/docs/unity/index.yml index a0024f8e..dd6fdd20 100644 --- a/docs/unity/index.yml +++ b/docs/unity/index.yml @@ -1,3 +1,3 @@ -label: Unity Plugin +label: Unity plugin icon: code order: 0 \ No newline at end of file diff --git a/docs/unity/vm.md b/docs/unity/vm.md index 7c0ad4e0..2564c561 100644 --- a/docs/unity/vm.md +++ b/docs/unity/vm.md @@ -1,5 +1,5 @@ --- -label: Virtual Machine +label: Virtual machine icon: dot order: 20 ---