I have some problems with my Script
- Code: Select all
sing System.Collections;
using System.Collections.Generic;
using PlayerIOClient;
using gui = UnityEngine.GUILayout;
public class ChatEntry {
public string text = "";
public bool mine = true;
}
public class GameManager : MonoBehaviour {
public GUISkin customSkin;
public bool LoginWindow = true; //View Login Window Box.0
public bool ConnectToRoom = false; //Login & Connect to Multiplayer Server.
public bool RegisterWindow = false; //View Register Window Box.
public bool showChatBox = true;
public bool showChatShip = false;
public GameObject target;
public GameObject PlayerPrefab;
public GameObject ToadPrefab;
private Connection pioconnection;
private List<PlayerIOClient.Message> msgList = new List<PlayerIOClient.Message>(); // Messsage queue implementation
public bool joinedroom = false;
string username = "";
string password = "";
string email = "";
// UI stuff
private Vector2 scrollPosition;
private ArrayList entries = new ArrayList();
private string inputField = "";
private Rect window = new Rect(0, 398, 300, 200);
private Rect window_login = new Rect(Screen.width/2-150,Screen.height/2-65, 300, 130);
private Rect window_register = new Rect(Screen.width/2-150,Screen.height/2-100, 300, 150);
private int toadspicked = 0;
private string infomsg = "";
private string gameId = "...-galac....-....-wweydzylw0.......";
void StartClient() {
// Required to setup the Player.IO Client in Unity3D
PlayerIOClient.PlayerIO.UnityInit(this);
Debug.Log("Starting");
/* -------------------------------------------------------
//Connect with a SimpleUser
PlayerIO.QuickConnect.SimpleConnect(
gameId,
username,
password,
delegate(Client client) {
------------------------------------------------------------
//Connect with a SimpleUser
PlayerIOClient.PlayerIO.Connect(
gameId, // Game id (Get your own at playerio.com. 1: Create user, 2:Goto admin pannel, 3:Create game, 4: Copy game id inside the "")
"public", // The id of the connection, as given in the settings section of the admin panel. By default, a connection with id='public' is created on all games.
username, // The id of the user connecting. This can be any string you like. For instance, it might be "fb10239" if you´re building a Facebook app and the user connecting has id 10239
null, // If the connection identified by the connection id only accepts authenticated requests, the auth value generated based on UserId is added here
null,
delegate(Client client) {
-------------------------------------------------------------
*/
//Connect with a SimpleUser
PlayerIO.QuickConnect.SimpleConnect(
gameId,
username,
password,
delegate(Client client) {
Debug.Log("Successfully connected to Player.IO");
infomsg = "Successfully connected to Player.IO";
LoginWindow = false;
target.transform.Find("NameTag").GetComponent<TextMesh>().text = username;
target.transform.name = username;
// Uncoment the line below to use the Development Server
// client.Multiplayer.DevServer = new ServerEndpoint("localhost",8184);
//Create or join the room
client.Multiplayer.CreateJoinRoom(
"UnityDemoRoom", //Room id. If set to null a random roomid is used
"UnityMushrooms", //The room type started on the server
true, //Should the room be visible in the lobby?
null,
null,
delegate(Connection connection) {
Debug.Log("Joined Room.");
infomsg = "Joined Room.";
// We successfully joined a room so set up the message handler
pioconnection = connection;
pioconnection.OnMessage += handlemessage;
joinedroom = true;
},
delegate(PlayerIOError error) {
Debug.Log("Error Joining Room: " + error.ToString());
infomsg = error.ToString();
}
);
},
delegate(PlayerIOError error) {
Debug.Log("Error connecting: " + error.ToString());
infomsg = error.ToString();
}
);
}
void handlemessage(object sender, PlayerIOClient.Message m) {
msgList.Add(m);
}
void FixedUpdate() {
// process message queue
while( msgList.Count>0 ) {
PlayerIOClient.Message m = msgList[0];
msgList.RemoveAt(0);
switch(m.Type) {
case "PlayerJoined":
GameObject newplayer = GameObject.Instantiate(target) as GameObject;
newplayer.transform.position = new Vector3(m.GetFloat(1), 0, m.GetFloat(2));
newplayer.name = m.GetString(0);
newplayer.transform.Find("NameTag").GetComponent<TextMesh>().text = m.GetString(0);
break;
case "Move":
GameObject upplayer = GameObject.Find(m.GetString(0));
upplayer.transform.LookAt(new Vector3(m.GetFloat(1), 0, m.GetFloat(2)));
// set transform x axis to 0, so the character will be facing forward
upplayer.transform.eulerAngles = new Vector3(0, upplayer.transform.eulerAngles.y, upplayer.transform.eulerAngles.z);
// get distance between current position and target position,
// we'll need to value to know how much the tween will last
float dist = Vector3.Distance(upplayer.transform.position, new Vector3(m.GetFloat(1), 0, m.GetFloat(2)));
// create a tween between current and target position
iTween.MoveTo(upplayer, iTween.Hash("x", m.GetFloat(1), "z", m.GetFloat(2), "onstart", "startwalk", "oncomplete", "stopwalk", "time", dist, "delay", 0, "easetype", iTween.EaseType.linear));
break;
case "Harvest":
GameObject hvplayer = GameObject.Find(m.GetString(0));
hvplayer.transform.LookAt(new Vector3(m.GetFloat(1), .5f, m.GetFloat(2)));
// set transform x axis to 0, so the character will be facing forward
hvplayer.transform.eulerAngles = new Vector3(0, hvplayer.transform.eulerAngles.y, hvplayer.transform.eulerAngles.z);
// get distance between current position and target position,
// we'll need to value to know how much the tween will last
float distance = Vector3.Distance(hvplayer.transform.position, new Vector3(m.GetFloat(1), 0, m.GetFloat(2)));
// create a tween between current and target position
iTween.MoveTo(hvplayer, iTween.Hash("x", m.GetFloat(1), "z", m.GetFloat(2), "onstart", "startwalk", "oncomplete", "stopharvest", "time", distance, "delay", 0, "easetype", iTween.EaseType.linear));
break;
case "Picked":
// remove the object when it's picked up
GameObject removetoad = GameObject.Find("Toad" + m.GetInt(0));
Destroy(removetoad);
break;
case "Chat"://if (showChatShip) {
if(m.GetString(0) != "Server") {
GameObject chatplayer = GameObject.Find(m.GetString(0));
chatplayer.transform.Find("Chat").GetComponent<TextMesh>().text = m.GetString(1);
chatplayer.transform.Find("Chat").GetComponent<MeshRenderer>().material.color = Color.white;
chatplayer.transform.Find("Chat").GetComponent<chatclear>().lastupdate = Time.time;
}
ChatText(m.GetString(0) + ": " + m.GetString(1), false);
break;
case "PlayerLeft":
// remove characters from the scene when they leave
GameObject playerd = GameObject.Find(m.GetString(0));
Destroy(playerd);
break;
case "Toad":
// adds a toadstool to the scene
GameObject newtoad = GameObject.Instantiate(ToadPrefab) as GameObject;
newtoad.transform.position = new Vector3(m.GetFloat(1), 0.1f, m.GetFloat(2));
newtoad.name = "Toad" + m.GetInt(0);
break;
case "ToadCount":
// updates how many toads have been picked up by the player
toadspicked = m.GetInt(0);
break;
}
}
// clear message queue after it's been processed
msgList.Clear();
}
void OnMouseDown() { //OnMouseDrag
// this function responds to mouse clicks on the ground
// it will send a move request to the server
// ignore user input if we're not inside a room
if(!joinedroom)
return;
Vector3 targetPosition = new Vector3(0, 0, 0);
var playerPlane = new Plane(Vector3.up, target.transform.position);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hitdist = 0.0f;
if(playerPlane.Raycast(ray, out hitdist)) {
targetPosition = ray.GetPoint(hitdist);
pioconnection.Send("Move", targetPosition.x, targetPosition.z);
}
}
void OnGUI() {
GUI.skin = customSkin;
if (LoginWindow) {
window_login = GUI.Window(2, window_login, LoginWindowBox, "Login"); //View Login Window Box.
}
if (RegisterWindow) {
window_register = GUI.Window(3, window_register, RegisterWindowBox, "Register"); //View Register Window Box.
}
if (GUI.Button(new Rect(25-25,Screen.height-0-200, 25, 26), " ", "Button_OpenChat"))
{
showChatBox = true;
}
if (showChatBox) {
window = GUI.Window(10, window, GlobalChatWindow, "Chat");
}
GUI.Label(new Rect(1, 20, 150, 20), "Toadstools picked: " + toadspicked);
if(infomsg != "") {
GUI.Label(new Rect(1, 1, Screen.width, 20), infomsg);
}
}
void LoginWindowBox(int id)
{
if (Screen.lockCursor) return;
gui.Label("Username:");
username = GUI.TextField(new Rect(70, 30, 300, 20), username, 25);
gui.Label("Password:");
password = GUI.PasswordField(new Rect(70, 54, 300, 20), password, "*"[0], 25);
gui.BeginHorizontal();
if (gui.Button("Login")) {
StartClient();
}
if (gui.Button("Register")) {
RegisterWindow = true;
LoginWindow = false;
}
gui.EndHorizontal();
}
void RegisterWindowBox(int id)
{
if (Screen.lockCursor) return;
gui.Label("Username:");
username = GUI.TextField(new Rect(70, 30, 300, 20), username, 25);
gui.Label("Password:");
password = GUI.PasswordField(new Rect(70, 54, 300, 20), password, "*"[0], 25);
gui.Label("E-Mail:");
email = GUI.TextField(new Rect(70, 78, 300, 20), email, 25);
gui.BeginHorizontal();
if (gui.Button("<- Back")) {
RegisterWindow = false;
LoginWindow = true;
}
if (gui.Button("Register & Join ->")) {
//Register a SimpleUser
PlayerIO.QuickConnect.SimpleRegister(
gameId,
username,
password,
email,
null, //Optional captchakey
null, //Optional captchavalue
null, //Optional extra data
null, //Optional partner id
//Here's a client, do something with the client.
delegate(Client register) {
Debug.Log("Account Successfully Created: " + register);
Debug.Log("Account Successfully Created");
RegisterWindow = false;
LoginWindow = true;
},
delegate(PlayerIORegistrationError errorregister) {
Debug.Log("Error Register: " + errorregister);
Debug.Log("Error Register Account");
}
);
}
gui.EndHorizontal();
}
public void HarvestAt(float posx, float posz) {
pioconnection.Send("MoveHarvest", posx, posz);
}
public void TryPickup(string id) {
pioconnection.Send("Pickup", id);
}
void GlobalChatWindow(int id) {
//if (GUI.Button(new Rect(0, 400, 25, 26), showChatShip ? "" : " ", "Button_Toggle"))
//if (GUI.Toggle(new Rect(25, 3, 30, 20), showChatShip, "Toggle"))
//{
// Chat up the Ship
// if (showChatShip)
// {
// showChatShip = false;
// }
// else
// {
// showChatShip = true;
// }
//}
if (GUI.Button(new Rect(1, 3, 20, 21), showChatBox ? "" : " ", "Button_CloseChat"))
{
// Focus first element
if (showChatBox)
{
showChatBox = false;
}
}
if(!joinedroom)
return;
GUI.FocusControl("Chat input field");
// Begin a scroll view. All rects are calculated automatically -
// it will use up any available screen space and make sure contents flow correctly.
// This is kept small with the last two parameters to force scrollbars to appear.
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
foreach(ChatEntry entry in entries) {
GUILayout.BeginHorizontal();
if(!entry.mine) {
GUILayout.Label(entry.text);
} else {
GUI.contentColor = Color.white;
GUILayout.Label(entry.text);
GUI.contentColor = Color.white;
}
GUILayout.EndHorizontal();
GUILayout.Space(3);
}
// End the scrollview we began above.
GUILayout.EndScrollView();
if(Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.Return && inputField.Length > 0) {
GameObject chatplayer = GameObject.Find(target.transform.name);
chatplayer.transform.Find("Chat").GetComponent<TextMesh>().text = inputField;
chatplayer.transform.Find("Chat").GetComponent<MeshRenderer>().material.color = Color.white;
chatplayer.transform.Find("Chat").GetComponent<chatclear>().lastupdate = Time.time;
ChatText(target.transform.name + ": " + inputField, true);
pioconnection.Send("Chat", inputField);
inputField = "";
}
GUI.SetNextControlName("Chat input field");
inputField = GUILayout.TextField(inputField);
GUI.DragWindow();
}
void ChatText(string str, bool own) {
var entry = new ChatEntry();
entry.text = str;
entry.mine = own;
entries.Add(entry);
if(entries.Count > 50)
entries.RemoveAt(0);
scrollPosition.y = 1000000;
}
}
The Console say the Error(s) is betwen 134 to 164.
What can i do to fix this Bug?
I hope you can help
Thanks,
X-GalacticaOnline
*Here one Picture from the Error Console
http://s14.directupload.net/images/120424/xobozybf.png
Link because the Picture is to large for this forum