Ejemplo n.º 1
0
    /// <summary>
    /// Instantiate a prefab over the network. This prefab needs to be located in the root of a "Resources" folder.
    /// </summary>
    /// <param name="prefab"></param>
    /// <param name="position"></param>
    /// <param name="rotation"></param>
    /// <param name="group"></param>
    /// <param name="data">optional instantiation data. This will be saved to it's PhotonView</param>
    /// <returns>The new instance.</returns>
    public static GameObject Instantiate(Object prefab, Vector3 position, Quaternion rotation, int group, object[] data)
    {
        if (!VerifyCanUseNetwork())
        {
            return(null);
        }

        GameObject prefabGo = (GameObject)Resources.Load(prefab.name, typeof(GameObject));

        if (prefabGo == null)
        {
            Debug.LogError("PhotonNetwork error: Could not Instantiate the prefab [" + prefab.name + "]. Please verify you have this gameobject in a Resources folder (and not in a subfolder)");
            return(null);
        }

        Component[]    views   = (Component[])prefabGo.GetComponentsInChildren <PhotonView>(true);
        PhotonViewID[] viewIDs = new PhotonViewID[views.Length];
        for (int i = 0; i < viewIDs.Length; i++)
        {
            viewIDs[i] = AllocateViewID();
        }

        // Send to others, create info
        Hashtable instantiateEvent = networkingPeer.SendInstantiate(prefab.name, position, rotation, group, viewIDs, data);

        // Instantiate the GO locally (but the same way as if it was done via event). This will also cache the instantiationId
        return(networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.mLocalActor, prefabGo));
    }
Ejemplo n.º 2
0
    private void Setup()
    {
        if (!Application.isPlaying)
        {
            return;
        }

        if (this.ranSetup)
        {
            return;
        }

        this.ranSetup = true;

        if (this.isSceneView)
        {
            bool result = PhotonNetwork.networkingPeer.PhotonViewSetup_FindMatchingRoot(gameObject);
            if (result)
            {
                // This instantiated prefab needs to be corrected as it's incorrectly reported as a sceneview.
                // It is wrongly reported as isSceneView because a scene-prefab changes have been applied to the project prefab
                // The scene's prefab viewID is therefore saved to the project prefab. This workaround fixes all problems
                this.sceneViewID = 0;
            }
            else
            {
                if (this.sceneViewID < 1)
                {
                    Debug.LogError("SceneView " + sceneViewID);
                }

                ID = new PhotonViewID(this.sceneViewID, null);
                this.registeredPhotonView = true;
                PhotonNetwork.networkingPeer.RegisterPhotonView(this);
            }
        }
        else
        {
            bool res = PhotonNetwork.networkingPeer.PhotonViewSetup_FindMatchingRoot(gameObject);
            if (!res)
            {
                if (PhotonNetwork.logLevel != PhotonLogLevel.ErrorsOnly)
                {
                    Debug.LogWarning("Warning: Did not find the root of a PhotonView. This is only OK if you used GameObject.Instantiate to instantiate this prefab. Object: " + this.name);
                }
            }
        }
    }
Ejemplo n.º 3
0
 //Start, since in Awake PhotonNetwork isn't set up properly.
 void Awake()
 {
     if (isSceneView)
     {
         if (sceneViewID < 1)
         {
             Debug.LogError("SceneView " + sceneViewID);
         }
         ID = new PhotonViewID(sceneViewID, null);
         registeredPhotonView = true;
         PhotonNetwork.networkingPeer.RegisterPhotonView(this);
     }
     else
     {
         ID = new PhotonViewID(0, null);
     }
 }
Ejemplo n.º 4
0
    void Start()
    // Verify setup, configure
    {
        photonView   = PhotonView.Get(this);
        photonViewID = photonView.viewID;


        Setup();
        // Retry setup if references were cleared post-add

        if (VerifySetup())
        {
            controller         = GetComponent <ThirdPersonControllerNET> ();
            controller.onJump += OnJump;
            // Have OnJump invoked when the ThirdPersonController starts a jump
            currentRotation = 0.0f;
            lastRootForward = root.forward;
        }
    }
Ejemplo n.º 5
0
    public void Serialize(ref PhotonViewID obj)
    {
        if (write)
        {
            this.data.Add(obj);
        }
        else
        {
            int ID = (int)data[currentItem];
            currentItem++;

            int          internalID = ID % PhotonNetwork.MAX_VIEW_IDS;
            int          actorID    = ID / PhotonNetwork.MAX_VIEW_IDS;
            PhotonPlayer owner      = null;
            if (actorID > 0)
            {
                owner = PhotonPlayer.Find(actorID);
            }

            obj = new PhotonViewID(internalID, owner);
        }
    }
Ejemplo n.º 6
0
    /// <summary>
    /// Request a new viewID for the local player
    /// </summary>
    /// <returns></returns>
    public static PhotonViewID AllocateViewID()
    {
        // int playerID = player.ID;
        int newID = 0;

        while (networkingPeer.allocatedIDs.ContainsKey(newID))
        {
            newID++;
        }

        if (newID > MAX_VIEW_IDS)
        {
            Debug.LogError("ERROR: Too many view IDs used!");
            newID = 0;
        }

        int          ID     = newID;
        PhotonViewID viewID = new PhotonViewID(ID, player);

        networkingPeer.allocatedIDs.Add(newID, viewID);
        return(viewID);
    }
Ejemplo n.º 7
0
    public void SpawnNetworkedPlayer(Vector3 pos, Quaternion rot, PhotonViewID id1, PhotonPlayer np)
    {
        Transform newPlayer = Instantiate(tankPrefab, pos, rot) as Transform;
        //Set the PhotonView
        PhotonView[] nViews = newPlayer.GetComponentsInChildren<PhotonView>();
        nViews[0].viewID = id1;

        // Name the gameObject for cleanliness
        newPlayer.gameObject.name = np.name;

        // Set tank mode
        if (np.isLocal)
        {
            playerTank = newPlayer.gameObject;
            playerScript = newPlayer.GetComponent<Tank>();
            playerScript.SetTankType(Tank.TankMode.LocalPlayer);
            cameraManager.orbit = newPlayer.gameObject.transform;
        }
        else
        {
            newPlayer.GetComponent<Tank>().SetTankType(Tank.TankMode.RemotePlayer);
        }
    }
Ejemplo n.º 8
0
    /// <summary>
    /// Instantiate a scene-owned prefab over the network. The PhotonViews will be controllable by the MasterClient. This prefab needs to be located in the root of a "Resources" folder.
    /// </summary>
    /// <remarks>Instead of using prefabs in the Resources folder, you can manually Instantiate and assign PhotonViews. See doc.</remarks>
    /// <param name="prefabName">Name of the prefab to instantiate.</param>
    /// <param name="position">Position Vector3 to apply on instantiation.</param>
    /// <param name="rotation">Rotation Quaternion to apply on instantiation.</param>
    /// <param name="group">The group for this PhotonView.</param>
    /// <param name="data">Optional instantiation data. This will be saved to it's PhotonView.instantiationData.</param>
    /// <returns>The new instance of a GameObject with initialized PhotonView.</returns>
    public static GameObject InstantiateSceneObject(string prefabName, Vector3 position, Quaternion rotation, int group, object[] data)
    {
        if (!VerifyCanUseNetwork())
        {
            return null;
        }
        if (!isMasterClient)
        {
            Debug.LogError("PhotonNetwork error [InstantiateSceneObject]: Only the master client can Instantiate scene objects");
            return null;
        }

        GameObject prefabGo = (GameObject)Resources.Load(prefabName, typeof(GameObject));
        if (prefabGo == null)
        {
            Debug.LogError("PhotonNetwork error [InstantiateSceneObject]: Could not Instantiate the prefab [" + prefabName + "]. Please verify you have this gameobject in a Resources folder (and not in a subfolder)");
            return null;
        }
        if (prefabGo.GetComponent<PhotonView>() == null)
        {
            Debug.LogError("PhotonNetwork error [InstantiateSceneObject]: Could not Instantiate the prefab [" + prefabName + "] as it has no PhotonView attached to the root.");
            return null;
        }

        Component[] views = (Component[])prefabGo.GetComponentsInChildren<PhotonView>(true);
        PhotonViewID[] viewIDs = new PhotonViewID[views.Length];
        for (int i = 0; i < viewIDs.Length; i++)
        {
            viewIDs[i] = AllocateSceneViewID();
        }

        // Send to others, create info
        Hashtable instantiateEvent = networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, true);

        // Instantiate the GO locally (but the same way as if it was done via event). This will also cache the instantiationId
        return networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.mLocalActor, prefabGo);
    }
Ejemplo n.º 9
0
 /// <summary>
 /// Unregister a view ID
 /// </summary>
 /// <param name="viewID">PhotonViewID instance</param>
 public static void UnAllocateViewID(PhotonViewID viewID)
 {
     UnAllocateViewID(viewID.ID);
 }
    internal Hashtable SendInstantiate(string prefabName, Vector3 position, Quaternion rotation, int group, PhotonViewID[] viewIDs, object[] data, bool isGlobalObject)
    {
        int instantiateId = this.AllocateInstantiationId();

        Hashtable instantiateEvent = new Hashtable(); // This players info is sent via ActorID
        instantiateEvent[(byte)0] = prefabName;

        if (position != Vector3.zero)
        {
            instantiateEvent[(byte)1] = position;
        }

        instantiateEvent[(byte)2] = rotation;

        if (group != 0)
        {
            instantiateEvent[(byte)3] = group;
        }

        if (viewIDs != null && viewIDs.Length > 0)
        {
            instantiateEvent[(byte)4] = viewIDs; // LIMITS PHOTONVIEWS&PLAYERS
        }

        if (data != null)
        {
            instantiateEvent[(byte)5] = data;
        }

        instantiateEvent[(byte)6] = this.ServerTimeInMilliSeconds;
        instantiateEvent[(byte)7] = instantiateId;

        EventCaching cacheMode = EventCaching.AddToRoomCache;
        if (isGlobalObject) cacheMode = EventCaching.AddToRoomCacheGlobal;

        this.OpRaiseEvent(PhotonNetworkMessages.Instantiation, instantiateEvent, true, 0, cacheMode, ReceiverGroup.Others);
        return instantiateEvent;
    }
    internal GameObject DoInstantiate(Hashtable evData, PhotonPlayer photonPlayer, GameObject resourceGameObject)
    {
        string prefabName = (string)evData[(byte)0];

        Vector3 position;
        if (evData.ContainsKey((byte)1))
        {
            position = (Vector3)evData[(byte)1];
        }
        else
        {
            position = Vector3.zero;
        }

        Quaternion rotation = (Quaternion)evData[(byte)2];

        int group = 0;
        if (evData.ContainsKey((byte)3))
        {
            group = (int)evData[(byte)3];
        }

        PhotonViewID[] viewsIDs;
        if (evData.ContainsKey((byte)4))
        {
            viewsIDs = (PhotonViewID[])evData[(byte)4];
        }
        else
        {
            viewsIDs = new PhotonViewID[0];
        }

        object[] data;
        if (evData.ContainsKey((byte)5))
        {
            data = (object[])evData[(byte)5];
        }
        else
        {
            data = new object[0];
        }

        int serverTime = (int)evData[(byte)6];
        int instantiationId = (int)evData[(byte)7];

        // SetReceiving filtering
        if (this.blockReceivingGroups.Contains(group))
        {
            return null; // Ignore group
        }

        // Check prefab
        if (resourceGameObject == null)
        {
            resourceGameObject = (GameObject)Resources.Load(prefabName, typeof(GameObject));
            if (resourceGameObject == null)
            {
                Debug.LogError("PhotonNetwork error: Could not Instantiate the prefab [" + prefabName + "]. Please verify you have this gameobject in a Resources folder.");
                return null;
            }
        }

        //Add this PhotonView setup info to a list, so that the PhotonView can use this to setup if it's accessed DURING the Instantiation call (in awake)
        InstantiatedPhotonViewSetup newPVS = new InstantiatedPhotonViewSetup();
        newPVS.viewIDs = viewsIDs;
        newPVS.group = group;
        newPVS.instantiationData = data;
        instantiatedPhotonViewSetupList.Add(newPVS);

        // Instantiate the object
        GameObject go = (GameObject)GameObject.Instantiate(resourceGameObject, position, rotation);
        this.instantiatedObjects.Add(instantiationId, go);

        SetupInstantiatedGO(go, newPVS);

        // Send mono event
        object[] messageInfoParam = new object[1];
        messageInfoParam[0] = new PhotonMessageInfo(photonPlayer, serverTime, null);

        MonoBehaviour[] monos = go.GetComponentsInChildren<MonoBehaviour>();
        for (int index = 0; index < monos.Length; index++)
        {
            MonoBehaviour mono = monos[index];
            MethodInfo methodI = this.GetCachedMethod(mono, PhotonNetworkingMessage.OnPhotonInstantiate);
            if (methodI != null)
            {
                object result = methodI.Invoke((object)mono, messageInfoParam);
                if (methodI.ReturnType == typeof(System.Collections.IEnumerator))
                {
                    PhotonHandler.SP.StartCoroutine((IEnumerator)result);
                }
            }
        }

        return go;
    }
Ejemplo n.º 12
0
    private void Setup()
    {
        if (!Application.isPlaying) return;
        if (ranSetup) return;

        ranSetup = true;
        if (isSceneView)
        {

            if (sceneViewID < 1) Debug.LogError("SceneView " + sceneViewID);
            ID = new PhotonViewID(sceneViewID, null);
            registeredPhotonView = true;
            PhotonNetwork.networkingPeer.RegisterPhotonView(this);
        }
        else
        {
            //If this was PUN.Instantiated, this will setup the viewID in Awake
            //bool res =
            PhotonNetwork.networkingPeer.PhotonViewSetup_FindMatchingRoot(gameObject);
            //Debug.Log("Found: "+res);
        }
    }
Ejemplo n.º 13
0
    void SpawnOnNetwork(Vector3 pos, Quaternion rot, PhotonViewID id1, bool amOwner, PhotonPlayer np)
    {
        Transform newPlayer = Instantiate(playerPrefab, pos, rot) as Transform;
        SetPlayerTransform(np, newPlayer);

        SetNetworkViewIDs(newPlayer.gameObject, id1);

        if (amOwner)
        {
            myLocalTransform = newPlayer;
        }
        Player5 tmp = newPlayer.GetComponent<Player5>();
        tmp.SetOwner(amOwner);
    }
Ejemplo n.º 14
0
    /// <summary>
    /// Request a new viewID for the local player
    /// </summary>
    /// <returns></returns>
    public static PhotonViewID AllocateViewID()
    {
        // int playerID = player.ID;
        int newID = 0;
        while (networkingPeer.allocatedIDs.ContainsKey(newID))
        {
            newID++;
        }

        if (newID >= MAX_VIEW_IDS)
        {
            Debug.LogError("ERROR: Too many view IDs used!");
            newID = 0;
        }

        int ID = newID;
        PhotonViewID viewID = new PhotonViewID(ID, player);

        networkingPeer.allocatedIDs.Add(newID, viewID);
        return viewID;
    }
Ejemplo n.º 15
0
    public override bool Equals(object p)
    {
        PhotonViewID pp = p as PhotonViewID;

        return(pp != null && this.ID == pp.ID);
    }
    private static PhotonViewID[] AllocateSceneViewIDs(int number)
    {
        PhotonViewID[] viewIDs = new PhotonViewID[number];
        PhotonView[] photonViews = Resources.FindObjectsOfTypeAll(typeof(PhotonView)) as PhotonView[];

        if (photonViews == null || photonViews.Length == MAX_VIEW_IDS)
        {
            return null;
        }

        int lastAssignedSceneViewId = MAX_VIEW_IDS;
        for (int view = 0; view < number; view++)
        {
            int id;
            for (id = lastAssignedSceneViewId - 1; id >= 1; id--)
            {
                bool idIsInUse = false;
                foreach (PhotonView photonView in photonViews)
                {
                    if (photonView.viewID != null && photonView.viewID.ID == id)
                    {
                        idIsInUse = true;
                        break;
                    }
                }

                if (!idIsInUse)
                {
                    viewIDs[view] = new PhotonViewID(id, null);
                    lastAssignedSceneViewId = id;
                    break;
                }
            }

            // no ID was free?!
            if (lastAssignedSceneViewId != id)
            {
                Debug.Log("SceneView ID lookup failed.");
                return null;
            }
        }

        return viewIDs;
    }
    // Verify setup, configure
    void Start()
    {
        photonView = PhotonView.Get(this);
         	 photonViewID = photonView.viewID;

        Setup ();
            // Retry setup if references were cleared post-add

        if (VerifySetup ())
        {
            controller = GetComponent<ThirdPersonControllerNET> ();
            controller.onJump += OnJump;
                // Have OnJump invoked when the ThirdPersonController starts a jump
            currentRotation = 0.0f;
            lastRootForward = root.forward;
        }
    }
Ejemplo n.º 18
0
 //When a PhotonView instantiates it has viewID=0 and is unusable.
 //We need to assign the right viewID -on all players(!)- for it to work
 void SetPhotonViewIDs(GameObject go, PhotonViewID id1)
 {
     PhotonView[] nViews = go.GetComponentsInChildren<PhotonView>();
     nViews[0].viewID = id1;
 }
Ejemplo n.º 19
0
    void SpawnOnNetwork(Vector3 pos, Quaternion rot, PhotonViewID id1, PhotonPlayer np)
    {
        Transform newPlayer = Instantiate(playerPrefab, pos, rot) as Transform;
        //Set transform
        PlayerInfo4 pNode = GetPlayer(np);
        pNode.transform = newPlayer;
        //Set photonviewID everywhere!
        SetPhotonViewIDs(newPlayer.gameObject, id1);

        if (pNode.IsLocal())
        {
            localPlayerInfo = pNode;
        }

        //Maybe call some specific action on the instantiated object?
        //PLAYERSCRIPT tmp = newPlayer.GetComponent<PLAYERSCRIPT>();
        //tmp.SetPlayer(pNode.networkPlayer);
    }
Ejemplo n.º 20
0
 /// <summary>
 /// Unregister a view ID
 /// </summary>
 /// <param name="viewID">PhotonViewID instance</param>
 public static void UnAllocateViewID(PhotonViewID viewID)
 {
     UnAllocateViewID(viewID.ID % MAX_VIEW_IDS); //We need only the last bit, not the actor info (actor changes per room)
 }
Ejemplo n.º 21
0
 private void Setup()
 {
     if (!Application.isPlaying) return;
     if (ranSetup) return;
     ranSetup = true;
     if (isSceneView)
     {
         bool result = PhotonNetwork.networkingPeer.PhotonViewSetup_FindMatchingRoot(gameObject);
         if (result)
         {
             // This instantiated prefab needs to be corrected as it's incorrectly reported as a sceneview.
             // It is wrongly reported as isSceneView because a scene-prefab changes have been applied to the project prefab
             // The scene's prefab viewID is therefore saved to the project prefab. This workaround fixes all problems
             sceneViewID = 0;
         }
         else
         {
             if (sceneViewID < 1) Debug.LogError("SceneView " + sceneViewID);
             ID = new PhotonViewID(sceneViewID, null);
             registeredPhotonView = true;
             PhotonNetwork.networkingPeer.RegisterPhotonView(this);
         }
     }
     else
     {
         bool res = PhotonNetwork.networkingPeer.PhotonViewSetup_FindMatchingRoot(gameObject);
         if (!res)
         {
             if(PhotonNetwork.logLevel != PhotonLogLevel.ErrorsOnly)
                 Debug.LogWarning("Warning: Did not find the root of a PhotonView. This is only OK if you used GameObject.Instantiate to instantiate this prefab. Object: "+this.name);
         }
     }
 }
Ejemplo n.º 22
0
    /// <summary>
    /// Request a new viewID for the scene
    /// </summary>
    /// <returns></returns>
    private static PhotonViewID AllocateSceneViewID()
    {
        int newID = -1;
        for (int i = MAX_VIEW_IDS - 1; i >= 1; i--)
        {
            if (IsSceneViewIDFree(i))
            {
                newID = i;
                break;
            }
        }
        if (newID == -1)
            Debug.LogError("You ran out of scene view ID's!");

        PhotonViewID viewID = new PhotonViewID(newID, null);

        return viewID;
    }
Ejemplo n.º 23
0
 /// <summary>
 /// Unregister a view ID
 /// </summary>
 /// <param name="viewID">PhotonViewID instance</param>
 public static void UnAllocateViewID(PhotonViewID viewID)
 {
     UnAllocateViewID(viewID.ID);
 }
Ejemplo n.º 24
0
 //Start, since in Awake PhotonNetwork isn't set up properly.
 void Awake()
 {
     if (isSceneView)  
     {
         if (sceneViewID < 1) Debug.LogError("SceneView " + sceneViewID);
         ID = new PhotonViewID(sceneViewID, null);
         registeredPhotonView = true;
         PhotonNetwork.networkingPeer.RegisterPhotonView(this);
     }
     else
     {
         ID = new PhotonViewID(0, null);
     }
 }
Ejemplo n.º 25
0
 void SetNetworkViewIDs(GameObject go, PhotonViewID id1)
 {
     Component[] nViews = go.GetComponentsInChildren<PhotonView>();
     (nViews[0] as PhotonView).viewID = id1;
 }
Ejemplo n.º 26
0
    public void Serialize(ref PhotonViewID obj)
    {
        if (write)
        {
            data[currentItem] = obj.ID;
            currentItem++;
        }
        else
        {
            int ID = (int)data[currentItem];
            currentItem++;
            
            int internalID = ID % PhotonNetwork.MAX_VIEW_IDS;
            int actorID = ID / PhotonNetwork.MAX_VIEW_IDS;
            PhotonPlayer owner = null;
            if (actorID > 0)
            {
                owner = PhotonPlayer.Find(actorID);
            }

            obj = new PhotonViewID(internalID, owner);
        }
    }
Ejemplo n.º 27
0
    internal GameObject DoInstantiate(Hashtable evData, PhotonPlayer photonPlayer, GameObject resourceGameObject)
    {
        string prefabName = (string)evData[(byte)0];
        
        Vector3 position;
        if (evData.ContainsKey((byte)1))
        {
            position = (Vector3)evData[(byte)1];
        }
        else
        {
            position = Vector3.zero;
        }

        Quaternion rotation = (Quaternion)evData[(byte)2];

        int group = 0;
        if (evData.ContainsKey((byte)3))
        {
            group = (int)evData[(byte)3];
        }

        PhotonViewID[] viewsIDs;
        if (evData.ContainsKey((byte)4))
        {
            viewsIDs = (PhotonViewID[])evData[(byte)4];
        }
        else
        {
            viewsIDs = new PhotonViewID[0];
        }

        object[] data;
        if (evData.ContainsKey((byte)5))
        {
            data = (object[])evData[(byte)5];
        }
        else
        {
            data = new object[0];
        }

        int serverTime = (int)evData[(byte)6];
        int instantiationId = (int)evData[(byte)7];

        // SetReceiving filtering
        if (this.blockReceivingGroups.Contains(group))
        {
            return null; // Ignore group
        }

        // Check prefab
        if (resourceGameObject == null)
        {
            resourceGameObject = (GameObject)Resources.Load(prefabName, typeof(GameObject));
            if (resourceGameObject == null)
            {
                Debug.LogError("PhotonNetwork error: Could not Instantiate the prefab [" + prefabName + "]. Please verify you have this gameobject in a Resources folder.");
                return null;
            }
        }

        // Instantiate the object
        GameObject go = (GameObject)GameObject.Instantiate(resourceGameObject, position, rotation);
        this.instantiatedObjects.Add(instantiationId, go);

        // Assign view IDs
        PhotonView[] views = (PhotonView[])go.GetComponentsInChildren<PhotonView>();
        int i = 0;
        foreach (PhotonView view in views)
        {
            view.viewID = viewsIDs[i];
            view.group = group;
            view.instantiationData = data;
            i++;
        }

        // Send mono event
        object[] messageInfoParam = new object[1];
        messageInfoParam[0] = new PhotonMessageInfo(photonPlayer, serverTime, null);

        MonoBehaviour[] monos = go.GetComponentsInChildren<MonoBehaviour>();
        foreach (MonoBehaviour mono in monos)
        {
            MethodInfo methodI = this.GetCachedMethod(mono, ReflectionMethods.OnPhotonInstantiate);
            if (methodI != null)
            {
                object result = methodI.Invoke((object)mono, messageInfoParam);
                if (methodI.ReturnType == typeof(System.Collections.IEnumerator))
                {
                    PhotonHandler.SP.StartCoroutine((IEnumerator)result);
                }
            }
        }

        return go;
    }