JSBinding / Code Snippets

时间:2024-12-17 22:04:32

new a gameobject & overloaded methds

var go1 = new UnityEngine.GameObject.ctor();
var go2 = new UnityEngine.GameObject.ctor$$String("Hello");

add/get c# component

var trans = go.GddComponent$1(UnityEngine.Transform.ctor);
var image = go.AddComponent$1(UnityEngine.UI.Image.ctor);
image.set_color(new UnityEngine.Color.ctor$$Single$$Single$$Single(125/255, 78/255, 3/255));

add/get javascript component

// a javascript monobehaviour
jss.define_mb("Dog", function () {
this.Eat = function () {
print("EAT!");
}
}); // some function
function fun(goDog) {
goDog.AddComponent$1(jss.Dog);
var dog = goDog.GetComponent$1(jss.Dog);
dog.Eat();
}

define a javascript monobehaviour

jss.define_mb("UIController", function () {

    // UnityEngine.Object
// add this variable to Unity Inspector and drag something to it
this.oTileRoot = null; this.oScore = null;
this.oBestScore = null; // auto called from c#
this.Start = function () {
} // auto called from c#
this.Update = function () {
}
});

JSBinding / Code Snippets

static methods & enum

if (UnityEngine.Input.GetKeyDown$$KeyCode(UnityEngine.KeyCode.UpArrow)) {
inputMgr.emit("move", 0);
break;
}

properties (get_, set_)

var e = this.animator.get_enabled();
this.animator.set_enabled(!e);
var pos = this.trans.get_position();
other.trans.set_position(pos);

ref/out

var $cv = { Value: UnityEngine.Vector3.get_zero() };
var newPos = UnityEngine.Vector3.SmoothDamp$$Vector3$$Vector3$$Vector3$$Single(
this.trans.get_position(),
this.toPos,
$cv,
0.07); print("new pos: " + $cv.Value);

delegates & properties & overloaded functions

btn.get_onClick().RemoveAllListeners();
btn.get_onClick().AddListener$$UnityAction(function () {
print("button clicked!");
});

Coroutine

jss.define_mb("CorutineTest", function() {

    // called from c#
this.Start = function () {
this.StartCoroutine$$IEnumerator(this.SayHello());
} // notice syntax here!
this.SayHello = function*() {
var i = 10; while (i > 0) {
print("hello jsb " + i);
i--;
yield new UE.WaitForSeconds.ctor(1);
}
} this.Update = function () {
var elapsed = UE.Time.get_deltaTime(); // update coroutines and invokes manually!
this.$UpdateAllCoroutines(elapsed);
this.$UpdateAllInvokes(elapsed);
}
}

back to JSBinding / Home