1

Is it possible to have separated projects for both client and server in Unity3D by using UNet?

As far as I have seen it seems to be impossible, Unity3D official documentation is not much clear on that topic and have found zero examples or articles about that.

If you use the NetworkManager provided by Unity3D there is a lot of coupling of server side logic and game client logic, is it possible to separate this into two different projects? Server side should be just a headless server and client should be the normal game client without all the server logic. If it is possible, how should it looks like?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Alejandro Morán
  • 669
  • 1
  • 13
  • 36
  • 1
    Unity allows for dedicated server or one client hosting the game. It's really up to you how you want it to work. But I believe what you're looking for is the `NetworkServer` class. That has decoupled logic for the server. – Brandon Miller Jan 04 '18 at 20:43
  • @BrandonMiller Thanks, that seems to be a good starting point, seems to be what I was looking for. – Alejandro Morán Jan 04 '18 at 20:46
  • @BrandonMiller I'm facing a similar problem now, let's focus on Spawn method which receives a GameObject as a parameter, prefabs should be stored in the server and then sent to client? does the client needs to have access to that prefabs? – Alejandro Morán Jan 04 '18 at 23:52
  • No, prefabs should be stored in every client and when they receive the "spawn" message they should handle their own instantiation. Your goal is to put as little load on the server as possible. Use the resources of the clients PC to your advantage. – Brandon Miller Jan 05 '18 at 15:33
  • @BrandonMiller How is that possible? Spawn method signature receives a GameObject as first parameter, what should we send there? – Alejandro Morán Jan 06 '18 at 11:22

1 Answers1

0

Using Network class you can easily differentation between the server and Client.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void OnGUI() {
        if (Network.isServer)
            GUILayout.Label("Running as a server");
        else
            if (Network.isClient)
                GUILayout.Label("Running as a client");

    }
}

Or there are properties on the NetworkBehaviour class that allow scripts to know what the network context is of a networked object at any time.

  1. isServer - true if the object is on a server (or host) and has been spawned.
  2. isClient - true if the object is on a client, and was created by the server.
  3. isLocalPlayer - true if the object is a player object for this client.
  4. hasAuthority - true if the object is owned by the local process
derHugo
  • 83,094
  • 9
  • 75
  • 115
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • Thanks for your help but that does not answer my first question about having server and client in different projects, this way seems to be a little bit dirty in comparison with the mainstream techniques we have in other development areas in which code is well organized and separated. – Alejandro Morán Jan 15 '18 at 10:16
  • I guess UNET don't provide this kind of feature But you can make dedicated server. additionally, it is not messy or difficult. – Muhammad Faizan Khan Jan 15 '18 at 10:28