feat unity point客户端初始化
This commit is contained in:
58
Client/Point/Assets/Scripts/Connection.cs
Normal file
58
Client/Point/Assets/Scripts/Connection.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
11
Client/Point/Assets/Scripts/Connection.cs.meta
Normal file
11
Client/Point/Assets/Scripts/Connection.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 978b6860fdd772442ba87da5c3024a0d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
14
Client/Point/Assets/Scripts/PlayerMove.cs
Normal file
14
Client/Point/Assets/Scripts/PlayerMove.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
11
Client/Point/Assets/Scripts/PlayerMove.cs.meta
Normal file
11
Client/Point/Assets/Scripts/PlayerMove.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8262c63fb4770549befe57c9245251c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user