feat unity point客户端初始化

This commit is contained in:
2025-12-15 11:23:22 +08:00
parent 3498cf158d
commit 1bd50d298a
34 changed files with 3819 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
using UnityEngine;
using NativeWebSocket;
public class Connection : MonoBehaviour
{
WebSocket websocket;
async void Start()
{
websocket = new WebSocket("ws://localhost:8501?token=1");
websocket.OnOpen += () => { Debug.Log("Connection open!"); };
websocket.OnError += (e) => { Debug.Log("Error! " + e); };
websocket.OnClose += (e) => { Debug.Log("Connection closed!"); };
websocket.OnMessage += (bytes) =>
{
Debug.Log("OnMessage!");
Debug.Log(bytes);
// getting the message as a string
// var message = System.Text.Encoding.UTF8.GetString(bytes);
// Debug.Log("OnMessage! " + message);
};
// Keep sending messages at every 0.3s
// InvokeRepeating("SendWebSocketMessage", 0.0f, 0.3f);
// waiting for messages
await websocket.Connect();
}
void Update()
{
#if !UNITY_WEBGL || UNITY_EDITOR
websocket.DispatchMessageQueue();
#endif
}
async void SendWebSocketMessage()
{
if (websocket.State == WebSocketState.Open)
{
// Sending bytes
await websocket.Send(new byte[] { 10, 20, 30 });
// Sending plain text
await websocket.SendText("plain text message");
}
}
private async void OnApplicationQuit()
{
await websocket.Close();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 978b6860fdd772442ba87da5c3024a0d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float moveSpeed = 5f; // 移动速度
private void Update()
{
var moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
var moveY = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
transform.Translate(moveX, moveY, 0);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e8262c63fb4770549befe57c9245251c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: