quakeball/Assets/Scripts/Net/CanvasInput.cs

62 lines
2.1 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using NeonTea.Quakeball.TeaNet.Peers;
using NeonTea.Quakeball.Net.Packets;
using System;
namespace NeonTea.Quakeball.Net {
2020-08-05 03:21:04 +02:00
public class CanvasInput : MonoBehaviour, PeerMessageListener {
public Button Host;
public Button Join;
2020-08-05 03:21:04 +02:00
public Button Stop;
public Button Send;
public InputField HostAddr;
public InputField Port;
public InputField MessageField;
2020-08-05 03:21:04 +02:00
public Text TextField;
private static List<string> Stuff = new List<string>();
void Start() {
Host.onClick.AddListener(() => {
string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "0.0.0.0" : HostAddr.text;
string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text;
Debug.Log(addr);
Net.Singleton.StartServer(addr, Int32.Parse(port), this);
});
Join.onClick.AddListener(() => {
string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "127.0.0.1" : HostAddr.text;
string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text;
Net.Singleton.StartClient(addr, Int32.Parse(port), this);
});
2020-08-05 03:21:04 +02:00
Stop.onClick.AddListener(() => {
Net.Singleton.Stop();
});
Send.onClick.AddListener(() => {
if (Net.Singleton.Peer != null) {
foreach (ulong uid in Net.Singleton.Connections) {
HelloPckt pckt = new HelloPckt();
pckt.Text = MessageField.text;
Net.Singleton.Peer.SendReliableLater(uid, pckt);
Net.Singleton.Peer.SendReliable(uid, pckt);
}
}
});
2020-08-05 03:21:04 +02:00
}
void Update() {
TextField.text = string.Join("\n", Stuff.ToArray());
}
public void Message(string text) {
Stuff.Add(text);
Debug.Log(text);
}
2020-08-05 21:54:53 +02:00
public void Err(string text) {
Debug.Log(text);
}
}
}