111 lines
3.0 KiB
C#
111 lines
3.0 KiB
C#
using NoSugarNet.ClientCoreNet.Standard2;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class MainUI : MonoBehaviour
|
||
{
|
||
|
||
public Button btnStart;
|
||
public Button btnStop;
|
||
public InputField inputIP;
|
||
public InputField inputPort;
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
|
||
string LastIP = PlayerPrefs.GetString("LastIP");
|
||
string LastPort = PlayerPrefs.GetString("LastPort");
|
||
if(!string.IsNullOrEmpty(LastIP))
|
||
inputIP.text = LastIP;
|
||
if (!string.IsNullOrEmpty(LastPort))
|
||
inputPort.text = LastPort;
|
||
|
||
btnStart.onClick.AddListener(InitNoSugarNetClient);
|
||
btnStop.onClick.AddListener(StopNoSugarNetClient);
|
||
AddLog("");
|
||
|
||
AppNoSugarNet.OnUpdateStatus += OnUpdateStatus;
|
||
AppNoSugarNet.Init(OnNoSugarNetLog);
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
AppNoSugarNet.Close();
|
||
}
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
|
||
}
|
||
|
||
void InitNoSugarNetClient()
|
||
{
|
||
if (string.IsNullOrEmpty(inputIP.text))
|
||
{
|
||
OnNoSugarNetLog(0,"ÅäÖôíÎó");
|
||
return;
|
||
}
|
||
|
||
PlayerPrefs.SetString("LastIP", inputIP.text);
|
||
PlayerPrefs.GetString("LastPort",inputPort.text);
|
||
AppNoSugarNet.Connect(inputIP.text, Convert.ToInt32(inputPort.text));
|
||
}
|
||
void StopNoSugarNetClient()
|
||
{
|
||
AppNoSugarNet.Close();
|
||
}
|
||
|
||
|
||
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()
|
||
{
|
||
// ´´½¨Ò»¸öеÄGUIStyle
|
||
GUIStyle style = new GUIStyle();
|
||
|
||
// ÉèÖÃ×ÖÌå´óС
|
||
style.fontSize = 24;
|
||
style.fontStyle = FontStyle.Bold;
|
||
style.normal.textColor = Color.white;
|
||
|
||
GUI.TextField(new Rect(10, 400, Screen.width - 20, 1520), logText, style);
|
||
}
|
||
|
||
static void AddLog(string msg)
|
||
{
|
||
// Ìí¼ÓеÄÈÕÖ¾ÐÅÏ¢µ½Îı¾¿ò
|
||
logText += string.Format("{0}\n", System.DateTime.Now.ToString("HH:mm:ss> ") + msg);
|
||
|
||
// ÏÞÖÆÎı¾¿òÏÔʾµÄ×î´óÐÐÊý
|
||
if (logText.Split('\n').Length > 80)
|
||
{
|
||
string[] lines = logText.Split('\n');
|
||
logText = "";
|
||
for (int i = lines.Length - 50; i < lines.Length; i++)
|
||
{
|
||
logText += lines[i] + "\n";
|
||
}
|
||
}
|
||
}
|
||
}
|