Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- public class MyClient : MonoBehaviour {
- private string serverIP = "127.0.0.1";
- private int port = 5708;
- private NetworkClient client;
- private bool isConnected = false;
- private GameObject infoDisplayText;
- private string playerName="gg";
- public Texture2D texToSend;
- string typeToSend = "Deer";
- string idToSend = "1";
- int strengthToSend = 80;
- int hitPointsToSend = 2;
- // Use this for initialization
- void Start () {
- NetworkManager.singleton.networkAddress = serverIP;
- NetworkManager.singleton.networkPort = port;
- client = NetworkManager.singleton.StartClient ();
- infoDisplayText = GameObject.Find("InfoDisplay");
- }
- void Update(){
- if (NetworkManager.singleton.IsClientConnected () && !isConnected) {
- Debug.Log("Connected to server");
- infoDisplayText.GetComponent<Text>().text += playerName + " connected.\n";
- isConnected = true;
- }
- }
- public void SendOnButtonPress()
- {
- if (isConnected == true)
- SendTexture(texToSend, typeToSend, idToSend, strengthToSend, hitPointsToSend);
- }
- //Call to send the Texture and a simple string message
- public void SendTexture(Texture2D tex, string type, string id, int strength, int hitpoints)
- {
- AnimalData animalData = new AnimalData();
- animalData.Tex = tex.GetRawTextureData();
- animalData.Type = type;
- animalData.Id = id;
- animalData.Strength = strength;
- animalData.Hitpoints = hitpoints;
- //if image size is too large, you'll get buffer is too large error
- //in that case, you'll need to send data chunk by chunk.
- //You can look up here to do that:
- //http://answers.unity3d.com/questions/1113376/unet-send-big-amount-of-data-over-network-how-to-s.html
- Debug.Log(client.Send(AnimalDataMsgType.animalData, animalData));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement