Puerts is Puert TS, Tencent's open source ts hot update solution. Project github: https://github.com/Tencent/puerts
Preparation: Unity 2021.3.25f1
nodejs v16.13.1
1. Download puerts Address: https://github .com/Tencent/puerts/releases version can be selected at
will. After decompression, put the puerts folder under assets/. As shown in the figure:
2. Generate code.
3. Create a ts folder under assets to store the ts source code. folder The name is random, I call it TsProject here. Then execute the command:
npm init -y
npm i typescript -D
Create a tsconfig.json file. This is mainly the configuration of ts.
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"sourceMap": true,
"noImplicitAny": true,
"typeRoots": [
"../Puerts/Typing",
"../Gen/Typing",
"./node_modules/@types"
],
"outDir": "output"
}
}
package.json, here is mainly to modify scripts instructions.build and postbuild.
{
"name": "tsproject",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc -p tsconfig.json",
"postbuild": "node copyJsFile.js output ../Resources"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^5.1.6"
}
}
copyJsFile.js file download address: https://github.com/chexiongsheng/puerts_unity_demo/blob/master/projects/0_Basic_Demo/TsProj/copyJsFile.js
Here comes the main.ts
import {
System, UnityEngine } from 'csharp'
UnityEngine.Debug.Log('Hello World');
let obj: UnityEngine.GameObject = new UnityEngine.GameObject("testObject");
obj.transform.position = new UnityEngine.Vector3(10, 2, 23);
After completion: the tsproject folder looks like this:
Now start compiling ts.
Use the build command under the scripts of package.json. Then use the postbuild command.
After completion, you can see the generated code in the Resources folder. If Resources are not created, you need to create them manually.
4. The logic of communicating js in c#.
The Require.cs file is mounted on the lamp or other objects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Puerts;
public class Require : MonoBehaviour
{
JsEnv jsEnv;
// Start is called before the first frame update
void Start()
{
jsEnv = new JsEnv();
jsEnv.Eval(@"require('main')");//这里会从Resources中加载对应的main.js.txt文件
}
// Update is called once per frame
void Update()
{
}
private void OnDestroy()
{
jsEnv.Dispose();
}
}
Then start to run the editor: the log is typed out, and an empty object is generated.
In fact, there is a simpler test js script, which does not require the troublesome creation of the ts just now. JsCallCs.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Puerts;
public class JsCallCs : MonoBehaviour
{
JsEnv jsEnv;
// Start is called before the first frame update
void Start()
{
jsEnv = new JsEnv();//下面是相当于拿到js的代码
jsEnv.Eval(@"
const CS=require('csharp');
let gameObject=new CS.UnityEngine.GameObject('testObject');
CS.UnityEngine.Debug.Log(gameObject.name);
");
}
// Update is called once per frame
void Update()
{
}
private void OnDestroy()
{
jsEnv.Dispose();
}
}
Another: There is another pit here, that is, in copyJsFile.js, when compiling ts, it is main.js under Resources, but what c# needs is the main.js.txt file, which is a string. An error will be reported, Error code 58, mian cannot be found. Solution:
release the comment here, and then recompile ts.