NoSugarNet/NosugarNetForUnity/Assets/MainUI.cs

111 lines
3.0 KiB
C#
Raw Normal View History

2024-04-15 15:16:10 +08:00
using NoSugarNet.ClientCoreNet.Standard2;
2024-04-15 11:32:03 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainUI : MonoBehaviour
{
public Button btnStart;
2024-04-15 15:16:10 +08:00
public Button btnStop;
2024-04-15 11:32:03 +08:00
public InputField inputIP;
public InputField inputPort;
// Start is called before the first frame update
void Start()
{
2024-04-15 15:16:10 +08:00
string LastIP = PlayerPrefs.GetString("LastIP");
string LastPort = PlayerPrefs.GetString("LastPort");
if(!string.IsNullOrEmpty(LastIP))
inputIP.text = LastIP;
if (!string.IsNullOrEmpty(LastPort))
inputPort.text = LastPort;
2024-04-15 11:32:03 +08:00
btnStart.onClick.AddListener(InitNoSugarNetClient);
2024-04-15 15:16:10 +08:00
btnStop.onClick.AddListener(StopNoSugarNetClient);
AddLog("");
AppNoSugarNet.OnUpdateStatus += OnUpdateStatus;
AppNoSugarNet.Init(OnNoSugarNetLog);
}
private void OnDisable()
{
AppNoSugarNet.Close();
2024-04-15 11:32:03 +08:00
}
// Update is called once per frame
void Update()
{
}
void InitNoSugarNetClient()
{
if (string.IsNullOrEmpty(inputIP.text))
{
OnNoSugarNetLog(0,"<22><><EFBFBD>ô<EFBFBD><C3B4><EFBFBD>");
return;
}
2024-04-15 15:16:10 +08:00
PlayerPrefs.SetString("LastIP", inputIP.text);
PlayerPrefs.GetString("LastPort",inputPort.text);
AppNoSugarNet.Connect(inputIP.text, Convert.ToInt32(inputPort.text));
}
void StopNoSugarNetClient()
{
AppNoSugarNet.Close();
2024-04-15 11:32:03 +08:00
}
static void OnUpdateStatus(NetStatus netState)
{
string info = $"User:{netState.ClientUserCount} Tun:{netState.TunnelCount} rec: {ConvertBytesToKilobytes(netState.srcReciveSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tReciveSecSpeed)}K/s send: {ConvertBytesToKilobytes(netState.srcSendSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tSendSecSpeed)}K/s";
OnNoSugarNetLog(0,info);
}
static string ConvertBytesToKilobytes(long bytes)
{
return Math.Round((double)bytes / 1024, 2).ToString("F2");
}
static void OnNoSugarNetLog(int LogLevel, string msg)
{
Debug.Log(msg);
AddLog(msg);
}
static string logText = "";
void OnGUI()
{
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>µ<EFBFBD>GUIStyle
GUIStyle style = new GUIStyle();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
style.fontSize = 24;
2024-04-15 15:16:10 +08:00
style.fontStyle = FontStyle.Bold;
style.normal.textColor = Color.white;
GUI.TextField(new Rect(10, 400, Screen.width - 20, 1520), logText, style);
2024-04-15 11:32:03 +08:00
}
static void AddLog(string msg)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>־<EFBFBD><D6BE>Ϣ<EFBFBD><CFA2><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
logText += string.Format("{0}\n", System.DateTime.Now.ToString("HH:mm:ss> ") + msg);
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-04-15 15:16:10 +08:00
if (logText.Split('\n').Length > 80)
2024-04-15 11:32:03 +08:00
{
string[] lines = logText.Split('\n');
logText = "";
for (int i = lines.Length - 50; i < lines.Length; i++)
{
logText += lines[i] + "\n";
}
}
}
}