Esempio n. 1
0
        ///// <summary>
        ///// Add the NetworkIdenity/PhotonView to an NST gameobject. Must be added before runtime (thus this is editor only script).
        ///// If added at runtime, it may get added AFTER network events fire. Also will attempt to add this NST as a registered prefab
        ///// and player prefab. Will also attempt to register the supplied go with the NetworkManager and as the PlayerPrefab if there is none
        ///// but one is expected.
        ///// </summary>
        //public static void EnsureHasEntityComponentForNetLib(GameObject go, bool playerPrefabCandidate = true)
        //{
        //	if (!Application.isPlaying)
        //		AddAsRegisteredPrefab(go, true, !playerPrefabCandidate, true);
        //}
        /// <summary>
        /// Attempts to add a prefab with NST on it to the NetworkManager spawnable prefabs list, after doing some checks to make
        /// sure it makes sense to. Will then add as the network manager player prefab if it is set to auto spawwn and is still null.
        /// </summary>
        public static bool AddAsRegisteredPrefab(GameObject go, bool playerPrefabCandidate, bool silence = false)
        {
            if (Application.isPlaying)
            {
                return(false);
            }

            // Don't replace an existing playerPrefab
            NetworkManager nm = NetAdapterTools.GetNetworkManager();

            if (!nm)
            {
                return(false);
            }

            PrefabType type     = PrefabUtility.GetPrefabType(go);
            GameObject prefabGO = (type == PrefabType.Prefab) ? go : PrefabUtility.GetPrefabParent(go) as GameObject;

            if (!prefabGO)
            {
                if (!silence)
                {
                    Debug.Log("You have a NST component on a gameobject '" + go.name + "', which is not a prefab. Be sure to make '" + go.name + "' a prefab, otherwise it cannot be registered with the NetworkManager for network spawning.");
                }
                return(false);
            }

            NetworkIdentity ni = prefabGO.GetComponent <NetworkIdentity>();

            if (!ni)
            {
                if (!silence)
                {
                    Debug.Log("There is no NetworkIdentity on '" + go.name + "', so it cannot be registered with the NetworkManager for network spawning.");
                }
                return(false);
            }

            // Force the NetworkIdentity to be valid. Bad things happen if we don't do this. UNET suck.
            ni.assetId.IsValid();

            if (!nm.spawnPrefabs.Contains(prefabGO))
            {
                Debug.Log("Automatically adding '<b>" + prefabGO.name + "</b>' to the NetworkManager spawn list for you.");

                nm.spawnPrefabs.Add(prefabGO);
                EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
            }

            // Set this as the player prefab if there is none yet
            if (nm.playerPrefab == null && nm.autoCreatePlayer && playerPrefabCandidate)
            {
                Debug.Log("Automatically adding '<b>" + prefabGO.name + "</b>' to the NetworkManager as the <b>playerPrefab</b>. If this isn't desired, assign your the correct prefab to the Network Manager, or turn off Auto Create Player in the NetworkManager.");
                nm.playerPrefab = prefabGO;
                EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
            }

            NetAdapterTools.EnsureNMPlayerPrefabIsLocalAuthority(nm);
            return(true);
        }
        private void Update()
        {
            if (Application.isPlaying)
            {
                return;
            }

            if (EditorApplication.isCompiling)
            {
                return;
            }

            if (!needsEditorModePostAwakeCheck)
            {
                return;
            }

            //Destroy the existing Master so it can be readded, to ensure it hasn't been messed up by a library change.
            //NetAdapterTools.RemoveComponentTypeFromScene<NSTMaster>(true);

            //FindMissingScripts.DestroyMissingComponentOnRoot(FindObjectOfType<MasterNetAdapter>().gameObject);
            NetAdapterTools.RemoveUnusedNetworkManager();
            NetAdapterTools.TryToAddDependenciesEverywhere();
#if MIRROR || !UNITY_2019_1_OR_NEWER
            NetAdapterTools.GetNetworkManager(true);
#endif
            NetAdapterTools.CopyPlayerPrefabFromPUNtoOthers();
            NetAdapterTools.EnsureNMPlayerPrefabIsLocalAuthority();
            NetAdapterTools.EnsureSceneNetLibDependencies(false);

            needsEditorModePostAwakeCheck = false;
        }