PhotonNetwork.InstantiateSceneObject C# (CSharp) Метод

InstantiateSceneObject() публичный статический Метод

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.
Only the master client can Instantiate scene objects. Instead of using prefabs in the Resources folder, you can manually Instantiate and assign PhotonViews. See doc.
public static InstantiateSceneObject ( string prefabName, Vector3 position, Quaternion rotation, int group, object data ) : GameObject
prefabName string Name of the prefab to instantiate.
position Vector3 Position Vector3 to apply on instantiation.
rotation Quaternion Rotation Quaternion to apply on instantiation.
group int The group for this PhotonView.
data object Optional instantiation data. This will be saved to it's PhotonView.instantiationData.
Результат GameObject
    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;
        if (!UsePrefabCache || !PrefabCache.TryGetValue(prefabName, out prefabGo))
        {
            prefabGo = (GameObject)Resources.Load(prefabName, typeof(GameObject));
            if (UsePrefabCache)
            {
                PrefabCache.Add(prefabName, prefabGo);
            }
        }

        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;
        }

        // a scene object instantiated with network visibility has to contain a PhotonView
        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.GetPhotonViewsInChildren();
        int[] viewIDs = AllocateSceneViewIDs(views.Length);

        if (viewIDs == null)
        {
            Debug.LogError("PhotonNetwork error [InstantiateSceneObject]: Could not Instantiate the prefab [" + prefabName + "] as no ViewIDs are free to use. Max is: " + MAX_VIEW_IDS);
            return null;
        }

        // 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);
    }

Usage Example

Пример #1
0
    public void SpawnHoles()
    {
        int y;
        int x;

        for (int i = 0; i < 10; i++)
        {
            y = Random.Range(-90, -30);
            x = Random.Range(10, 152);
            PhotonNetwork.InstantiateSceneObject("Holes", new Vector3((float)x, (float)y, 10f), Quaternion.identity, 0, null);
            if (x < 153)
            {
                GameObject lava = GameObject.Find("GenLava(Clone)");
                lava.GetComponent <PhotonView>().RPC("SetInitialBlock", PhotonTargets.All, x, y - (int)lava.GetComponent <Lava>().offset_y);
            }
        }
        for (int i = 0; i < 10; i++)
        {
            y = Random.Range(5, 90);
            x = Random.Range(154, 300);
            PhotonNetwork.InstantiateSceneObject("Holes", new Vector3((float)x, (float)y, 10f), Quaternion.identity, 0, null);
            if (x > 153)
            {
                GameObject water = GameObject.Find("GenWater(Clone)");
                //Debug.Log (x + "/" + (y - (int)lavaScript.offset_y));
                water.GetComponent <PhotonView>().RPC("SetInitialBlock", PhotonTargets.All, x - (int)water.GetComponent <Lava>().offset_x, y);
            }
        }
    }
All Usage Examples Of PhotonNetwork::InstantiateSceneObject