This commit is contained in:
blueloveTH 2023-06-24 21:10:24 +08:00
parent 5dbd198c32
commit ee2d69dcd6
2 changed files with 35 additions and 3 deletions

32
docs/unity/console.md Normal file
View File

@ -0,0 +1,32 @@
---
label: Python Console
icon: dot
order: 5
---
You can open the Python console in Unity by clicking the `Window/Python Console` menu item.
By default, the console creates a unmodified `VM` instance to execute your code.
You may want to provide an enhanced `VM` instance for the console in Unity Editor.
For example, adding some class bindings in `UnityEngine` namespace.
To do this, you need to create a class derived from `VM` and put it in `Assets/Editor/` folder.
By adding `[EditorVM]` attribute to the class,
the console will use it instead of the default `VM` instance.
```csharp
using UnityEngine;
using PocketPython;
[EditorVM] // this attribute is required
public class EnhancedVM: VM{
public EnhancedVM() {
RegisterAutoType<GameObject>(builtins);
RegisterAutoType<Transform>(builtins);
RegisterAutoType<Vector2>(builtins);
RegisterAutoType<Vector3>(builtins);
// ...
}
}
```

View File

@ -43,7 +43,7 @@ The features marked with `YES` are supported, and the features marked with `NO`
| Unpacking | `a, b = 1, 2` | YES | YES | | Unpacking | `a, b = 1, 2` | YES | YES |
| Star Unpacking | `a, *b = [1, 2, 3]` | YES | YES | | Star Unpacking | `a, *b = [1, 2, 3]` | YES | YES |
| Exception | `raise/try..catch` | YES | NO | | Exception | `raise/try..catch` | YES | NO |
| Dynamic Code | `eval()/exec()` | YES | NO | | Dynamic Code | `eval()/exec()` | YES | YES |
| Reflection | `hasattr()/getattr()/setattr()` | YES | YES | | Reflection | `hasattr()/getattr()/setattr()` | YES | YES |
| Import | `import/from..import` | YES | YES | | Import | `import/from..import` | YES | YES |
| Context Block | `with <expr> as <id>:` | YES | NO | | Context Block | `with <expr> as <id>:` | YES | NO |
@ -65,7 +65,7 @@ which means passing arguments between C# and Python is extremely easy and intuit
| Python Type | C# Type | | Python Type | C# Type |
| ----------- | ------- | | ----------- | ------- |
| `None` | `PocketPy.NoneType` | | `None` | `NoneType` |
| `object` | `System.Object` | | `object` | `System.Object` |
| `bool` | `System.Boolean` | | `bool` | `System.Boolean` |
| `int` | `System.Int32` | | `int` | `System.Int32` |
@ -73,7 +73,7 @@ which means passing arguments between C# and Python is extremely easy and intuit
| `str` | `System.String` | | `str` | `System.String` |
| `tuple` | `System.Object[]` | | `tuple` | `System.Object[]` |
| `list` | `System.Collections.Generic.List<object>` | | `list` | `System.Collections.Generic.List<object>` |
| `dict` | `System.Collections.Generic.Dictionary<PocketPy.PyDictKey, object>` | | `dict` | `System.Collections.Generic.Dictionary<PyDictKey, object>` |
| ... | ... | | ... | ... |
### Python Console in Editor ### Python Console in Editor