diff --git a/.obsidian/plugins/various-complements/data.json b/.obsidian/plugins/various-complements/data.json index b18fccf..bdac71e 100644 --- a/.obsidian/plugins/various-complements/data.json +++ b/.obsidian/plugins/various-complements/data.json @@ -126,6 +126,30 @@ "lastUpdated": 1690635291419 } } + }, + "UsingReactUMG": { + "UsingReactUMG": { + "currentFile": { + "count": 1, + "lastUpdated": 1690971735176 + } + } + }, + "编译插件后进入工程,点击工具栏的": { + "编译插件后进入工程,点击工具栏的": { + "currentFile": { + "count": 1, + "lastUpdated": 1690972218475 + } + } + }, + "json,": { + "json,": { + "currentFile": { + "count": 1, + "lastUpdated": 1690972883636 + } + } } } } \ No newline at end of file diff --git a/03-UnrealEngine/Gameplay/PuerTS/Puerts(一)——学习资料归纳.md b/03-UnrealEngine/Gameplay/PuerTS/Puerts(一)——学习资料归纳.md index 1f167e2..5b43200 100644 --- a/03-UnrealEngine/Gameplay/PuerTS/Puerts(一)——学习资料归纳.md +++ b/03-UnrealEngine/Gameplay/PuerTS/Puerts(一)——学习资料归纳.md @@ -42,13 +42,43 @@ tsc xxx.ts ``` # Puerts -## QuickStart -1. 编译插件后进入工程,点击工具栏的`ue.d.ts`生成接口代码。 -2. 代码写在项目目录下的`TypeScript`中。(插件运行后会自动创建) -3. 之后就会在编辑器`Blueprints/TypeScript`目录下出现资产图标。 - +## Setup +1. 编译插件。 +2. 进入插件目录`Plugins\Puerts`执行`node enable_puerts_module.js`。 +3. 在项目根目录下执行`npm init`,并且添加。之后重新生成一次VS解决方案并且执行`npm install`。 +```json +"dependencies": { + "@types/react": "^15.6.6", + "@types/react-reconciler": "^0.18.0", + "@types/mocha": "^7.0.1" + } +``` +5. 代码写在项目目录下的`Content/TypeScript`中。 +6. 之后就会在编辑器`Blueprints/TypeScript`目录下出现资产图标。 +7. 加入ReactUMG。 + 1. 进入 Content/javascript 目录 npm init 创建 package.json。 + 2. 创建成功后,向文件中粘贴如下内容 + ```json +"dependencies": { + "react": "^16.11.0", + "react-reconciler": "^0.23.0" + } +``` + 3. 然后 npm install 一次。 + 4. 打开工程根目录的 tsconfig.json, 在 typeRoots 中 加入 "Plugins/ReactUMG/Typing"。 +8. 打开 工程,在引擎中点击 ue.d.ts 。 ### 调试方法 -具体可以参考:# Puerts Inspector指南(一)在UE4和Unity里调试Javascripthttps://zhuanlan.zhihu.com/p/359598262 +具体可以参考: +- Puerts Inspector指南(一)在UE4和Unity里调试Javascript:https://zhuanlan.zhihu.com/p/359598262 + +调试器的选择有: +1. 在Chrome输入`devtools://devtools/bundled/inspector.html?v8only=true&ws=127.0.0.1:8080`。 +2. 在Chrome输入`chrome://inspect`,点击Configure...,输入IP&Port后,点击Inspect。 +3. 使用VSCode进行调试。 + 1. 在`Launch Program`处点击`add Configuration`。 + 2. 选择`Node.js: Attach`。 + 3. 设置端口。 + 4. 点击绿色箭头即可调试。 #### 方式1:自创建虚拟机模式下调试配置 创建FJsEnv传入调试端口 @@ -64,7 +94,7 @@ GameScript->Start("QuickStart", Arguments); ``` #### 方式2:自动绑定模式下调试配置 -菜单上选择“编辑->项目设置”,打开设置页面后在“插件->Puerts Setting”页面中开启调试以及设置端口 +1. 菜单上选择`Edit->ProjectSettings`,打开设置页面后在`Plugins -> Puerts Setting`页面中开启调试以及设置端口。 # TypeScript - Express TypeScript化改造:https://www.jianshu.com/p/978628a15027 @@ -93,10 +123,131 @@ $ ts-node-dev ./bin/www ``` # Puerts +Puerts的使用方法主要为 +- 继承原生UE C++类 +- 在C++类中调用TypeScript文件 +- Mixin +## puerts_unreal_demo 食用方法 +项目默认使用UTsGameInstance,可以在cpp中修改OnStart()中调用的TS脚本文件名,来查看结果。优先查看TS文件: +- QuickStart +- UsingMixin +- UsingMakeUClass +- UI + - UsingWidget + - UsingReactUMG +### 蓝图相关 +```javascript +//蓝图加载 +//UE.Class.Load方式 +//let bpClass = UE.Class.Load('/Game/StarterContent/TestBlueprint.TestBlueprint_C') +//let bpActor = UE.GameplayStatics.BeginDeferredActorSpawnFromClass(gameInstance, bpClass, undefined) as UE.Game.StarterContent.TestBlueprint.TestBlueprint_C; +blueprint.load(UE.Game.StarterContent.TestBlueprint.TestBlueprint_C); +const TestBlueprint_C = UE.Game.StarterContent.TestBlueprint.TestBlueprint_C; //别名 +let bpActor = UE.GameplayStatics.BeginDeferredActorSpawnFromClass(gameInstance, TestBlueprint_C.StaticClass(), undefined) as UE.Game.StarterContent.TestBlueprint.TestBlueprint_C; +UE.GameplayStatics.FinishSpawningActor(bpActor, undefined); +bpActor.Foo(false, 8000, 9000); +//如果确定后续不需要使用TestBlueprint_C了,应该unload节省内存 +blueprint.unload(TestBlueprint_C); + +//蓝图结构体加载 +//UE.UserDefinedStruct.Load方式 +//let TestStruct = UE.UserDefinedStruct.Load("UserDefinedStruct'/Game/StarterContent/TestStruct.TestStruct'"); +//let testStruct = UE.NewStruct(TestStruct) as UE.Game.StarterContent.TestStruct.TestStruct; +blueprint.load(UE.Game.StarterContent.TestStruct.TestStruct); +const TestStruct = UE.Game.StarterContent.TestStruct.TestStruct; +let testStruct = new TestStruct(); +testStruct.age = 10; +testStruct.speed = 5; +bpActor.Bar(testStruct); +blueprint.unload(TestStruct); + +//蓝图枚举 +console.log("-------------------------15---------------------------"); +console.log(UE.Game.StarterContent.TestEnum.TestEnum.Blue); +console.log(UE.Game.StarterContent.TestEnum.TestEnum.Red); +console.log(UE.Game.StarterContent.TestEnum.TestEnum.Green); +``` + +### Delegate +```javascript +//Delegate +function MutiCast1(i) { +    console.warn("MutiCast1<<<", i); +} + +function MutiCast2(i) { +    console.warn("MutiCast2>>>", i); +    actor.NotifyWithInt.Remove(MutiCast2);//调用一次后就停掉 +} + +actor.NotifyWithInt.Add(MutiCast1) +actor.NotifyWithInt.Add(MutiCast2) + +console.log("NotifyWithString.IsBound", actor.NotifyWithString.IsBound()); +console.log("NotifyWithRefString.IsBound", actor.NotifyWithRefString.IsBound()); +actor.NotifyWithRefString.Bind((strRef) => { + //console.error("NotifyWithRefString"); + console.log("NotifyWithRefString", $unref(strRef)); + $set(strRef, "out to NotifyWithRefString");//引用参数输出 +}); +console.log("NotifyWithString.IsBound", actor.NotifyWithString.IsBound()); +console.log("NotifyWithRefString.IsBound", actor.NotifyWithRefString.IsBound()); + +actor.NotifyWithStringRet.Bind((inStr) => { + return "////" + inStr; +}); + +actor.NotifyWithInt.Broadcast(888999); +let strRef = $ref("666"); +actor.NotifyWithRefString.Execute(strRef); +console.log("out str:" + $unref(strRef)); +let retStr = actor.NotifyWithStringRet.Execute("console.log('hello world')"); +console.log("ret str:" + retStr); +console.log("waiting native call script..........."); + +//Pass JsFunction as Delegate +function IsJohn(str:string) : boolean { + return str == "John"; +} +obj.PassJsFunctionAsDelegate(toManualReleaseDelegate(IsJohn)); +//release after using +releaseManualReleaseDelegate(IsJohn); + +//unhandledRejection +on('unhandledRejection', function(reason: any) { + console.log('unhandledRejection~~~'); +}); + +new Promise(()=>{ + throw new Error('unhandled rejection'); +}); +``` ## 热更新方法 https://zhuanlan.zhihu.com/p/364505146 -## UE访问Puerts +## UE C++调用Puerts脚本方法 +```c++ +//h +TSharedPtr GameScript; + +//cpp +GameScript = MakeShared(); +TArray> Arguments; +Arguments.Add(TPair(TEXT("GameInstance"), this)); +GameScript->Start("QuickStart", Arguments); +``` + +```javascript +//在FJsEnv启动,调用Start时传入的参数可以通过argv获取。如果是继承ue类方式,这里的argv是空的 +let gameInstance = (argv.getByName("GameInstance") as UE.GameInstance); +let actor =  UE.GameplayStatics.BeginDeferredActorSpawnFromClass(gameInstance, UE.MainActor.StaticClass(), undefined) as UE.MainActor; +UE.GameplayStatics.FinishSpawningActor(actor, undefined); + +console.log(actor.GetName()); +console.log(actor.K2_GetActorLocation().ToString()); +``` + +## UE C++访问Puerts 通过UDynamicDelegateProxy,其成员记录了绑定的虚拟机与JS函数。 \ No newline at end of file