private void RegisterCustomSpawners()
 {
     foreach (NetworkHash128 assetId in _assetIdToPrefab.Keys)
     {
         ClientScene.RegisterSpawnHandler(assetId, Spawn, UnSpawn);
     }
 }
示例#2
0
    void Awake()
    {
        if (instance != null)
        {
            Destroy(instance);
        }
        instance = this;

        enemyIds = new System.Guid[enemies.Length];
        prefabs  = new Dictionary <System.Guid, GameObject>();
        for (int i = 0; i < enemies.Length; i++)
        {
            System.Guid assetId = enemies[i].GetComponent <NetworkIdentity>().assetId;
            enemyIds[i] = assetId;
            prefabs.Add(assetId, enemies[i]);
            ClientScene.RegisterSpawnHandler(assetId, SpawnObject, UnSpawnObject);
        }

        itemIds = new System.Guid[items.Length];
        for (int i = 0; i < items.Length; i++)
        {
            System.Guid assetId = items[i].GetComponent <NetworkIdentity>().assetId;
            itemIds[i] = assetId;
            prefabs.Add(assetId, items[i]);
            ClientScene.RegisterSpawnHandler(assetId, SpawnObject, UnSpawnObject);
        }
    }
示例#3
0
    private void Awake()
    {
        instance = this;

        // We create a new dictionary
        poolDictionary = new Dictionary <string, Queue <GameObject> >();

        foreach (objectPoolItem item in itemsToPool)
        {
            // We create a gameObject queue for each key of the dictionary
            Queue <GameObject> objectPool = new Queue <GameObject>();

            // We add the objects to the pools
            for (int i = 0; i < item.amountToPool; i++)
            {
                GameObject go = Instantiate(item.objectToPool);
                go.SetActive(false);
                objectPool.Enqueue(go);
            }

            // We add the pool to the dictionary
            poolDictionary.Add(item.tag, objectPool);

            ClientScene.RegisterSpawnHandler(assetId, SpawnObject, UnSpawnObject);
        }
    }
示例#4
0
 public void SetSpawnHandler()
 {
     foreach (NetworkObject element in objectPool)
     {
         ClientScene.RegisterSpawnHandler(element.Id, SpawnObject, UnSpawnObject);
     }
 }
    public IEnumerator CommandsAndRPCCallsWorkTest()
    {
        NetworkClient.ShutdownAll();
        NetworkServer.Reset();

        ConnectionConfig config = new ConnectionConfig();

        config.AddChannel(QosType.ReliableSequenced);// this test requires correct sequence of packets.
        config.AddChannel(QosType.Unreliable);

        myClient = new NetworkClient();
        if (!myClient.Configure(config, 10))
        {
            Assert.Fail("Client configure failed");
        }

        NetworkServer.RegisterHandler(MsgType.AddPlayer, OnAddPlayer);
        myClient.RegisterHandler(MsgType.Connect, OnClientConnected);
        myClient.RegisterHandler(MsgType.Error, OnClientError);
        ClientScene.RegisterSpawnHandler(playerHash, OnSpawnPlayer, OnUnSpawnPlayer);

        int retries = 0;

        while (!NetworkServer.Listen("127.0.0.1", ++kListenPort))
        {
            Assert.IsTrue(retries++ < 10, "Couldn't Listen for more than 10 retries");
        }

        myClient.Connect("127.0.0.1", kListenPort);

        while (!isTestDone)
        {
            yield return(null);
        }
    }
示例#6
0
    public void Init()
    {
        assetId = m_Prefab.GetComponent <NetworkIdentity>().assetId;

        for (int i = 0; i < poolSize; ++i)
        {
            GameObject obj = (GameObject)Instantiate(m_Prefab);
            obj.transform.parent = this.transform;
            obj.name             = m_Prefab.name;
            m_Pool.Add(obj);
            obj.SetActive(false);
        }

        if (m_Prefab.GetComponent <Unit>() != null)
        {
            if (m_Prefab.GetComponent <Unit>().unitData != null)
            {
                foreach (GameObject go in m_Prefab.GetComponent <Unit>().unitData.objectToPool)
                {
                    if (PoolManager.Instance.poolDictionnary[go.name] != null)
                    {
                        Debug.Log("poolDictionary[agent] != null");

                        PoolManager.Instance.poolDictionnary[go.name].poolSize = 23;
                        PoolManager.Instance.poolDictionnary[go.name].Init();
                    }
                }
            }
        }
        ClientScene.RegisterSpawnHandler(assetId, SpawnObject, UnSpawnObject);
    }
示例#7
0
        /// <summary>
        /// Registers all of the spawnable prefabs to the ClientScene handler. This will allow the objects to be pooled on the client in an network game.
        /// </summary>
        private void Awake()
        {
            // Register all of the spawnable prefabs with the ClientScene. This will use ClientSpawnHandler and ClientUnspawnHandler so the objects can be pooled
            // even in an network game. Note that these objects cannot also be registered within NetworkManager.RegisteredSpawnablePrefabs otherwise
            // the handlers will not be called.
            m_AssetIDPrefabMap.Clear();
            for (int i = 0; i < m_SpawnablePrefabs.Count; ++i)
            {
                var networkID = m_SpawnablePrefabs[i].GetComponent <NetworkIdentity>();
                if (networkID != null)
                {
                    ClientScene.RegisterSpawnHandler(networkID.assetId, ClientSpawnHandler, ClientUnspawnHandler);
                    m_AssetIDPrefabMap.Add(networkID.assetId, m_SpawnablePrefabs[i]);
#if UNITY_EDITOR || DLL_RELEASE
                    // Quick sanity check within the editor only.
                    var registeredSpawnablePrefabs = NetworkManager.singleton.spawnPrefabs;
                    for (int j = 0; j < registeredSpawnablePrefabs.Count; ++j)
                    {
                        if (registeredSpawnablePrefabs[j].Equals(m_SpawnablePrefabs[i]))
                        {
                            Debug.LogWarning("Warning: " + m_SpawnablePrefabs[i] + " is added to both the ObjectPool and NetworkManager spawnable prefabs. Ensure it is " +
                                             "only added in one location.");
                            continue;
                        }
                    }
#endif
                }
            }
        }
示例#8
0
 void RegisterSpawnHandlers()
 {
     //actors
     ClientScene.RegisterSpawnHandler(gnomeAssetId, SpawnActor, UnSpawnActor);
     ClientScene.RegisterSpawnHandler(bootyBoxAssetId, SpawnActor, UnSpawnActor);
     ClientScene.RegisterSpawnHandler(bootyAssetId, SpawnActor, UnSpawnActor);
     ClientScene.RegisterSpawnHandler(bombAssetId, SpawnActor, UnSpawnActor);
 }
    public NetworkSpawnPerformanceTest(NetworkIdentity prefab)
    {
        spawnState = SpawnState.Spawn;

        objectInstance = Object.Instantiate(prefab.gameObject);
        objectInstance.SetActive(false);
        ClientScene.RegisterSpawnHandler(prefab.assetId, SpawnHandler, UnSpawnHandler);
    }
示例#10
0
 private void RegisterCustomSpawners()
 {
     foreach (NetworkHash128 assetId in assetIdToPrefab.Keys)
     {
         Debug.Log("Register assetId " + assetId.ToString());
         ClientScene.RegisterSpawnHandler(assetId, Spawn, UnSpawn);
     }
 }
示例#11
0
    public void onAssetMsg(NetworkMessage netMsg)
    {
        Debug.Log("Received assetId");
        AssetsMessage msg = netMsg.ReadMessage <AssetsMessage>();

        ClientScene.RegisterSpawnHandler(msg.assetId, SpawnSphere, UnspawnSphere);
        ClientScene.Ready(netMsg.conn);
    }
示例#12
0
    public GameObject SpawnObject(Vector3 position, NetworkHash128 assetId)
    {
        GameObject serverObj = m_prefabs[assetId];
        GameObject obj       = InstantiatePool(serverObj.name + "Client", position, Quaternion.identity);

        ClientScene.RegisterSpawnHandler(obj.GetComponent <NetworkIdentity>().assetId, SpawnObject, DestroyPool);
        return(obj);
    }
示例#13
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        //  Registers an asset on the clientside, given by the assetID.
        /// </summary>
        //  <param name="assetID"></param>
        private void RegisterAsset(NetworkHash128 assetID)
        {
            if (NetworkServer.active)
            {
                return;
            }

            ClientScene.RegisterSpawnHandler(_AssetID, ClientSpawn, ClientDespawn);
        }
示例#14
0
    public GameObject AddPools(Vector3 v)
    {
        assetId = unitPrefab.GetComponent <NetworkIdentity>().assetId;
        GameObject worm = (GameObject)MonoBehaviour.Instantiate(unitPrefab, v, Quaternion.identity);

        waypoints.Insert(0, worm.transform);
        ClientScene.RegisterSpawnHandler(assetId, SpawnObject, UnSpawnObject);
        return(worm);
    }
示例#15
0
 // Use this for initialization
 void Start()
 {
     //if (isServer)
     {
         Init();
         NetworkHash128 cueAssetId = cue.GetComponent <NetworkIdentity>().assetId;
         ClientScene.RegisterSpawnHandler(cueAssetId, SpawnStick, UnspawnStick);
     }
 }
示例#16
0
    private void InitialiseSpawnHandlers()
    {
        NetworkIdentity netID = objectToPool.GetComponent <NetworkIdentity> ();

        if (netID != null)
        {
            NetworkHash128 assetId = netID.assetId;
            ClientScene.RegisterSpawnHandler(assetId, Spawn, UnSpawn);
        }
    }
        public void Register()
        {
            var hash128 = NetworkHash128.Parse(this.SpawnId);

            ClientScene.RegisterSpawnHandler(
                hash128,
                this.OnClientSpawn,
                this.OnClientUnSpawn
                );
        }
示例#18
0
    public void Init()
    {
        assetId = m_Prefab.GetComponent <NetworkIdentity>().assetId;

        ClientScene.RegisterSpawnHandler(assetId, SpawnObject, UnSpawnObject);

        if (isServer)
        {
            CmdInit();
        }
    }
        /// <summary>
        /// Register a spawn handler on the client
        /// </summary>
        protected virtual void RegisterSpawnHandlerOnClient(SpawnHandlerData spawnData)
        {
            if (spawnData.serverPrefab == null || spawnData.clientSpawnDelegate == null || spawnData.clientUnSpawnDelegate == null)
            {
                Debug.LogWarning("SpawnData is missing needed data (spawn/unspawn delegate or server prefab)");
                return;
            }

            //Create a custom handler for the server prefab, so we can spawn a custom client prefab
            ClientScene.RegisterSpawnHandler(spawnData.serverPrefab.GetComponent <NetworkIdentity>().assetId, spawnData.clientSpawnDelegate, spawnData.clientUnSpawnDelegate);
        }
示例#20
0
    public ObjectPool(GameObject obj)
    {
        if (obj == null)
        {
            Debug.Log("ObjectPool initiated with a null object.");
        }

        sourceObject     = obj;
        sourceObjectHash = obj.GetComponent <NetworkIdentity>().assetId;

        ClientScene.RegisterSpawnHandler(sourceObjectHash, ClientSpawnHandler, ClientUnSpawnHandler);
    }
示例#21
0
    void Start()
    {
        assetId = m_Prefab.GetComponent <NetworkIdentity>().assetId;
        m_Pool  = new GameObject[m_ObjectPoolSize];
        for (int i = 0; i < m_ObjectPoolSize; ++i)
        {
            m_Pool[i]      = (GameObject)Instantiate(m_Prefab, Vector3.zero, Quaternion.identity);
            m_Pool[i].name = "PoolObject" + i;
            m_Pool[i].SetActive(false);
        }

        ClientScene.RegisterSpawnHandler(assetId, SpawnObject, UnSpawnObject);
    }
示例#22
0
    public IEnumerator PlayWithReadyStateTest()
    {
        NetworkClient.ShutdownAll();
        NetworkServer.Reset();

        GameObject nmObject = new GameObject();
        PlayWithReadyStateNetworkManager nmanager = nmObject.AddComponent <PlayWithReadyStateNetworkManager>();

        nmanager.networkAddress = "localhost";

        ClientScene.RegisterSpawnHandler(playerHash, PlayWithReadyStateNetworkManager.OnSpawnPlayer, PlayWithReadyStateNetworkManager.OnUnSpawnPlayer);

        NetworkServer.RegisterHandler(FromClientMsg1, OnServerFromClientMsg1);
        NetworkServer.RegisterHandler(FromClientMsg2, OnServerFromClientMsg2);
        NetworkServer.RegisterHandler(FromClientMsg3, OnServerFromClientMsg3);

        nmanager.StartServer();
        client1 = nmanager.StartClient();
        client1.RegisterHandler(MsgType.Connect, OnClient1Connect);
        client1.RegisterHandler(MsgId1, OnMsg1);
        client1.RegisterHandler(MsgId2, OnMsg2);
        client1.RegisterHandler(MsgId3, OnMsg3);

        // client2 is never ready, so should not recieve msgs
        client2 = new NetworkClient();
        client2.RegisterHandler(MsgType.Connect, OnClient2Connect);
        client2.RegisterHandler(MsgId1, OnMsg1);
        client2.RegisterHandler(MsgId2, OnMsg2);
        client2.RegisterHandler(MsgId3, OnMsg3);
        client2.RegisterHandler(MsgType.NotReady, OnNotReady);

        Assert.IsTrue(NetworkServer.active, "Server is not started");
        Assert.IsTrue(NetworkClient.active, "Client is not started");

        client2.Connect(NetworkManager.singleton.networkAddress, NetworkManager.singleton.networkPort);

        yield return(null);

        while (!isDone)
        {
            yield return(null);
        }

        CollectionAssert.AreEqual(resultListOfCallbacks, actualListOfCallbacks, "Wrong order of callbacks or some callback is missing");

        nmanager.StopServer();
        nmanager.StopClient();
        NetworkClient.ShutdownAll();
        UnityEngine.Object.Destroy(nmObject);
    }
    void Start()
    {
        _assetId = _bulletPrefab.GetComponent <NetworkIdentity> ().assetId;
        _pool    = new GameObject[OBJECT_POOL_SIZE];
        for (int i = 0; i < OBJECT_POOL_SIZE; ++i)
        {
            _pool [i]     = (GameObject)Instantiate(_bulletPrefab, Vector3.zero, Quaternion.identity);
            _pool[i].name = "BulletPoolObject" + i;
            _pool[i].SetActive(false);
            _pool [i].transform.parent = this.gameObject.transform;
        }

        ClientScene.RegisterSpawnHandler(_assetId, SpawnObject, UnSpawnObject);
    }
    public SpawnPool(GameObject prefab, int num, bool inMainGame)
    {
        _inMainGame = inMainGame;
        _pool       = new GameObject[num];
        for (int i = 0; i < num; i++)
        {
            _pool[i] = GameObject.Instantiate(prefab);
            _pool[i].SetActive(false);
            _pool[i].GetComponent <Powerups.PowerUpUnspawner>().MainGame = _inMainGame;
        }

        AssetId = prefab.GetComponent <NetworkIdentity>().assetId;
        ClientScene.RegisterSpawnHandler(AssetId, SpawnObject, UnSpawnObject);
    }
示例#25
0
 void RegisterSpawnHandler()
 {
     foreach (var n in typeof(EnvironmentObject).GetValue())
     {
         if (n == "None")
         {
             continue;
         }
         var prefab  = Resources.Load <GameObject>(n);
         var assetid = prefab.GetComponent <NetworkIdentity>().assetId;
         prefabs[assetid]     = prefab;
         assetidtoid[assetid] = n.Convert <EnvironmentObject>();
         ClientScene.RegisterSpawnHandler(assetid, new SpawnDelegate(SpawnHandler), new UnSpawnDelegate(UnSpawnHandler));
     }
 }
示例#26
0
    void Start()
    {
        //on client, this isn't required but is nice for testing.
        Application.runInBackground = true;

        var globals = FindObjectOfType <GlobalAssets>();

        _networkStateEntityProtoType = globals.NetworkEntityStatePrototype.GetComponent <NetworkIdentity>();

        ClientScene.RegisterSpawnHandler(_networkStateEntityProtoType.assetId, OnSpawnEntity, OnDespawnEntity);

        _client = new NetworkClient();
        _client.Connect("localhost", 7777);
        _client.RegisterHandler(MsgType.Connect, OnClientConnected);
    }
示例#27
0
    void Start()
    {
        ThrownStonesParent = GameObject.Find("GroupThrownStones");
        assetId            = stonePref.GetComponent <NetworkIdentity>().assetId;
        allStones          = new GameObject[poolSize];
        for (int i = 0; i < poolSize; ++i)
        {
            allStones[i]                  = (GameObject)Instantiate(stonePref, Vector3.zero, Quaternion.identity);
            allStones[i].name             = "stoneToThrow" + i;
            allStones[i].transform.parent = ThrownStonesParent.transform;
            allStones[i].SetActive(false);
        }

        ClientScene.RegisterSpawnHandler(assetId, spawnStone, unspawnStone);
    }
示例#28
0
        public override void Start()
        {
            base.Start();

            _cardStackAssetId = playController.cardStackPrefab.GetComponent <NetworkIdentity>().assetId;
            ClientScene.RegisterSpawnHandler(_cardStackAssetId, SpawnStack, UnSpawn);

            _cardAssetId = playController.cardModelPrefab.GetComponent <NetworkIdentity>().assetId;
            ClientScene.RegisterSpawnHandler(_cardAssetId, SpawnCard, UnSpawn);

            _dieAssetId = playController.diePrefab.GetComponent <NetworkIdentity>().assetId;
            ClientScene.RegisterSpawnHandler(_dieAssetId, SpawnDie, UnSpawn);

            Discovery  = GetComponent <CgsNetDiscovery>();
            ListServer = GetComponent <CgsNetListServer>();
            Debug.Log("[CgsNet Manager] Acquired Discovery and List Server.");
        }
示例#29
0
    public void RegisterPrefab(GameObject prefab)
    {
        NetworkIdentity networkIdentity = prefab.GetComponent <NetworkIdentity>();

        if (networkIdentity == null)
        {
            Debug.LogError("Registered Prefab doesnt contains Network Identity Component: " + prefab.name);
            return;
        }
        NetworkHash128 assetId = networkIdentity.assetId;

        if (!m_prefabs.ContainsKey(assetId))
        {
            m_prefabs.Add(assetId, prefab);
        }
        ClientScene.RegisterSpawnHandler(prefab.GetComponent <NetworkIdentity>().assetId, SpawnObject, DestroyPool);
    }
示例#30
0
    public void onAssetMsg(NetworkMessage netMsg)
    {
        Debug.Log("Received assetId");
        AssetsMessage msg = netMsg.ReadMessage <AssetsMessage>();

        foreach (NetworkHash128 hash in ClientScene.prefabs.Keys)
        {
            if (hash.Equals(prefab.assetId))
            {
                Debug.Log("Found match");
                return;
            }
        }
        Debug.Log("Registering spawn handler for prefab");
        ClientScene.RegisterSpawnHandler(msg.assetId, SpawnSphere, UnspawnSphere);
        Debug.Log("Readying client");
        ClientScene.Ready(netMsg.conn);
    }