print("Connect click " + TNManager.isConnected); //true
//加入55号房间,场景名为haha,注意第三个参数false表示如果房间没有人了,那么这个房间也就不存在了
TNManager.JoinChannel(55, "haha", false, 255, null); //
//下面也是加入房间,但是加入房间号随机,
// TNManager.CreateChannel("haha", false, 255, null, true);
注意不管是JoinChannel还是CreateChannel,都是进入房间,跳场景,但是有时候跳转完场景后,可能还没进入房间,这样 需要在跳场景后,添加一个脚本,绑定加入房间事件,同时还需要绑定其它事件
void Start()
{
TNManager.onJoinChannel += this.joinChannel; //自己加入到这个房间
TNManager.onPlayerJoin += this.playerJoin; //检测有其他人加入这个房间
TNManager.onPlayerLeave += this.playerLeave; //检测有其他人离开这个房间
}
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("joinChannel : " + TNManager.isConnected); //true
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(channelID, "ColoredObject", "Created Cube", false, pos, rot, color, 3f);
}
print(TNManager.IsHosting(channelID));
if (TNManager.IsHosting(channelID))
{
TNManager.SetChannelData(channelID, "RoomName", "wangerxiaofangniu");
}
else
{
DataNode ddd = TNManager.GetChannelData(channelID, "RoomName"); //获取房间名
print(ddd.name + " : " + ddd.value);
//foreach (Channel.Info item in )
//{
// string tempRoomName = item.data.GetHierarchy<string>("RoomName");
//}
}
}
[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
}
}