Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Quen · Oct 28, 2015 at 12:55 PM · unity 5networkvariables

How to send simple values on local network?

Hi everyone,

I want to send a few values between two Unity 5.2 applications in a local network. All tutorials I found are about a complete multiplayer setup. All I want to do is updating two values (one Vector3 and one float) constantly from one application to the other one. There is no need for matchmaking, player spawning, and all that stuff. Even the IPs could be hardcoded. Is there any "slim" way to do this, or do I have to set up a complete multiplayer project just for my two values? Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Quen · Oct 29, 2015 at 11:28 AM

After some more research, I was able to solve this by myself. Here is what I did, I think it is very slim and it works well:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Networking;
 
 public class MyNetworkManager : MonoBehaviour
 {
 
     public bool isAtStartup = true;
 
     NetworkClient myClient;
     public const short MyDataMsgID = 815;
 
     public bool isServer = false;
     public bool isClient = false;
 
 
     class MyMsgType
     {
         public static short ID = 815;
     }
 
     class MyDataMsg : MessageBase
     {
         public Vector3 position;
         public float angle;
         public string message;
     }
 
 
     void Update()
     {
         if (isAtStartup)
         {
             if (Input.GetKeyDown(KeyCode.Alpha1))
             {
                 // Now this is the SERVER
                 SetupServer();
             }
 
             if (Input.GetKeyDown(KeyCode.Alpha2))
             {
                 // Now this is the CLIENT
                 SetupClient();
             }
 
             if (Input.GetKeyDown(KeyCode.Alpha3))
             {
                 // Now this is a SERVER and a Local CLIENT too
                 SetupServer();
                 SetupLocalClient();
             }
         }
         else
         {
             if (isClient)
             {
                 // Maybe add conditions on when to send message...
                 SendMyDataMessage();
             }
         }
     }
 
     // Create a server and listen on a port
     public void SetupServer()
     {
         NetworkServer.Listen(4444);
         NetworkServer.RegisterHandler(MyMsgType.ID, OnRecievingData);
         isAtStartup = false;
         isServer = true;
         isClient = false;
     }
 
     // Create a client and connect to the server port
     public void SetupClient()
     {
         myClient = new NetworkClient();
         myClient.RegisterHandler(MsgType.Connect, OnConnected);
         myClient.Connect("127.0.0.1", 4444);
         isAtStartup = false;
         isClient = true;
         isServer = false;
     }
 
     // Create a local client and connect to the local server
     public void SetupLocalClient()
     {
         myClient = ClientScene.ConnectLocalServer();
         myClient.RegisterHandler(MsgType.Connect, OnConnected);
         isAtStartup = false;
     }
 
 
     // What the CLIENT should do when connected
     public void OnConnected(NetworkMessage netMsg)
     {
         Debug.Log("Connected to server");
     }
 
     // Call this with the CLIENT to send a message
     public void SendMyDataMessage()
     {
         var msg = new MyDataMsg();
         msg.position = new Vector3(0.0f,0.0f,0.0f);
         msg.angle = -21.74f;
         msg.message = "lets hope this comes through";
 
         myClient.Send(MyMsgType.ID, msg);
     }
 
     // What the SERVCER should do when getting a message
     public void OnRecievingData(NetworkMessage netMsg)
     {
         MyDataMsg myMsg = netMsg.ReadMessage<MyDataMsg>();
 
         // use the values from the message here
         Debug.Log("position: " + myMsg.position);
         Debug.Log("angle: " + myMsg.angle);
         Debug.Log("message: " + myMsg.message);
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i save a variable for only one scene? 1 Answer

Anyone using STOMP and SockJS on Unity? 1 Answer

How to create a object that can only be seen by one player? 0 Answers

How to get reference of the Local Game Player when a Game Scene is Loaded from a Lobby Scene [Multiplayer] 1 Answer

Creating and Accessing Unique Variables for Each Player on a Network 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges