入口场景挂下面脚本 ,一个按钮绑定dianji这个方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TNet;
using System.Net;
public class kkk : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
TNManager.Connect("10.1.40.81:5127");
DontDestroyOnLoad(this);
}
void Update()
{
}
public void dianji()
{
print("dan ji");
TNManager.JoinChannel(88, "haha", false, 255, null);
}
}
场景haha, 挂下面脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TNet;
using TMPro;
public class haha : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
TNManager.onJoinChannel += this.joinChannel; //自己加入到这个房间
TNManager.onPlayerJoin += this.playerJoin; //检测有其他人加入这个房间
TNManager.onPlayerLeave += this.playerLeave; //检测有其他人离开这个房间
}
private void playerLeave(int channelID, Player p)
{
print("Player " + p.id + "离开了通道" + channelID);
}
private void playerJoin(int channelID, Player p)
{
print("Player " + p.id + "加入了通道" + channelID);
}
//下面这个是自己加入房间后调用,注意是自己,自己,自己 ,重点说三遍
private void joinChannel(int channelID, bool success, string message)
{
print(channelID + " " + success + message);
for (int i = 0; i < 3; i++)
{
Vector3 pos = Vector3.up * 3f;
// Object's rotation is completely random
Quaternion rot = Quaternion.Euler(Random.value * 180f, Random.value * 180f, Random.value * 180f);
// Object's color is completely random
Color color = new Color(Random.value, Random.value, Random.value, 1f);
// Create the object using a custom creation function defined below.
// Note that passing "channelID" is optional. If you don't pass anything, TNet will pick one for you.
print("TNManager.Instantiate");
TNManager.Instantiate(88, "ColoredObject", "Created Cube", false, pos, rot, color, 3f);
}
}
[RCC]
static GameObject ColoredObject(GameObject prefab, Vector3 pos, Quaternion rot, Color c, float autoDestroyDelay)
{
print("ColoredObject ColoredObject ColoredObject");
// Instantiate the prefab
GameObject go = prefab.Instantiate();
// Set the position and rotation based on the passed values
Transform t = go.transform;
t.position = pos;
t.rotation = rot;
// Set the renderer's color as well
go.GetComponentInChildren<Renderer>().material.color = c;
// Destroy the object after enough time has passed
if (autoDestroyDelay > 0f) go.DestroySelf(autoDestroyDelay);
return go;
}
public TextMeshProUGUI txt;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
//print(TNManager.playerID);
//print(TNManager.channels.size);
//print(TNManager.channels[0].id);
//print(TNManager.lastChannelID);
//print(TNManager.isConnected);
//print(TNManager.IsInChannel(TNManager.channels[0].id));
print(TNManager.GetChannel(88).host.name); //房主名字
print(TNManager.GetChannel(88).host.id); //房主id
print(TNManager.GetChannel(88).id); // 通道房间id
print(TNManager.GetChannel(88).created.size); //0
print(TNManager.GetChannel(88).players.size); //其它玩家的数量,自己除外
print(TNManager.players.size); //房间除去自己其它玩家的数量
print(TNManager.playerID); // 自己id
print(TNManager.playerName); // 自己的名字
print(TNManager.playerData); // null
txt.text = TNManager.GetChannel(88).host.name + " : " + TNManager.GetChannel(88).host.id + " : " + TNManager.GetChannel(88).rfcs + " :- " + TNManager.GetChannel(88).isPersistent + " :+ ";
}
}
}