Advertisement
HashZayed

MyClient

Oct 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6.  
  7. public class MyClient : MonoBehaviour {
  8.     private string serverIP = "127.0.0.1";
  9.     private int port = 5708;
  10.     private NetworkClient client;
  11.     private bool isConnected = false;
  12.     private GameObject infoDisplayText;
  13.  
  14.     private string playerName="gg";
  15.     public Texture2D texToSend;
  16.     string typeToSend = "Deer";
  17.     string idToSend = "1";
  18.     int strengthToSend = 80;
  19.     int hitPointsToSend = 2;
  20.  
  21.     // Use this for initialization
  22.     void Start () {
  23.         NetworkManager.singleton.networkAddress = serverIP;
  24.         NetworkManager.singleton.networkPort = port;
  25.         client = NetworkManager.singleton.StartClient ();
  26.         infoDisplayText = GameObject.Find("InfoDisplay");
  27.     }
  28.  
  29.     void Update(){
  30.         if (NetworkManager.singleton.IsClientConnected () && !isConnected) {
  31.             Debug.Log("Connected to server");
  32.             infoDisplayText.GetComponent<Text>().text += playerName + " connected.\n";
  33.             isConnected = true;
  34.         }
  35.     }
  36.  
  37.     public void SendOnButtonPress()
  38.     {
  39.         if (isConnected == true)
  40.             SendTexture(texToSend, typeToSend, idToSend, strengthToSend, hitPointsToSend);
  41.     }
  42.  
  43.     //Call to send the Texture and a simple string message
  44.     public void SendTexture(Texture2D tex, string type, string id, int strength, int hitpoints)
  45.     {
  46.         AnimalData animalData = new AnimalData();
  47.  
  48.         animalData.Tex = tex.GetRawTextureData();
  49.         animalData.Type = type;
  50.         animalData.Id = id;
  51.         animalData.Strength = strength;
  52.         animalData.Hitpoints = hitpoints;
  53.         //if image size is too large, you'll get buffer is too large error
  54.         //in that case, you'll need to send data chunk by chunk.
  55.         //You can look up here to do that:
  56.         //http://answers.unity3d.com/questions/1113376/unet-send-big-amount-of-data-over-network-how-to-s.html
  57.         Debug.Log(client.Send(AnimalDataMsgType.animalData, animalData));
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement