Skip to content

Commit 0eb797c

Browse files
committed
improve unity release
1 parent 1df7673 commit 0eb797c

5 files changed

Lines changed: 32 additions & 399 deletions

File tree

.github/workflows/unity-upm-release.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ jobs:
4343
cp src/NativeWebSocket/IWebSocket.cs upm-package/WebSocket/
4444
cp src/NativeWebSocket/WebSocketTypes.cs upm-package/WebSocket/
4545
46-
# Unity-specific files (WebSocket.cs has #if !UNITY_WEBGL guard,
47-
# WebSocketWebGL.cs + WebSocketFactoryWebGL.cs have #if UNITY_WEBGL guard)
48-
cp integrations/Unity/WebSocket.cs upm-package/WebSocket/
46+
# Wrap core WebSocket.cs with Unity conditional compilation guard
47+
{ echo '#if !UNITY_WEBGL || UNITY_EDITOR'; echo ''; cat src/NativeWebSocket/WebSocket.cs; echo ''; echo '#endif'; } > upm-package/WebSocket/WebSocket.cs
48+
49+
# Unity-specific files (WebGL platform support)
4950
cp integrations/Unity/WebSocketWebGL.cs upm-package/WebSocket/
5051
cp integrations/Unity/WebSocketFactoryWebGL.cs upm-package/WebSocket/
5152
cp integrations/Unity/WebSocket.jslib upm-package/WebSocket/
@@ -81,8 +82,10 @@ jobs:
8182
cp src/NativeWebSocket/IWebSocket.cs upm-package/Assets/WebSocket/
8283
cp src/NativeWebSocket/WebSocketTypes.cs upm-package/Assets/WebSocket/
8384
85+
# Wrap core WebSocket.cs with Unity conditional compilation guard
86+
{ echo '#if !UNITY_WEBGL || UNITY_EDITOR'; echo ''; cat src/NativeWebSocket/WebSocket.cs; echo ''; echo '#endif'; } > upm-package/Assets/WebSocket/WebSocket.cs
87+
8488
# Unity-specific files
85-
cp integrations/Unity/WebSocket.cs upm-package/Assets/WebSocket/
8689
cp integrations/Unity/WebSocketWebGL.cs upm-package/Assets/WebSocket/
8790
cp integrations/Unity/WebSocketFactoryWebGL.cs upm-package/Assets/WebSocket/
8891
cp integrations/Unity/WebSocket.jslib upm-package/Assets/WebSocket/

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ Used in [Colyseus Unity SDK](https://github.com/colyseus/colyseus-unity-sdk).
1616

1717
*Requires Unity 2019.1+ with .NET 4.x+ Runtime*
1818

19+
> **Note:** Do not copy the raw source files from this repository directly into
20+
> your Unity project. The core `WebSocket.cs` requires a build-time
21+
> transformation to add WebGL conditional compilation guards. Use one of the
22+
> install methods below instead.
23+
1924
**Via UPM (Unity Package Manager):**
2025
1. Open Unity
2126
2. Open Package Manager Window
2227
3. Click Add Package From Git URL
23-
4. Enter URL: `https://github.com/endel/NativeWebSocket.git#upm`
28+
4. Enter URL: `https://github.com/endel/NativeWebSocket.git#upm-2.0`
2429

25-
**Manually:**
26-
1. [Download this project](https://github.com/endel/NativeWebSocket/archive/master.zip)
27-
2. Copy the sources from `NativeWebSocket/Assets/WebSocket` into your `Assets` directory.
30+
**Via .unitypackage:**
31+
1. Download `NativeWebSocket.unitypackage` from the [Releases](https://github.com/endel/NativeWebSocket/releases) page
32+
2. In Unity, go to Assets > Import Package > Custom Package and select the downloaded file
2833

2934
## MonoGame / .NET
3035

@@ -60,6 +65,8 @@ public class Connection : MonoBehaviour
6065

6166
async void Start()
6267
{
68+
Application.runInBackground = true; // Recommended for WebGL
69+
6370
websocket = new WebSocket("ws://localhost:3000");
6471

6572
websocket.OnOpen += () => Debug.Log("Connection open!");
@@ -93,6 +100,11 @@ public class Connection : MonoBehaviour
93100
}
94101
```
95102

103+
**WebGL note:** Unity pauses the game loop when the browser tab loses focus, which
104+
stops all WebSocket send/receive callbacks. To keep the connection active in the
105+
background, set `Application.runInBackground = true` in your script or enable
106+
**Run In Background** in Player Settings > Resolution and Presentation.
107+
96108
## MonoGame
97109

98110
Add the `WebSocketGameComponent` to your game. This installs a

examples/Unity/Connection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class Connection : MonoBehaviour
2525

2626
async void Start()
2727
{
28+
Application.runInBackground = true;
29+
2830
websocket = new WebSocket("ws://localhost:3000");
2931

3032
websocket.OnOpen += () =>

0 commit comments

Comments
 (0)