Advertisement
HashZayed

MyServer

Oct 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 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 MyServer:MonoBehaviour {
  8.     int MAX_CONNECTION = 4;
  9.     string hostId="127.0.0.1";
  10.     int port = 5708;
  11.     private GameObject infoDisplayText;
  12.  
  13.     // Use this for initialization
  14.     void Start () {
  15.         NetworkManager.singleton.networkAddress = hostId;
  16.         NetworkManager.singleton.networkPort = port;
  17.         NetworkManager.singleton.StartHost ();
  18.         NetworkServer.RegisterHandler(AnimalDataMsgType.animalData, OnTextureReceive);
  19.         infoDisplayText = GameObject.Find("InfoDisplay");
  20.     }
  21.    
  22.     // Update is called once per frame
  23.     void Update () {
  24.        
  25.     }
  26.  
  27.     //Called when texture is received
  28.     public void OnTextureReceive(NetworkMessage netMsg)
  29.     {
  30.         AnimalData animalData = netMsg.ReadMessage<AnimalData>();
  31.  
  32.         string type = animalData.Type;
  33.         Debug.Log("Type : " + type);
  34.  
  35.         string id = animalData.Id;
  36.         Debug.Log("ID : " + id);
  37.  
  38.         int strength = animalData.Strength;
  39.         Debug.Log("Strength : " + strength);
  40.  
  41.         int hitpoints = animalData.Hitpoints;
  42.         Debug.Log("Hit Points : " + hitpoints);
  43.  
  44.  
  45.         //Your Received Texture2D
  46.         Texture2D receivedtexture = new Texture2D(64, 64, TextureFormat.DXT5, false);
  47.         receivedtexture.LoadRawTextureData(animalData.Tex);
  48.         receivedtexture.Apply();
  49.  
  50.         Debug.Log(type + " data received!");
  51.         infoDisplayText.GetComponent<Text>().text += type + " data received!\n";
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement