码迷,mamicode.com
首页 > 其他好文 > 详细

Photon自定义加载Resource之外的资源

时间:2019-01-16 13:55:28      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:return   locate   自定义   delegate   iat   other   ati   ack   The   

以下代码放置到PhotonNetwork.cs即可

    #region >>> Photon自定义异步加载GameObject

    public delegate void CustomLoader(string prefabName, Action<UnityEngine.Object> ac);
    public static bool InstantiateCustom(string prefabName, Vector3 position, Quaternion rotation, byte group, object[] data, CustomLoader loader, Action<GameObject> callback)
    {
        if (!connected || (InstantiateInRoomOnly && !inRoom))
        {
            Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Client should be in a room. Current connectionStateDetailed: " + PhotonNetwork.connectionStateDetailed);
            return false;
        }

        GameObject prefabGo;
        if (!UsePrefabCache || !PrefabCache.TryGetValue(prefabName, out prefabGo))
        {
            loader(prefabName, (ab) =>
            {
                prefabGo = (GameObject)(ab);

                if (UsePrefabCache)
                {
                    PrefabCache.Add(prefabName, prefabGo);
                }

                if (prefabGo == null)
                {
                    Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Verify the Prefab is in a Resources folder (and not in a subfolder)");
                    callback.Invoke(null);
                }

                // a scene object instantiated with network visibility has to contain a PhotonView
                if (prefabGo.GetComponent<PhotonView>() == null)
                {
                    Debug.LogError("Failed to Instantiate prefab:" + prefabName + ". Prefab must have a PhotonView component.");
                    callback.Invoke(null);
                }

                Component[] views = (Component[])prefabGo.GetPhotonViewsInChildren();
                int[] viewIDs = new int[views.Length];
                for (int i = 0; i < viewIDs.Length; i++)
                {
                    //Debug.Log("Instantiate prefabName: " + prefabName + " player.ID: " + player.ID);
                    viewIDs[i] = AllocateViewID(player.ID);
                }

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

                // Instantiate the GO locally (but the same way as if it was done via event). This will also cache the instantiationId
                callback.Invoke(networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.LocalPlayer, prefabGo));
            });
            return true;
        }
        if (prefabGo != null)
        {
            // a scene object instantiated with network visibility has to contain a PhotonView
            if (prefabGo.GetComponent<PhotonView>() == null)
            {
                Debug.LogError("Failed to Instantiate prefab:" + prefabName + ". Prefab must have a PhotonView component.");
                callback.Invoke(null);
            }

            Component[] views = (Component[])prefabGo.GetPhotonViewsInChildren();
            int[] viewIDs = new int[views.Length];
            for (int i = 0; i < viewIDs.Length; i++)
            {
                //Debug.Log("Instantiate prefabName: " + prefabName + " player.ID: " + player.ID);
                viewIDs[i] = AllocateViewID(player.ID);
            }

            // Send to others, create info
            Hashtable instantiateEvent = networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, false);
            callback.Invoke(networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.LocalPlayer, prefabGo));
            return true;
        }
        return false;
    }

    #endregion

  

Photon自定义加载Resource之外的资源

标签:return   locate   自定义   delegate   iat   other   ati   ack   The   

原文地址:https://www.cnblogs.com/kenkao/p/10276556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!