GameObject GetPrefab(InstantiateType type)
    {
        switch (type)
        {
        case InstantiateType.Player:
            return(playerPrefab);

        default:
            throw new System.InvalidOperationException("Unknown InstantiateType " + type);
        }
    }
    void HandleInstantiate(byte[] data)
    {
        ByteReader byteReader = new ByteReader(data);

        byteReader.ReadByte();          // MessageType
        int             clientID     = byteReader.ReadInt();
        int             localID      = byteReader.ReadInt();
        InstantiateType instanceType = (InstantiateType)byteReader.ReadByte();
        Vector3         spawnPos     = byteReader.ReadVector3();
        Quaternion      spawnRot     = byteReader.ReadQuaternion();

        InstantiateFromNetwork(instanceType, clientID, localID, spawnPos, spawnRot);
    }
    void CreateNetworkObject(byte[] data)
    {
        ByteReader byteReader = new ByteReader(data);

        byteReader.ReadByte();
        int             clientID     = byteReader.ReadInt();
        int             localID      = byteReader.ReadInt();
        InstantiateType instanceType = (InstantiateType)byteReader.ReadByte();
        Vector3         position     = byteReader.ReadVector3();
        Quaternion      rotation     = byteReader.ReadQuaternion();

        Log(string.Format("Instantiate Object: {0} {1} {2}", instanceType, clientID, localID));
        InstantiateFromNetwork(instanceType, clientID, localID, position, rotation);
    }
Ejemplo n.º 4
0
 public CellGridView(
     InstantiateType Instantiate, int boardSize, GameObject blackCube, GameObject star)
 {
     this.Instantiate = Instantiate;
     this.boardSize   = boardSize;
     this.blackCube   = blackCube;
     groundSprites    = new List <List <GameObject> >();
     for (int y = 0; y < boardSize; y++)
     {
         groundSprites.Add(new List <GameObject>());
         for (int x = 0; x < boardSize; x++)
         {
             var sprite = Instantiate(star, boardPosTo3DPos(boardSize, x, y), Quaternion.identity);
             groundSprites[groundSprites.Count - 1].Add(sprite);
         }
     }
 }
    GameObject InstantiateFromNetwork(InstantiateType instantiateType, int clientID, int localID, Vector3 spawnPos, Quaternion spawnRot)
    {
        GameObject    instance      = GameObject.Instantiate(GetPrefab(instantiateType), spawnPos, spawnRot);
        NetworkObject networkObject = instance.GetComponent <NetworkObject>();

        if (networkObject == null)
        {
            throw new System.NullReferenceException("Object must have NetworkObject Component.");
        }

        networkObject.isLocal  = false;
        networkObject.clientID = clientID;
        networkObject.localID  = localID;

        m_NetworkObjects.Add(networkObject);

        return(instance);
    }
    public GameObject Instantiate(InstantiateType instantiateType, Vector3 spawnPos, Quaternion spawnRot)
    {
        // Byte Order
        // int localID
        // Vector3 instantiateType
        // Vecotr3 spawnPos
        // Quaternion spawnRot
        byte[]     sendingData = new byte[sizeof(int) + sizeof(byte) + sizeof(float) * 7];
        ByteWriter byteWriter  = new ByteWriter(sendingData);

        byteWriter.WriteInt(localIDCounter);
        byteWriter.WriteByte((byte)instantiateType);
        byteWriter.WriteVector3(spawnPos);
        byteWriter.WriteQuaternion(spawnRot);

        Log("Instantiating Object...");
        Log("Assigned LocalID: " + (localIDCounter));

        SendMessage(MessageType.Instantiate, sendingData);

        // Actual instantiate from Unity
        GameObject    instance      = GameObject.Instantiate(GetPrefab(instantiateType), spawnPos, spawnRot);
        NetworkObject networkObject = instance.GetComponent <NetworkObject>();

        if (networkObject == null)
        {
            throw new System.NullReferenceException("Object must have NetworkObject Component.");
        }

        networkObject.clientID = clientID;
        networkObject.isLocal  = true;
        networkObject.localID  = localIDCounter;

        m_NetworkObjects.Add(networkObject);

        // Increase LocalID Counter
        localIDCounter++;

        return(instance);
    }