private void SpawnGameObjects(object sender, MessageReceivedEventArgs e)
    {
        if (e.Tag == NetworkTags.InGame.SpawnObject)
        {
            SpawnMessage spawnMessage = e.GetMessage().Deserialize <SpawnMessage>();

            string resourcePath = NetworkObjectDictionary.GetResourcePath(spawnMessage.ResourceID);

            var ball = Instantiate(
                Resources.Load(resourcePath) as GameObject,
                new Vector3(spawnMessage.X, spawnMessage.Y, 0),
                Quaternion.identity)
                       .GetComponent <NetworkObject>();
            ball.ID = spawnMessage.ID;
        }
    }
    protected virtual void Start()
    {
        // If we are on the client and the ID is not set, destroy the object
        if (!GameServerManager.IsInitialised && ID == 0)
        {
            Destroy(gameObject);
        }
        else if (GameServerManager.IsInitialised)
        {
            ID = GetInstanceID();

            GameServerManager.Instance.RegisterNetworkObject(this);

            if (!NetworkObjectDictionary.HasResource(resourceId))
            {
                throw new System.Exception("No stored resource for ID: " + resourceId);
            }
        }
    }
Example #3
0
    ///<summary>
    /// Spawn object if message received is tagged as SPAWN_OBJECT
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void SpawnGameObjects(object sender, MessageReceivedEventArgs e)
    {
        if (e.Tag == NetworkTags.InGame.SPAWN_OBJECT)
        {
            // Get message data
            SpawnMessageModel spawnMessage =
                e.GetMessage().Deserialize <SpawnMessageModel>();

            // Spawn the game object
            string resourcePath =
                NetworkObjectDictionary.GetResourcePathFor(spawnMessage.resourceID);

            GameObject go = Resources.Load(resourcePath) as GameObject;

            go.GetComponent <NetworkObject>().id = spawnMessage.networkID;

            Debug.Log("--->> from CM: x position " + spawnMessage.x);
            Debug.Log("--->> from CM: y position " + spawnMessage.y);

            Instantiate(go, new Vector3(spawnMessage.x, spawnMessage.y, 0),
                        Quaternion.identity);
        }
    }