Skip to content

Commit

Permalink
[unity] update document to compat with 1.4.0-rc.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieyang committed Sep 23, 2022
1 parent 6f77662 commit 2cb7be6
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 71 deletions.
18 changes: 8 additions & 10 deletions doc/unity/en/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ts/js中调用require('./a/b')时,ILoader会被调用并传入字符串".../a/
执行

~~~bash
sudo xattr -r -d com.apple.quarantine puerts.bundle
sudo xattr -r -d com.apple.quarantine puer.bundle
~~~

## 生成代码打包手机版本时报方法(runInEditMode等等)找不到
Expand Down Expand Up @@ -83,27 +83,25 @@ npm install source-map-support --save-dev
```
然后执行如下代码:
``` javascript
var csharp = require("csharp");
var puerts = require("puerts");
puerts.registerBuildinModule("path", {
puer.registerBuildinModule("path", {
dirname(path) {
return csharp.System.IO.Path.GetDirectoryName(path);
return CS.System.IO.Path.GetDirectoryName(path);
},
resolve(dir, url) {
url = url.replace(/\\/g, "/");
while (url.startsWith("../")) {
dir = csharp.System.IO.Path.GetDirectoryName(dir);
dir = CS.System.IO.Path.GetDirectoryName(dir);
url = url.substr(3);
}
return csharp.System.IO.Path.Combine(dir, url);
return CS.System.IO.Path.Combine(dir, url);
},
});
puerts.registerBuildinModule("fs", {
puer.registerBuildinModule("fs", {
existsSync(path) {
return csharp.System.IO.File.Exists(path);
return CS.System.IO.File.Exists(path);
},
readFileSync(path) {
return csharp.System.IO.File.ReadAllText(path);
return CS.System.IO.File.ReadAllText(path);
},
});
(function () {
Expand Down
30 changes: 15 additions & 15 deletions doc/unity/en/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
js/ts调用时,可以找到该类;

- 会生成一个静态类(wrap),在js调用时,直接静态调用,加快调用速度,否则是通过反射调用。
-index.d.ts中生成函数的声明,在ts调用时,import时,可以找到
-index.d.ts中生成函数的声明,在ts调用时,可以由代码提示找到

- **用法**

Expand Down Expand Up @@ -225,7 +225,7 @@
如果后续不需要-=,那么可以直接传函数当delegate

```typescript
import { PuertsTest, System } from 'csharp'
const { PuertsTest, System } = CS
let obj = new PuertsTest.DerivedClass();
obj.MyCallback = msg => console.log("do not need remove, msg=" + msg);
```
Expand Down Expand Up @@ -275,8 +275,8 @@
```

```typescript
import {PuertsTest} from 'csharp'
import {$ref, $unref} from 'puerts'
const {PuertsTest} = CS
const {$ref, $unref} = puer
let obj = new PuertsTest.DerivedClass();
let p1 = $ref();
let p2 = $ref(10);
Expand Down Expand Up @@ -308,8 +308,8 @@
```

```typescript
import {PuertsTest,System} from 'csharp'
import {$generic} from 'puerts'
const {PuertsTest,System} = CS
const {$generic} = puer
//$generic调用性能不会太好,同样泛型参数建议整个工程,至少一个文件内只做一次
let List = $generic(System.Collections.Generic.List$1, System.Int32);
let Dictionary = $generic(System.Collections.Generic.Dictionary$2, System.String, List);
Expand Down Expand Up @@ -349,8 +349,8 @@
```

```typescript
import {PuertsTest,System} from 'csharp'
import {$typeof} from 'puerts'
const {PuertsTest,System} = CS
const {$typeof} = puer
let arr = System.Array.CreateInstance($typeof(System.Int32),3) as System.Array$1<number>;
arr.set_Item(0, 11);
arr.set_Item(1, 22);
Expand Down Expand Up @@ -381,8 +381,8 @@
```

```typescript
import {PuertsTest} from 'csharp'
import {$extension} from 'puerts'
const {PuertsTest} = CS
const {$extension} = puer
$extension(PuertsTest.BaseClass, PuertsTest.BaseClassExtension);
let obj = new PuertsTest.BaseClass();
obj.Extension1();
Expand Down Expand Up @@ -414,8 +414,8 @@
```

```typescript
import {PuertsTest} from 'csharp'
import {$promise} from 'puerts'
const {PuertsTest} = CS
const {$promise} = puer
async function asyncCall() {
let obj = new PuertsTest.DerivedClass();
let task = obj.GetFileLength("Assets/Examples/05_Typescript/TsQuickStart.cs");
Expand Down Expand Up @@ -448,8 +448,8 @@
```

```typescript
import {PuertsTest} from 'csharp'
import {$set} from 'puerts'
const {PuertsTest} = CS
const {$set} = puer
let a = 10;
let b = $ref(10);
//b = 20; 如果这样赋值的话,最后运行结果 b = undefined
Expand Down Expand Up @@ -480,7 +480,7 @@
```

```typescript
import {PuertsTest} from 'csharp'
const {PuertsTest} = CS
let obj1 = new PuertsTest.BaseClass();
let obj2 = new PuertsTest.BaseClass();
obj1.baseIntField = 11;
Expand Down
6 changes: 1 addition & 5 deletions doc/unity/en/tutorial/cs2js.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class TestClass
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp');
const obj = new CS.TestClass();
obj.AddEventCallback1(i => console.log(i));
obj.Trigger();
Expand Down Expand Up @@ -95,9 +93,7 @@ public class JsBehaviour : MonoBehaviour
{
if (jsEnv == null) jsEnv = new JsEnv(new DefaultLoader(), 9229);

var init = jsEnv.Eval<ModuleInit>(@"const m =
const CS = require('csharp');
var init = jsEnv.Eval<ModuleInit>(@"
class Rotate {
constructor(bindTo) {
this.bindTo = bindTo;
Expand Down
19 changes: 6 additions & 13 deletions doc/unity/en/tutorial/js2cs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ void Start() {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp')
console.log(new CS.UnityEngine.Vector3(1, 2, 3));
// (1.0, 2.0, 3.0)
")
Expand All @@ -46,7 +45,6 @@ void Start() {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp')
CS.UnityEngine.Debug.Log('Hello World');
const rect = new CS.UnityEngine.Rect(0, 0, 2, 2);
CS.UnityEngine.Debug.Log(rect.Contains(CS.UnityEngine.Vector2.one)); // True
Expand Down Expand Up @@ -75,16 +73,15 @@ class Example4 {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp')
// 通过puerts.$ref创建一个可以用于使用out/ref参数的变量
let p1 = puerts.$ref();
let p2 = puerts.$ref(10);
let p1 = puer.$ref();
let p2 = puer.$ref(10);
let ret = Example4.InOutArgFunc(100, p1, p2);
console.log('ret=' + ret + ', out=' + puerts_1.$unref(p1) + ', ref=' + puerts_1.$unref(p2));
// ret=200, out=100, ref=20
// 通过puerts.$generic来创建一个List<int>类型
let List = puerts.$generic(CS.System.Collections.Generic.List$1, CS.System.Int32);
let List = pues.$generic(CS.System.Collections.Generic.List$1, CS.System.Int32);
let lst = new List();
lst.Add(1);
lst.Add(0);
Expand All @@ -106,10 +103,8 @@ void Start() {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp');
let go = new CS.UnityEngine.GameObject('testObject');
go.AddComponent(puerts.$typeof(CS.UnityEngine.ParticleSystem));
go.AddComponent(puer.$typeof(CS.UnityEngine.ParticleSystem));
const Vector3 = CS.UnityEngine.Vector3;
Vector3.op_Multiply(Vector3.up, 1600)
Expand Down Expand Up @@ -143,11 +138,9 @@ class Example6 {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp');
(async function() {
let task = obj.GetFileLength('xxxx');
let result = await puerts.$promise(task);
let result = await puer.$promise(task);
console.log('file length is ' + result);
})()
.catch(err=> {
Expand All @@ -156,7 +149,7 @@ void Start() {
")
}
```
对于 C# 的`async`函数,JS 侧通过`puerts.$promise`包装一下 C# 返回的 task,即可 await 调用了
对于 C# 的`async`函数,JS 侧通过`puer.$promise`包装一下 C# 返回的 task,即可 await 调用了

-------------
这一部分是有关 JS 调用 C# 的。下一部分我们反过来,介绍 C# 调用 JS
2 changes: 1 addition & 1 deletion doc/unity/en/wrapper/extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ puerts会在生成代码时帮你遍历整个assembly,将该类存在的所有
2. 在JS侧调用手动扩展函数
如下图所示,参数0是扩展目标类,参数1是包含扩展函数的扩展类。你可以在JsEnv初始化的位置去调用这个函数
```
puerts.$extension(CS.PuertsTest.BaseClass, CS.PuertsTest.BaseClassExtension);
puer.$extension(CS.PuertsTest.BaseClass, CS.PuertsTest.BaseClassExtension);
```

3. 打开PUERTS_REFLECT_ALL_EXTENSION宏
Expand Down
16 changes: 7 additions & 9 deletions doc/unity/zhcn/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,25 @@ npm install source-map-support --save-dev
```
然后执行如下代码:
``` javascript
var csharp = require("csharp");
var puerts = require("puerts");
puerts.registerBuildinModule("path", {
puer.registerBuildinModule("path", {
dirname(path) {
return csharp.System.IO.Path.GetDirectoryName(path);
return CS.System.IO.Path.GetDirectoryName(path);
},
resolve(dir, url) {
url = url.replace(/\\/g, "/");
while (url.startsWith("../")) {
dir = csharp.System.IO.Path.GetDirectoryName(dir);
dir = CS.System.IO.Path.GetDirectoryName(dir);
url = url.substr(3);
}
return csharp.System.IO.Path.Combine(dir, url);
return CS.System.IO.Path.Combine(dir, url);
},
});
puerts.registerBuildinModule("fs", {
puer.registerBuildinModule("fs", {
existsSync(path) {
return csharp.System.IO.File.Exists(path);
return CS.System.IO.File.Exists(path);
},
readFileSync(path) {
return csharp.System.IO.File.ReadAllText(path);
return CS.System.IO.File.ReadAllText(path);
},
});
(function () {
Expand Down
4 changes: 0 additions & 4 deletions doc/unity/zhcn/tutorial/cs2js.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class TestClass
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp');
const obj = new CS.TestClass();
obj.AddEventCallback1(i => console.log(i));
obj.Trigger();
Expand Down Expand Up @@ -96,8 +94,6 @@ public class JsBehaviour : MonoBehaviour
if (jsEnv == null) jsEnv = new JsEnv(new DefaultLoader(), 9229);

var init = jsEnv.Eval<ModuleInit>(@"const m =
const CS = require('csharp');
class Rotate {
constructor(bindTo) {
this.bindTo = bindTo;
Expand Down
19 changes: 6 additions & 13 deletions doc/unity/zhcn/tutorial/js2cs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ void Start() {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp')
console.log(new CS.UnityEngine.Vector3(1, 2, 3));
// (1.0, 2.0, 3.0)
")
Expand All @@ -46,7 +45,6 @@ void Start() {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp')
CS.UnityEngine.Debug.Log('Hello World');
const rect = new CS.UnityEngine.Rect(0, 0, 2, 2);
CS.UnityEngine.Debug.Log(rect.Contains(CS.UnityEngine.Vector2.one)); // True
Expand Down Expand Up @@ -75,16 +73,15 @@ class Example4 {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp')
// 通过puerts.$ref创建一个可以用于使用out/ref参数的变量
let p1 = puerts.$ref();
let p2 = puerts.$ref(10);
let p1 = puer.$ref();
let p2 = puer.$ref(10);
let ret = Example4.InOutArgFunc(100, p1, p2);
console.log('ret=' + ret + ', out=' + puerts_1.$unref(p1) + ', ref=' + puerts_1.$unref(p2));
// ret=200, out=100, ref=20
// 通过puerts.$generic来创建一个List<int>类型
let List = puerts.$generic(CS.System.Collections.Generic.List$1, CS.System.Int32);
let List = puer.$generic(CS.System.Collections.Generic.List$1, CS.System.Int32);
let lst = new List();
lst.Add(1);
lst.Add(0);
Expand All @@ -106,10 +103,8 @@ void Start() {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp');
let go = new CS.UnityEngine.GameObject('testObject');
go.AddComponent(puerts.$typeof(CS.UnityEngine.ParticleSystem));
go.AddComponent(puer.$typeof(CS.UnityEngine.ParticleSystem));
const Vector3 = CS.UnityEngine.Vector3;
Vector3.op_Multiply(Vector3.up, 1600)
Expand Down Expand Up @@ -143,11 +138,9 @@ class Example6 {
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval(@"
const CS = require('csharp');
(async function() {
let task = obj.GetFileLength('xxxx');
let result = await puerts.$promise(task);
let result = await puer.$promise(task);
console.log('file length is ' + result);
})()
.catch(err=> {
Expand All @@ -156,7 +149,7 @@ void Start() {
")
}
```
对于 C# 的`async`函数,JS 侧通过`puerts.$promise`包装一下 C# 返回的 task,即可 await 调用了
对于 C# 的`async`函数,JS 侧通过`puer.$promise`包装一下 C# 返回的 task,即可 await 调用了

-------------
这一部分是有关 JS 调用 C# 的。下一部分我们反过来,介绍 C# 调用 JS
2 changes: 1 addition & 1 deletion doc/unity/zhcn/wrapper/extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ puerts会在生成代码时帮你遍历整个assembly,将该类存在的所有
2. 在JS侧调用手动扩展函数
如下图所示,参数0是扩展目标类,参数1是包含扩展函数的扩展类。你可以在JsEnv初始化的位置去调用这个函数
```
puerts.$extension(CS.PuertsTest.BaseClass, CS.PuertsTest.BaseClassExtension);
puer.$extension(CS.PuertsTest.BaseClass, CS.PuertsTest.BaseClassExtension);
```

3. 打开PUERTS_REFLECT_ALL_EXTENSION宏
Expand Down

0 comments on commit 2cb7be6

Please sign in to comment.