Search Unity

How to connect two applications

Discussion in 'Multiplayer' started by imDanOush, Oct 14, 2016.

  1. imDanOush

    imDanOush

    Joined:
    Oct 12, 2013
    Posts:
    368
    Hello.

    I wanted to change value of a variable of my PC game project with my phone, have a look at the image below.
    Please note that the Boolean variable which is explained in the above image, belongs to the PC game, And the phone app only accesses it (and modifies it) via a connection.
    Is it possible to be done via UNet? If yes, would you please give me some clue about it, If no, does Photon or TNet or any other unity asset do so? Thanks in advance.
     
    Yhugo98 likes this.
  2. imDanOush

    imDanOush

    Joined:
    Oct 12, 2013
    Posts:
    368
    So after some research, I've found out that UNet can do it. And it was really easy.
    All you have to do is to use one same script for the two and treat them as if they were used in one scene of a project.
     
  3. AOBRI

    AOBRI

    Joined:
    Jan 7, 2017
    Posts:
    1
    Hi There :),

    I'm glad you managed to solve it I am facing a similar issue and would like to ask for more details.

    What do you mean by one same script ?
    does that script contain any special code? or how to make unity understand that ?
     
  4. imDanOush

    imDanOush

    Joined:
    Oct 12, 2013
    Posts:
    368
    Hello.
    I'll try to explain it with the script I've used. I'll also show you the project in a youtube video to see what is actually happening. The script and the project are the exact ones I've talked about here.

    So let's begin.

    Instruction:
    We have two scenes, one with only a UI canvas and a button and the other is the actual game scene with cars and the stuff. Let's call the first scene, Scene A and the other one Scene B. Those scenes both use one script for their objects.
    I've used it for a self-made autonomous car and its remote control. I've used this project for my AI class and it worked!.

    This is the script*:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5. using System;
    6.  
    7. public class NavmAgent : NetworkBehaviour
    8. {
    9.     public UnityEngine.UI.Toggle tgl; //This UI element is used for the "remote scene" which a user will control an event
    10.  
    11.     [SyncVar(hook = "UpdatingGo")]    public bool go = false; // The boolean var "go" is a "SyncVar**" that is "Hooked***"
    12.  
    13.     public Transform target;
    14.     bool doIt = false;
    15.  
    16.     void UpdatingGo(bool _go)
    17.     {
    18.         Debug.Log("The user from the scene A changed behavior of object in the scene B");
    19.  
    20.         //Do whatever you want the game do when the toggle from the other scene is changed by a user
    21.     }
    22.  
    23.     public void onClick() //This function is used when the toggle UI element value is changed
    24.     {
    25.         go = tgl.isOn; ;
    26.         Debug.Log("Sent new Go value");
    27.     }
    28. }
    29.  
    You can use one scene A per one scene B.Although It may be possible to use one Scene A for more than scene B in a network.

    In order to fully understand what is going on as a result, have a look at the project in real-time in the video below. The Scene B is shown on the left side of the video and Scene A is on the right side.
    Notice how as soon as I hit "Come here" toggle UI element, the car starts going to where I virtually am.
    *The script is simplified.
    **A SyncVar variable is always synced between Unity Builds (your game project) in a multiplayer session.
    *** "Hooked" indicates that if a "hooked" var (here, the boolean one) changed, fire an event (here, the function starts the "UpdatingGo(bool _go)" function).

    NOTE: I should have used get; and set; and...so I don't condone anyone to write a code without specific standards (unless you're lazy for that :D ).
     
    Yhugo98 likes this.
  5. jackvob1

    jackvob1

    Joined:
    Mar 2, 2018
    Posts:
    38
    i'm still confused have u got any more example simple one like just changing Scene ? ( Apps B scene from 0 to 1 by using Apps A )
     
  6. imDanOush

    imDanOush

    Joined:
    Oct 12, 2013
    Posts:
    368
    Unfortunately I don't have, I suggest reading the official doc (https://docs.unity3d.com/Manual/UNetGettingStarted.html) it is written pretty well.

    NOTICE: The Unity's multiplayer feature (UNet) will be changing to something else, I recommend not to use UNet at all and try to learn the new system instead. More info is available here: https://support.unity3d.com/hc/en-us/articles/360001252086-UNet-Deprecation-FAQ .
    Regards.
     
  7. Andy3D

    Andy3D

    Joined:
    Jan 31, 2015
    Posts:
    42
    I want to achieve this too. I need a unity app on an android tablet to update a unity app on android VR headset. No external multiplayer is needed. Just wondering whats the best way to do this in 2019, given Unity's dropping of support for the solution mentioned here.
     
    baptistou83 likes this.