This commit is contained in:
杨号敬 2025-01-09 12:03:44 +08:00
parent 7d038da1e7
commit 9bf04fe941
4 changed files with 497 additions and 97 deletions

View File

@ -5533,7 +5533,7 @@ RectTransform:
m_Father: {fileID: 2031406836}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0.06608202}
m_AnchorMin: {x: 0, y: 0.06608212}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 20, y: 20}
@ -10454,7 +10454,7 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 64216422}
m_HandleRect: {fileID: 64216421}
m_Direction: 2
m_Value: 0.99999857
m_Value: 1
m_Size: 0.9339179
m_NumberOfSteps: 0
m_OnValueChanged:
@ -138518,7 +138518,8 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1479024099}
- component: {fileID: 1479024100}
- component: {fileID: 1479024101}
- component: {fileID: 1479024102}
m_Layer: 5
m_Name: The_best_fisherman_PK
m_TagString: Untagged
@ -138547,7 +138548,29 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1479024100
--- !u!114 &1479024101
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1479024098}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9af3f04677f530449a0810b75783fbca, type: 3}
m_Name:
m_EditorClassIdentifier:
TimeText: {fileID: 1804395153}
introtext: {fileID: 1425714360}
lefttext: {fileID: 1943550231}
righttext: {fileID: 1010492249}
TouruBtn: {fileID: 1300542675}
selleftbtn: {fileID: 974547746}
selrightbtn: {fileID: 977172793}
Dropdown: {fileID: 1336765447}
photoMovement: {fileID: 1011623730}
photoMovement1: {fileID: 680775552775591948}
--- !u!114 &1479024102
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
@ -138567,9 +138590,7 @@ MonoBehaviour:
selleftbtn: {fileID: 974547746}
selrightbtn: {fileID: 977172793}
Dropdown: {fileID: 1336765447}
returnbtn: {fileID: 2127736424}
photoMovement: {fileID: 1011623730}
photoMovement1: {fileID: 680775552775591948}
--- !u!1 &1479805049
GameObject:
m_ObjectHideFlags: 0
@ -200824,17 +200845,6 @@ MonoBehaviour:
m_EditorClassIdentifier:
gotoscene: {fileID: 1192041085}
nowScene: {fileID: 1479024098}
--- !u!114 &2127736424 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 8631885451795898584, guid: 638b7f7423db411419568d073208d929, type: 3}
m_PrefabInstance: {fileID: 989483048}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2127736419}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &2128193624
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,405 @@
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using Newtonsoft.Json;
using System.Drawing;
using UnityEngine.UI;
public class FishingPK : MonoBehaviour
{
private ClientWebSocket _webSocket;
private const string WebSocketUri = "ws://47.95.201.243:9527/api/ws";
private string AuthorizationValue;
/// <summary>
/// 倒计时
/// </summary>
private float lastCallTime = 0f;
float remainingTime = 0f;
public Text TimeText;
private float interval = 1f; // 每秒调用一次
/// <summary>
/// 文本描述
/// </summary>、
public Text introtext;
/// <summary>
/// 左右金币
/// </summary>
public Text lefttext;
public Text righttext;
public Button TouruBtn;
public Button selleftbtn;
public Button selrightbtn;
public Dropdown Dropdown;
int pos = 0;
private UnityEngine.Color normalColor = UnityEngine.Color.white; // 默认颜色
private UnityEngine.Color selectedColor = UnityEngine.Color.blue; // 选中时的颜色
/// <summary>
/// 钓鱼
/// </summary>
///
public PhotoMovement photoMovement;
public PhotoMovement photoMovement1;
private async void Start()
{
TimeText.gameObject.SetActive(false);
Debug.Log(PlayerPrefs.GetString("UserToken"));
AuthorizationValue = PlayerPrefs.GetString("UserToken");
await ConnectWebSocket();
selleftbtn.onClick.AddListener(() =>
{
pos = 1;
ChangeButtonColor(selleftbtn, selectedColor);
ChangeButtonColor(selrightbtn, normalColor); // 恢复右侧按钮颜色
});
// 为右侧按钮添加点击监听器
selrightbtn.onClick.AddListener(() =>
{
pos = 2;
ChangeButtonColor(selrightbtn, selectedColor);
ChangeButtonColor(selleftbtn, normalColor); // 恢复左侧按钮颜色
});
// 调用发送方法
await SendJsonMessage("{ \"code\": \"FISHING_PK\", \"content\": \"{\\\"action\\\":\\\"INFO\\\"}\" }");
TouruBtn.onClick.AddListener(async() =>
{
await SendJsonMessage(int.Parse(Dropdown.options[Dropdown.value].text), pos);
});
}
private void ChangeButtonColor(Button btn, UnityEngine.Color color)
{
btn.gameObject.GetComponent<Image>().color = color;
}
private async Task ConnectWebSocket()
{
_webSocket = new ClientWebSocket();
// 添加 Authorization 头
_webSocket.Options.SetRequestHeader("Authorization", "Bearer " + AuthorizationValue);
try
{
Debug.Log("正在连接到 WebSocket...");
await _webSocket.ConnectAsync(new Uri(WebSocketUri), CancellationToken.None);
Debug.Log("WebSocket 连接成功!");
// 开始接收消息
_ = ReceiveMessages();
}
catch (Exception e)
{
Debug.LogError($"WebSocket 连接失败:{e.Message}");
}
}
private async Task ReceiveMessages()
{
var buffer = new byte[1024];
try
{
while (_webSocket.State == WebSocketState.Open)
{
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
Debug.Log("WebSocket 连接已被服务器关闭。");
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "关闭连接", CancellationToken.None);
}
else
{
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
Debug.Log($"接收到消息:{message}");
var baseResponse = JsonConvert.DeserializeObject<BaseResponse>(message);
//Promptmgr.Instance.PromptBubble(baseResponse.message);
if (baseResponse != null)
{
switch (baseResponse.code)
{
// 解析消息为 钓手PK信息
case "FISHING_PK_INFO":
// 解析为 FishJoinroomresponse 类型
FishPKInforResponse fishResponse = JsonConvert.DeserializeObject<FishPKInforResponse>(message);
//Promptmgr.Instance.PromptBubble(fishResponse.message);
if (fishResponse?.data != null)
{
Debug.Log(fishResponse.data.balance);
Debug.Log(fishResponse.data.intro_text);
introtext.text = fishResponse.data.intro_text;
Debug.Log(fishResponse.data.countdown);
TimeText.gameObject.SetActive(true);
remainingTime = (float)fishResponse.data.countdown;
Debug.Log(fishResponse.data.countdown_type);
if(fishResponse.data.countdown_type==0)
{
photoMovement.type = true;
photoMovement.Del1();
photoMovement.To1();
photoMovement1.type = true;
photoMovement1.Del1();
photoMovement1.To1();
}
else
{
photoMovement.Del1();
photoMovement.type = false;
photoMovement.To2();
photoMovement1.Del1();
photoMovement1.type = false;
photoMovement1.To2();
}
Debug.Log(fishResponse.data.amount_left);
lefttext.text= fishResponse.data.amount_left.ToString();
Debug.Log(fishResponse.data.amount_right);
righttext.text= fishResponse.data.amount_right.ToString();
}
break;
//解析为钓手PK投入
case "FISHING_PK_BETTING":
// 解析消息为 Fishresponse 对象
FishPKBetonResponse fishResponse1 = JsonConvert.DeserializeObject<FishPKBetonResponse>(message);
Promptmgr.Instance.PromptBubble(fishResponse1.message);
// 检查是否成功反序列化
if (fishResponse1 != null && fishResponse1.data != null)
{
Debug.Log(fishResponse1.data.balance);
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
break;
//解析为钓手Pk 倒计时
case "FISHING_PK_COUNTDOWN":
// 解析消息为 Fishresponse 对象
FishPKCDResponse fishResponse2 = JsonConvert.DeserializeObject<FishPKCDResponse>(message);
//Promptmgr.Instance.PromptBubble(fishResponse2.message);
// 检查是否成功反序列化
if (fishResponse2 != null && fishResponse2.data != null)
{
Debug.Log(fishResponse2.data.countdown);
remainingTime = (float)fishResponse2.data.countdown;
Debug.Log(fishResponse2.data.countdown_type);
if (fishResponse2.data.countdown_type == 0)
{
photoMovement.type = true;
photoMovement.Del1();
photoMovement.To1();
photoMovement1.type = true;
photoMovement1.Del1();
photoMovement1.To1();
}
else
{
photoMovement.Del1();
photoMovement.type = false;
photoMovement.To2();
photoMovement1.Del1();
photoMovement1.type = false;
photoMovement1.To2();
}
Debug.Log(fishResponse2.data.amount_left);
Debug.Log(fishResponse2.data.amount_right);
lefttext.text = fishResponse2.data.amount_left.ToString();
righttext.text = fishResponse2.data.amount_right.ToString();
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
break;
case "FISHING_PK_SETTLE":
FishPKSETResponse fishResponse3 = JsonConvert.DeserializeObject<FishPKSETResponse>(message);
//Promptmgr.Instance.PromptBubble(fishResponse2.message);
// 检查是否成功反序列化
if (fishResponse3 != null && fishResponse3.data != null)
{
Debug.Log(fishResponse3.data.balance);
Debug.Log(fishResponse3.data.status);
Debug.Log(fishResponse3.data.amount);
Debug.Log(fishResponse3.data.reward);
Debug.Log(fishResponse3.data.weight_left);
Debug.Log(fishResponse3.data.weight_right);
Promptmgr.Instance.PromptBubble(fishResponse3.data.status);
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
break;
}
}
}
}
}
catch (Exception e)
{
Debug.LogError($"接收消息时出错:{e.Message}");
}
}
public async Task SendJsonMessage(int count, int postion)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open)
{
Debug.LogError("WebSocket 未连接,无法发送消息。");
return;
}
try
{
var message = new
{
code = "FISHING_PK",
content = $"{{\"action\":\"BETTING\",\"amount\":{count},\"position\":{postion}}}"
};
// 将对象序列化为 JSON 字符串
string jsonMessage = JsonConvert.SerializeObject(message);
var encodedMessage = Encoding.UTF8.GetBytes(jsonMessage);
var buffer = new ArraySegment<byte>(encodedMessage);
await _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
Debug.Log($"已发送 JSON 消息:{jsonMessage}");
}
catch (Exception e)
{
Debug.LogError($"发送消息时出错:{e.Message}");
}
}
public async Task SendJsonMessage(string json)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open)
{
Debug.LogError("WebSocket 未连接,无法发送消息。");
return;
}
try
{
var encodedMessage = Encoding.UTF8.GetBytes(json);
var buffer = new ArraySegment<byte>(encodedMessage);
await _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
Debug.Log($"已发送 JSON 消息:{json}");
}
catch (Exception e)
{
Debug.LogError($"发送消息时出错:{e.Message}");
}
}
private async void OnApplicationQuit()
{
if (_webSocket != null && _webSocket.State == WebSocketState.Open)
{
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "应用程序退出", CancellationToken.None);
_webSocket.Dispose();
Debug.Log("WebSocket 连接已关闭。");
}
}
void UpdateCountdownText(float remainingTime)
{
// 将剩余时间转换为小时、分钟和秒
int hours = Mathf.FloorToInt(remainingTime / 3600);
int minutes = Mathf.FloorToInt((remainingTime % 3600) / 60);
int seconds = Mathf.FloorToInt(remainingTime % 60);
// 使用格式化字符串显示倒计时00:00:00
TimeText.text = "倒计时:"+string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds);
}
private void Update()
{
if (Time.time - lastCallTime >= interval)
{
// 每秒调用一次的代码
if (remainingTime > 0)
{
remainingTime -= 1;
UpdateCountdownText(remainingTime);
Debug.Log("进入倒计时");
}
// 更新上次调用时间
lastCallTime = Time.time;
}
}
}
public class FishPKInforResponse
{
public string code { get; set; }
public string message { get; set; }
public FishPKInfor data { get; set; }
}
public class FishPKInfor
{
public float balance { get; set; }
public string intro_text { get; set; }
public int countdown { get; set; }
public int countdown_type { get; set; }
public float amount_left { get; set; }
public float amount_right { get; set; }
}
public class FishPKBetonResponse
{
public string code { get; set; }
public string message { get; set; }
public FishPKdata data { get; set; }
}
public class FishPKdata
{
public float balance { get; set; }
}
public class FishPKCDResponse
{
public string code { get; set; }
public string message { get; set; }
public FishPKCDdata data { get; set; }
}
public class FishPKCDdata
{
public int countdown { get; set; }
public int countdown_type { get; set; }
public float amount_left { get; set; }
public float amount_right { get; set; }
}
public class FishPKSETResponse
{
public string code { get; set; }
public string message { get; set; }
public FishPKSETdata data { get; set; }
}
public class FishPKSETdata
{
public float balance { get; set; }
public string status { get; set; }
public float amount { get; set; }
public float reward { get; set; }
public float weight_left { get; set; }
public float weight_right { get; set; }
}

View File

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

View File

@ -38,13 +38,12 @@ public class WebFishingPK : MonoBehaviour
private UnityEngine.Color normalColor = UnityEngine.Color.white; // 默认颜色
private UnityEngine.Color selectedColor = UnityEngine.Color.blue; // 选中时的颜色
public Button returnbtn;
/// <summary>
/// 钓鱼
/// </summary>
///
public PhotoMovement photoMovement;
public PhotoMovement photoMovement1;
private async void Start()
{
TimeText.gameObject.SetActive(false);
@ -71,10 +70,6 @@ public class WebFishingPK : MonoBehaviour
{
await SendJsonMessage(int.Parse(Dropdown.options[Dropdown.value].text), pos);
});
//returnbtn.onClick.AddListener(async () =>
//{
// await SendJsonMessage("{ \"code\": \"FISHING_PK\", \"content\": \"{\\\"action\\\":\\\"LEAVE\\\"}\" }");
//});
}
private void ChangeButtonColor(Button btn, UnityEngine.Color color)
@ -145,24 +140,13 @@ public class WebFishingPK : MonoBehaviour
Debug.Log(fishResponse.data.countdown_type);
if(fishResponse.data.countdown_type==0)
{
photoMovement.type = true;
photoMovement.Del1();
photoMovement.To1();
photoMovement1.type = true;
photoMovement1.Del1();
photoMovement1.To1();
}
else
{
photoMovement.Del1();
photoMovement.type = false;
photoMovement.To2();
photoMovement1.Del1();
photoMovement1.type = false;
photoMovement1.To2();
}
Debug.Log(fishResponse.data.amount_left);
@ -201,23 +185,13 @@ public class WebFishingPK : MonoBehaviour
Debug.Log(fishResponse2.data.countdown_type);
if (fishResponse2.data.countdown_type == 0)
{
photoMovement.type = true;
photoMovement.Del1();
photoMovement.To1();
photoMovement1.type = true;
photoMovement1.Del1();
photoMovement1.To1();
}
else
{
photoMovement.Del1();
photoMovement.type = false;
photoMovement.To2();
photoMovement1.Del1();
photoMovement1.type = false;
photoMovement1.To2();
}
Debug.Log(fishResponse2.data.amount_left);
Debug.Log(fishResponse2.data.amount_right);
@ -352,59 +326,59 @@ public class WebFishingPK : MonoBehaviour
}
}
public class FishPKInforResponse
{
public string code { get; set; }
public string message { get; set; }
public FishPKInfor data { get; set; }
}
public class FishPKInfor
{
public float balance { get; set; }
//public class FishPKInforResponse
//{
// public string code { get; set; }
// public string message { get; set; }
// public FishPKInfor data { get; set; }
//}
//public class FishPKInfor
//{
// public float balance { get; set; }
public string intro_text { get; set; }
public int countdown { get; set; }
public int countdown_type { get; set; }
public float amount_left { get; set; }
public float amount_right { get; set; }
// public string intro_text { get; set; }
// public int countdown { get; set; }
// public int countdown_type { get; set; }
// public float amount_left { get; set; }
// public float amount_right { get; set; }
}
public class FishPKBetonResponse
{
public string code { get; set; }
public string message { get; set; }
public FishPKdata data { get; set; }
}
public class FishPKdata
{
public float balance { get; set; }
}
public class FishPKCDResponse
{
public string code { get; set; }
public string message { get; set; }
public FishPKCDdata data { get; set; }
}
public class FishPKCDdata
{
public int countdown { get; set; }
public int countdown_type { get; set; }
public float amount_left { get; set; }
public float amount_right { get; set; }
}
public class FishPKSETResponse
{
public string code { get; set; }
public string message { get; set; }
public FishPKSETdata data { get; set; }
}
public class FishPKSETdata
{
public float balance { get; set; }
//}
//public class FishPKBetonResponse
//{
// public string code { get; set; }
// public string message { get; set; }
// public FishPKdata data { get; set; }
//}
//public class FishPKdata
//{
// public float balance { get; set; }
//}
//public class FishPKCDResponse
//{
// public string code { get; set; }
// public string message { get; set; }
// public FishPKCDdata data { get; set; }
//}
//public class FishPKCDdata
//{
// public int countdown { get; set; }
// public int countdown_type { get; set; }
// public float amount_left { get; set; }
// public float amount_right { get; set; }
//}
//public class FishPKSETResponse
//{
// public string code { get; set; }
// public string message { get; set; }
// public FishPKSETdata data { get; set; }
//}
//public class FishPKSETdata
//{
// public float balance { get; set; }
public string status { get; set; }
public float amount { get; set; }
public float reward { get; set; }
public float weight_left { get; set; }
public float weight_right { get; set; }
}
// public string status { get; set; }
// public float amount { get; set; }
// public float reward { get; set; }
// public float weight_left { get; set; }
// public float weight_right { get; set; }
//}