-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
525aef5
commit d67ff6c
Showing
70 changed files
with
6,327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.meta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using UnityEngine; | ||
using Puerts; | ||
|
||
namespace PuertsTest | ||
{ | ||
public class JsCallCs : MonoBehaviour | ||
{ | ||
JsEnv jsEnv; | ||
|
||
void Start() | ||
{ | ||
jsEnv = new JsEnv(); | ||
|
||
jsEnv.Eval(@" | ||
const CS = require('csharp'); | ||
let gameObject = new CS.UnityEngine.GameObject('testObject'); | ||
CS.UnityEngine.Debug.Log(gameObject.name); | ||
"); | ||
} | ||
|
||
void OnDestroy() | ||
{ | ||
jsEnv.Dispose(); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using UnityEngine; | ||
using Puerts; | ||
|
||
namespace PuertsTest | ||
{ | ||
public class Require : MonoBehaviour | ||
{ | ||
JsEnv jsEnv; | ||
|
||
// Use this for initialization | ||
void Start() | ||
{ | ||
//JsEnv还有一个构造函数可以传loader, | ||
//通过实现不同的loader,可以做到从诸如 | ||
//AssetBundle,压缩包,网络等源加载代码 | ||
//这个无参构造会用默认的loader,默认loader | ||
//从Resources目录加载 | ||
jsEnv = new JsEnv(); | ||
|
||
jsEnv.Eval(@" | ||
require('main') | ||
"); | ||
} | ||
|
||
void OnDestroy() | ||
{ | ||
jsEnv.Dispose(); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const module1 = require('module1'); | ||
|
||
module1.callMe('from john'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
console.log('module1 loading'); | ||
|
||
function callMe(msg) { | ||
console.log('callMe called', msg); | ||
} | ||
|
||
exports.callMe = callMe; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using UnityEngine; | ||
using Puerts; | ||
|
||
namespace PuertsTest | ||
{ | ||
public class Callback : MonoBehaviour | ||
{ | ||
JsEnv jsEnv; | ||
|
||
// Use this for initialization | ||
void Start() | ||
{ | ||
jsEnv = new JsEnv(); | ||
|
||
//要使用值类型参数或者返回值的委托要声明,而Callback1因为是引用类型所以不用。 | ||
jsEnv.UsingAction<int>(); | ||
|
||
jsEnv.Eval(@" | ||
const CS = require('csharp'); | ||
let obj = new CS.PuertsTest.TestClass(); | ||
//如果你后续要remove,需要这样构建一个Delegate,后续可以用该Delegate引用去remove | ||
let delegate = new CS.PuertsTest.Callback1(o => o.Foo()); | ||
obj.AddEventCallback1(delegate); | ||
obj.AddEventCallback2(i => console.log(i)); //如果不需要remove,直接传函数即可 | ||
obj.Trigger(); | ||
obj.RemoveEventCallback1(delegate); | ||
obj.Trigger(); | ||
"); | ||
} | ||
|
||
void OnDestroy() | ||
{ | ||
jsEnv.Dispose(); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using UnityEngine; | ||
using Puerts; | ||
|
||
namespace PuertsTest | ||
{ | ||
public delegate void Callback1(TestClass obj); | ||
|
||
public delegate void Callback2(int str); | ||
|
||
public class TestClass | ||
{ | ||
Callback1 callback1; | ||
|
||
Callback2 callback2; | ||
|
||
public void AddEventCallback1(Callback1 callback1) | ||
{ | ||
this.callback1 += callback1; | ||
} | ||
|
||
public void RemoveEventCallback1(Callback1 callback1) | ||
{ | ||
this.callback1 -= callback1; | ||
} | ||
|
||
public void AddEventCallback2(Callback2 callback2) | ||
{ | ||
this.callback2 += callback2; | ||
} | ||
|
||
public void Trigger() | ||
{ | ||
Debug.Log("begin Trigger"); | ||
if (callback1 != null) | ||
{ | ||
callback1(this); | ||
} | ||
if (callback2 != null) | ||
{ | ||
callback2(1024); | ||
} | ||
Debug.Log("end Trigger"); | ||
} | ||
|
||
public void Foo() | ||
{ | ||
Debug.Log("Foo"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using UnityEngine; | ||
using Puerts; | ||
using System; | ||
|
||
namespace PuertsTest | ||
{ | ||
public delegate void ModuleInit(JsBehaviour monoBehaviour); | ||
|
||
//只是演示纯用js实现MonoBehaviour逻辑的可能, | ||
//但从性能角度这并不是最佳实践,会导致过多的跨语言调用 | ||
public class JsBehaviour : MonoBehaviour | ||
{ | ||
public string ModuleName;//可配置加载的js模块 | ||
|
||
public Action JsStart; | ||
public Action JsUpdate; | ||
public Action JsOnDestroy; | ||
|
||
static JsEnv jsEnv; | ||
|
||
void Awake() | ||
{ | ||
if (jsEnv == null) jsEnv = new JsEnv(); | ||
|
||
var init = jsEnv.Eval<ModuleInit>("const m = require('" + ModuleName + "'); m.init;"); | ||
|
||
if (init != null) init(this); | ||
} | ||
|
||
void Start() | ||
{ | ||
if (JsStart != null) JsStart(); | ||
} | ||
|
||
void Update() | ||
{ | ||
if (JsUpdate != null) JsUpdate(); | ||
} | ||
|
||
void OnDestroy() | ||
{ | ||
if (JsOnDestroy != null) JsOnDestroy(); | ||
JsStart = null; | ||
JsUpdate = null; | ||
JsOnDestroy = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const speed = 10; | ||
const CS = require('csharp'); | ||
|
||
class Rotate { | ||
constructor(bindTo) { | ||
this.bindTo = bindTo; | ||
this.bindTo.JsUpdate = () => this.onUpdate(); | ||
this.bindTo.JsOnDestroy = () => this.onDestroy(); | ||
} | ||
|
||
onUpdate() { | ||
//js��֧�ֲ�������������Vector3�ij���ô�� | ||
let r = CS.UnityEngine.Vector3.op_Multiply(CS.UnityEngine.Vector3.up, CS.UnityEngine.Time.deltaTime * speed); | ||
this.bindTo.transform.Rotate(r); | ||
} | ||
|
||
onDestroy() { | ||
console.log('onDestroy...'); | ||
} | ||
} | ||
|
||
exports.init = function(bindTo) { | ||
new Rotate(bindTo); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":3,"file":"QuickStart.js","sourceRoot":"","sources":["../QuickStart.ts"],"names":[],"mappings":";AAAA,kBAAkB;;AAElB,mCAAsD;AACtD,mC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CAAC,CAAC,CAAC;AACX,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACX,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACX,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;AAEnB,oEAAoE;AACpE;;;;;;;;sDAQsD"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"use strict"; | ||
//部署:npm run build | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const csharp_1 = require("csharp"); | ||
const puerts_1 = require("puerts"); | ||
//静态函数 | ||
csharp_1.UnityEngine.Debug.Log('hello world'); | ||
//对象构造 | ||
let obj = new csharp_1.PuertsTest.DerivedClass(); | ||
//实例成员访问 | ||
obj.BMFunc(); //父类方法 | ||
obj.DMFunc(csharp_1.PuertsTest.MyEnum.E1); //子类方法 | ||
console.log(obj.BMF, obj.DMF); | ||
obj.BMF = 10; //父类属性 | ||
obj.DMF = 30; //子类属性 | ||
console.log(obj.BMF, obj.DMF); | ||
//静态成员 | ||
console.log(csharp_1.PuertsTest.BaseClass.BSF, csharp_1.PuertsTest.DerivedClass.DSF, csharp_1.PuertsTest.DerivedClass.BSF); | ||
//委托,事件 | ||
//如果你后续不需要-=,可以像这样直接传函数当delegate | ||
obj.MyCallback = msg => console.log("do not need remove, msg=" + msg); | ||
//通过new构建的delegate,后续可以拿这个引用去-= | ||
let delegate = new csharp_1.PuertsTest.MyCallback(msg => console.log('can be removed, msg=' + msg)); | ||
//由于ts不支持操作符重载,Delegate.Combine相当于C#里头的obj.myCallback += delegate; | ||
obj.MyCallback = csharp_1.System.Delegate.Combine(obj.MyCallback, delegate); | ||
obj.Trigger(); | ||
//Delegate.Remove相当于C#里头的obj.myCallback += delegate; | ||
obj.MyCallback = csharp_1.System.Delegate.Remove(obj.MyCallback, delegate); | ||
obj.Trigger(); | ||
//事件 | ||
obj.add_MyEvent(delegate); | ||
obj.Trigger(); | ||
obj.remove_MyEvent(delegate); | ||
obj.Trigger(); | ||
//静态事件 | ||
csharp_1.PuertsTest.DerivedClass.add_MyStaticEvent(delegate); | ||
obj.Trigger(); | ||
csharp_1.PuertsTest.DerivedClass.remove_MyStaticEvent(delegate); | ||
obj.Trigger(); | ||
//可变参数 | ||
obj.ParamsFunc(1024, 'haha', 'hehe', 'heihei'); | ||
//in out 参数 | ||
let p1 = puerts_1.$ref(); | ||
let p2 = puerts_1.$ref(10); | ||
let ret = obj.InOutArgFunc(100, p1, p2); | ||
console.log('ret=' + ret + ', out=' + puerts_1.$unref(p1) + ', ref=' + puerts_1.$unref(p2)); | ||
//泛型 | ||
//先通过$generic实例化泛型参数 | ||
let List = puerts_1.$generic(csharp_1.System.Collections.Generic.List$1, csharp_1.System.Int32); | ||
let lst = new List(); | ||
lst.Add(1); | ||
lst.Add(0); | ||
lst.Add(2); | ||
lst.Add(4); | ||
obj.PrintList(lst); | ||
//typescript和c#的async,await联动,为了不在低版本的Unity下报错,先注释,c#7.3以上版本可以打开这些注释 | ||
/*async function asyncCall() { | ||
let task = obj.GetFileLength("Assets/Examples/05_Typescript/TsQuickStart.cs"); | ||
let result = await $promise(task); | ||
console.log('file length is ' + result); | ||
let task2 = obj.GetFileLength("notexistedfile");//这个会抛文件找不到异常,被catch | ||
let result2 = await $promise(task2); | ||
console.log('file length is ,' + result2); | ||
} | ||
asyncCall().catch(e => console.error("catch:" + e));*/ | ||
//# sourceMappingURL=QuickStart.js.map |
Oops, something went wrong.