JSBinding + SharpKit / Convert 2DPlatformer to JavaScript version

2DPlatformer is a Unity3D official demo. Asset store URL: https://www.assetstore.unity3d.com/en/#!/content/11228

What we are going to do now is convert 2DPlatformer to JavaScript version.

All C# scripts are all in 2DPlatformer/Scripts/ folder. We are going to use SharpKit to convert all C# scripts to JavaScript, and finally delete that folder.

NOTE: the JSBinding package already contains JavaScript version 2DPlatformer game.

1. Download 2DPlatformer package from asset store and import it into project.

2. Coroutines are not supported, so we have to rewrite all yields in C# scripts

TODO: ....

3. Click JSB | Generate JS and CS Bindings. (ignore this step if it was clicked before)

4. Correctly set up _JSEngine.prefab variables in Inspector.

5. Add [JsType()] attributes to all C# scripts in folder 2DPlatformer

// before

1 using UnityEngine;
2 using System.Collections;
3
4 public class Gun : MonoBehaviour
5 {
6     public GameObject rocketGO;                // Prefab of the rocket.
7     public float speed = 20f;                // The speed the rocket will fire at.

// after (Line 4 and Line 6 are added)

 1 using UnityEngine;
 2 using System.Collections;
 3
 4 using SharpKit.JavaScript;
 5
 6 [JsType(JsMode.Clr,"../../StreamingAssets/JavaScript/SharpKitGenerated/2DPlatformer/Scripts/Gun.javascript")]
 7 public class Gun : MonoBehaviour
 8 {
 9     public GameObject rocketGO;                // Prefab of the rocket.
10     public float speed = 20f;                // The speed the rocket will fire at.

6. use sharpkit to compile C# scripts to JavaScript

open Visual Studio project from Unity menu

build sharpkit project

sharpkit will compile all C# classes marked with JsType attrubute to JavaScript and output result. Output file path is specified by 2nd parameter of JsType attribute.

all JavaScript files are all generated in StreamingAssets/JavaScript/SharpKitGenerated/2DPlatformer/ folder.

7. Click menu: JSB | Generate SharpKit JsType file CS.require list

 1 /* Generated by JSBinding.
 2
 3 * see JSAnalyzer.cs / OutputAllTypesWithJsTypeAttribute() function
 4
 5 * better not modify manually.
 6
 7 */
 8
 9
10
11 CS.require("SharpKitGenerated/2DPlatformer/Scripts/BackgroundParallax.javascript");
12 CS.require("SharpKitGenerated/2DPlatformer/Scripts/BackgroundPropSpawner.javascript");
13 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Bomb.javascript");
14 CS.require("SharpKitGenerated/2DPlatformer/Scripts/BombPickup.javascript");
15 CS.require("SharpKitGenerated/2DPlatformer/Scripts/CameraFollow.javascript");
16 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Destroyer.javascript");
17 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Enemy_Data.javascript");
18 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Enemy.javascript");
19 CS.require("SharpKitGenerated/2DPlatformer/Scripts/FollowPlayer.javascript");
20 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Gun.javascript");
21 CS.require("SharpKitGenerated/2DPlatformer/Scripts/HealthPickup.javascript");
22 CS.require("SharpKitGenerated/2DPlatformer/Scripts/LayBombs.javascript");
23 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Pauser.javascript");
24 CS.require("SharpKitGenerated/2DPlatformer/Scripts/PickupSpawner.javascript");
25 CS.require("SharpKitGenerated/2DPlatformer/Scripts/PlayerControl.javascript");
26 CS.require("SharpKitGenerated/2DPlatformer/Scripts/PlayerHealth.javascript");
27 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Remover.javascript");
28 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Rocket.javascript");
29 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Score.javascript");
30 CS.require("SharpKitGenerated/2DPlatformer/Scripts/ScoreShadow.javascript");
31 CS.require("SharpKitGenerated/2DPlatformer/Scripts/SetParticleSortingLayer.javascript");
32 CS.require("SharpKitGenerated/2DPlatformer/Scripts/Spawner.javascript");
33 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/DebugMessages.javascript");
34 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/Message.javascript");
35 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/MuchTweenSoWow.javascript");
36 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/TestEasingFunctions.javascript");
37 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/TestFPS.javascript");
38 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/TestHighConcurrency.javascript");
39 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/TestHighConcurrencyGroup.javascript");
40 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/TestShakeObject.javascript");
41 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/TestSlinky.javascript");
42 CS.require("SharpKitGenerated/DaikonForge Tween (Pro)/Examples/Scripts/TestSplineTween.javascript");
43 CS.require("SharpKitGenerated/JSBinding/Samples/DelegateTest.javascript");
44 CS.require("SharpKitGenerated/JSBinding/Samples/DictionaryTest.javascript");
45 CS.require("SharpKitGenerated/JSBinding/Samples/ListTest.javascript");
46 CS.require("SharpKitGenerated/JSBinding/Samples/PerformanceTest1.javascript");
47 CS.require("SharpKitGenerated/JSBinding/Samples/Serialization/Car.javascript");
48 CS.require("SharpKitGenerated/JSBinding/Samples/Serialization/SerializeSimple.javascript");
49 CS.require("SharpKitGenerated/JSBinding/Samples/Serialization/SerializeStruct.javascript");
50 CS.require("SharpKitGenerated/JSBinding/Samples/Serialization/AppleInfo.javascript");
51 CS.require("SharpKitGenerated/JSBinding/Samples/Serialization/Wheel.javascript");
52 CS.require("SharpKitGenerated/JSBinding/Samples/TestCoroutine.javascript");
53 CS.require("SharpKitGenerated/JSBinding/Samples/V3Test.javascript");
54 CS.require("SharpKitGenerated/JSBinding/Samples/Viewer.javascript");
55 CS.require("SharpKitGenerated/JSBinding/Samples/stScene.javascript");
56 CS.require("SharpKitGenerated/z_temp/test0610.javascript");

8. Remove all reference to C# scripts from profabs and GameObjects

Because out destination is to delete all C# scripts, no any prefabs nor GameObjests can refer to C# scripts.

We are going to replace all mono behaviours with JSComponent.

JSComponent is a MonoBehaviour which redirect every event (Awake, Start, etc.) to JavaScript.

 1 2DPlatformer/Prefabs/Characters/enemy1.prefab
 2 2DPlatformer/Prefabs/Characters/enemy2.prefab
 3 2DPlatformer/Prefabs/Characters/hero.prefab
 4 2DPlatformer/Prefabs/Environment/backgroundAnimation.prefab
 5 2DPlatformer/Prefabs/Environment/backgrounds.prefab
 6 2DPlatformer/Prefabs/Environment/Bus.prefab
 7 2DPlatformer/Prefabs/Environment/Cab.prefab
 8 2DPlatformer/Prefabs/Environment/env_clouds.prefab
 9 2DPlatformer/Prefabs/Environment/env_fog.prefab
10 2DPlatformer/Prefabs/Environment/env_riverMid.prefab
11 2DPlatformer/Prefabs/Environment/env_riverTop.prefab
12 2DPlatformer/Prefabs/Environment/Foregrounds.prefab
13 2DPlatformer/Prefabs/FX/part_splash.prefab
14 2DPlatformer/Prefabs/FX/part_warp.prefab
15 2DPlatformer/Prefabs/FX/splash.prefab
16 2DPlatformer/Prefabs/killTrigger.prefab
17 2DPlatformer/Prefabs/mainCamera.prefab
18 2DPlatformer/Prefabs/pickupManager.prefab
19 2DPlatformer/Prefabs/platformEnd.prefab
20 2DPlatformer/Prefabs/Props/bomb.prefab
21 2DPlatformer/Prefabs/Props/bombCrate.prefab
22 2DPlatformer/Prefabs/Props/explosionCircle.prefab
23 2DPlatformer/Prefabs/Props/explosionParticle.prefab
24 2DPlatformer/Prefabs/Props/healthCrate.prefab
25 2DPlatformer/Prefabs/Props/rocket.prefab
26 2DPlatformer/Prefabs/Props/rocketExplosion.prefab
27 2DPlatformer/Prefabs/Props/swan.prefab
28 2DPlatformer/Prefabs/spawner.prefab
29 2DPlatformer/Prefabs/UI/ui_100points.prefab
30 2DPlatformer/Prefabs/UI/ui_bombHUD.prefab
31 2DPlatformer/Prefabs/UI/ui_healthDisplay.prefab
32 2DPlatformer/Scenes/Level.unity

// before

// after

9. open scene 2DPlatformer/Scenes/Level.unity and drag a JSBinding/Prefabs/_JSEngine.prefab to the scene

It‘s done!!! Click play button to play the scene.

If everything goes right, you can delete 2DPlatformer/Scripts/ folder now !!

时间: 2024-09-30 15:18:41

JSBinding + SharpKit / Convert 2DPlatformer to JavaScript version的相关文章

JSBinding + SharpKit / Home

Description JSBinding is a great tool enabling you to run actual JavaScript in Unity3D. It contains Mozilla SpiderMonkey JavaScript engine version 33 library. New version is expected to work with SharpKit (sharpkit.net). SharpKit is an open source to

JSBinding + SharpKit / 实战:转换 2DPlatformer

2DPlatformer 是 Unity3D 的一个官方 Demo.本文将介绍使用 JSBinging + SharpKit 转换 2DPlatformer 的过程. 本文并不详细介绍每个步骤的细节.因为他们将在其他文章里做详细介绍. 准备工作:首先准备好 JSBinding 的工程,正确导入 JSBinding 插件. 2DPlatformer 是Unity3D的第一个示例代码,一共不到10M.麻雀虽小,五脏俱全.牵扯到很多东西,比如 prefab 的使用,结构体序列化,场景切换,音乐等等.

JSBinding+SharpKit / 菜单介绍

[JSB | Generate JS and CS Bindings] 生成绑定,即让 Js 和 Cs 互通.详情请看 JSBinding+SharpKit / 生成 JavaScript 绑定 [JSB | Add SharpKit JsType Attribute for all Structs and Classes] 在所有逻辑代码中,所有类的定义前面加上 [JsType(JsMode.Clr, "~/Assets/StreamingAssets/JavaScript/SharpKitG

JSBinding + SharpKit / Important Notes

Serialization of List<T> is not supported. public List<int> lst; // NOT supported, use int[] instead About menu: JSB | Add SharpKit JsType Attribute for all Structs and Classes. If you execute this menu more than once, only one JsType will be

JSBinding + SharpKit / To JavaScript-Only users

This plugin is mainly desined to work with SharpKit. You are expected to write C# and get JavaScript code with help of SharpKit. If you don't like C# and would like to write JavaScrtipt only, Yes, you can do this, in version 1.5. But it will be a lit

JSBinding + SharpKit / 编译 Cs 成 Js

轻轻一点菜单:[JSB | Compile Cs to Js] 主要产出:StreamingAssets/JavaScript/SharpkitGeneratedFiles.javascript,你的所有逻辑代码都在这里 其他产出: Temp/AllInvocations.txt:记录所有逻辑代码对框架代码的调用 (1) Temp/AllInvocationsWithLocation.txt:同上,但同时记录每个调用的文件名和行号 (2) Temp/YieldReturnTypes.txt:记录

JSBinding + SharpKit / run samples

Import JSBinding package to your project. on Windows 1) for 32bit editor: copy Assets/Plugins/x86/mozjs-33.dll to UnityInstallDir/Editor folder 2) for 64bit editor: copy Assets/Plugins/x86_64/mozjs-33.dll to UnityInstallDir/Editor folder open scene J

JSBinding + SharpKit / 原理篇:Delegate

以 NGUI 的 UIEventListener 为例: 有一个类: 1 using SharpKit.JavaScript; 2 using UnityEngine; 3 using System.Collections; 4 5 [JsType(JsMode.Clr,"../StreamingAssets/JavaScript/SharpKitGenerated/z_temp/test0610.javascript")] 6 public class test0610 : Mono

JSBinding + SharpKit / Supporting Coroutine

This document explains in detail how JSBinding supports Unity Coroutine in JavaScript. First, I suggest you read this page to understand coroutine scheduling: http://wiki.unity3d.com/index.php/CoroutineScheduler and also Yield instructions in Mozilla