static void OnPostprocessAllAssets( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { bool weChangedPhotonViews = false; // Strips any scene settings from PhotonViews in prefabs. // (i.e.: assigned viewIDs are removed) foreach (string str in importedAssets) { if (str.EndsWith(".prefab")) { Object[] objs = (Object[])AssetDatabase.LoadAllAssetsAtPath(str); foreach (Object obj in objs) { if (obj != null && obj.GetType() == typeof(GameObject)) { PhotonView[] views = ((GameObject)obj).GetComponents <PhotonView>(); foreach (PhotonView view in views) { PhotonViewInspector.MakeProjectView(view); } if (views.Length > 0) { weChangedPhotonViews = true; } } } } } // Problem here: VerifyAllSceneViews will only fix the prefabs instances in the current open scene, not for other scenes! // See PhotonEditor.EditorUpdate: Here we will check all newly opened scenes for this possible issue. // No known issues as of 5 March 2011 this seems to work fine with changing viewIDs on prefabs etc. (Even for scenes that are not open) - Mike/Leepo if (weChangedPhotonViews) { PhotonViewInspector.VerifyAllSceneViews(); } }
public static void RunConversion() { //Ask if user has made a backup. bool result = EditorUtility.DisplayDialog("Conversion", "Did you create a backup of your project before converting?", "Yes", "Abort conversion"); if (!result) { return; } //REAAAALY? result = EditorUtility.DisplayDialog("Conversion", "Disclaimer: The code conversion feature is quite crude, but should do it's job well (see the sourcecode). A backup is therefore strongly recommended!", "Yes, I've made a backup: GO", "Abort"); if (!result) { return; } Output(EditorApplication.timeSinceStartup + " Started conversion of Unity networking -> Photon"); //Ask to save current scene (optional) EditorApplication.SaveCurrentSceneIfUserWantsTo(); EditorUtility.DisplayProgressBar("Converting..", "Starting.", 0); //Convert NetworkViews to PhotonViews in Project prefabs //Ask the user if we can move all prefabs to a resources folder bool movePrefabs = EditorUtility.DisplayDialog("Conversion", "Can all prefabs that use a PhotonView be moved to a Resources/ folder? You need this if you use Network.Instantiate.", "Yes", "No"); string[] prefabs = Directory.GetFiles("Assets/", "*.prefab", SearchOption.AllDirectories); foreach (string prefab in prefabs) { EditorUtility.DisplayProgressBar("Converting..", "Object:" + prefab, 0.6f); Object[] objs = (Object[])AssetDatabase.LoadAllAssetsAtPath(prefab); int converted = 0; foreach (Object obj in objs) { if (obj != null && obj.GetType() == typeof(GameObject)) { converted += ConvertNetworkView(((GameObject)obj).GetComponents <NetworkView>(), false); } } if (movePrefabs && converted > 0) { //This prefab needs to be under the root of a Resources folder! string path = prefab.Replace("\\", "/"); int lastSlash = path.LastIndexOf("/"); int resourcesIndex = path.LastIndexOf("/Resources/"); if (resourcesIndex != lastSlash - 10) { if (path.Contains("/Resources/")) { Debug.LogWarning("Warning, prefab [" + prefab + "] was already in a resources folder. But has been placed in the root of another one!"); } //This prefab NEEDS to be placed under a resources folder string resourcesFolder = path.Substring(0, lastSlash) + "/Resources/"; EnsureFolder(resourcesFolder); string newPath = resourcesFolder + path.Substring(lastSlash + 1); string error = AssetDatabase.MoveAsset(prefab, newPath); if (error != "") { Debug.LogError(error); } Output("Fixed prefab [" + prefab + "] by moving it into a resources folder."); } } } //Convert NetworkViews to PhotonViews in scenes string[] sceneFiles = Directory.GetFiles("Assets/", "*.unity", SearchOption.AllDirectories); foreach (string sceneName in sceneFiles) { EditorApplication.OpenScene(sceneName); EditorUtility.DisplayProgressBar("Converting..", "Scene:" + sceneName, 0.2f); int converted2 = ConvertNetworkView((NetworkView[])GameObject.FindObjectsOfType(typeof(NetworkView)), true); if (converted2 > 0) { //This will correct all prefabs: The prefabs have gotten new components, but the correct ID's were lost in this case PhotonViewInspector.VerifyAllSceneViews(); Output("Replaced " + converted2 + " NetworkViews with PhotonViews in scene: " + sceneName); EditorApplication.SaveScene(EditorApplication.currentScene); } } //Convert C#/JS scripts (API stuff) List <string> scripts = new List <string>(); scripts.AddRange(Directory.GetFiles("Assets/", "*.cs", SearchOption.AllDirectories)); scripts.AddRange(Directory.GetFiles("Assets/", "*.js", SearchOption.AllDirectories)); scripts.AddRange(Directory.GetFiles("Assets/", "*.boo", SearchOption.AllDirectories)); EditorUtility.DisplayProgressBar("Converting..", "Scripts..", 0.9f); ConvertScripts(scripts); Output(EditorApplication.timeSinceStartup + " Completed conversion!"); EditorUtility.ClearProgressBar(); }
static void EditorUpdate() { if (ServerSetting == null || (!checkedPhotonWizard && ServerSetting.HostType == ServerSettings.HostingOption.NotSet)) { // Debug.Log("EditorUpdate open Wizard. " + ServerSetting + " checkedPhotonWizard " + checkedPhotonWizard); checkedPhotonWizard = true; PhotonEditor window = (PhotonEditor)GetWindow(typeof(PhotonEditor), false, "PUN Setup wizard", true); window.SetSetupWizard(); window.Show(); } // Workaround for TCP crash. Plus this surpresses any other recompile errors. if (EditorApplication.isCompiling) { if (PhotonNetwork.connected) { if (lastWarning > EditorApplication.timeSinceStartup - 3) { //Prevent error spam Debug.LogWarning("Unity recompile forced a Photon Disconnect"); lastWarning = EditorApplication.timeSinceStartup; } PhotonNetwork.Disconnect(); } } else if (!EditorApplication.isPlaying) { //The following code could be optimized if Unity provides the right callbacks. // The current performance is 'OK' as we do check if the list changes (add/remove/duplicate should always change the length) //We are currently checking all selected PhotonViews every editor-frame //Instead, we only want to check this when an asset is placed in a scene (at editor time) //We need some sort of "OnCreated" call for scene objects. UnityEngine.Object[] objs = Selection.GetFiltered(typeof(PhotonView), SelectionMode.ExcludePrefab | SelectionMode.Editable | SelectionMode.Deep); if (objs.Length != lastPhotonViewListLength) { bool changed = false; foreach (UnityEngine.Object obj in objs) { PhotonView view = obj as PhotonView; if (!PhotonViewInspector.VerifySceneView(view)) { changed = true; } } if (changed) { Debug.Log("PUN: Corrected one or more scene-PhotonViews."); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } lastPhotonViewListLength = objs.Length; } // Check the newly opened scene for wrong PhotonViews // This can happen when changing a prefab while viewing scene A. Instances in scene B will not be corrected. if (lastScene != EditorApplication.currentScene && EditorApplication.currentScene != "") { lastScene = EditorApplication.currentScene; PhotonViewInspector.VerifyAllSceneViews(); } } }
// called in editor, opens wizard for initial setup, keeps scene PhotonViews up to date and closes connections when compiling (to avoid issues) private static void EditorUpdate() { if (dontCheckPunSetup) { return; } // serverSetting is null when the file gets deleted. otherwise, the wizard should only run once and only if hosting option is not (yet) set if (!PhotonEditor.Current.DisableAutoOpenWizard && PhotonEditor.Current.HostType == ServerSettings.HostingOption.NotSet) { ShowRegistrationWizard(); } // Workaround for TCP crash. Plus this surpresses any other recompile errors. if (EditorApplication.isCompiling) { if (PhotonNetwork.connected) { if (lastWarning > EditorApplication.timeSinceStartup - 3) { // Prevent error spam Debug.LogWarning("Unity recompile forced a Photon Disconnect"); lastWarning = EditorApplication.timeSinceStartup; } PhotonNetwork.Disconnect(); } } else if (!EditorApplication.isPlaying) { // The following code could be optimized if Unity provides the right callbacks. // The current performance is 'OK' as we do check if the list changes (add/remove/duplicate should always change the length) // We are currently checking all selected PhotonViews on hierarchy- and on project-change // Instead, we only want to check this when an NEW asset is placed in a scene (at editor time) // We need some sort of "OnCreated" call for scene objects. UnityEngine.Object[] objs = Selection.GetFiltered(typeof(PhotonView), SelectionMode.ExcludePrefab | SelectionMode.Editable | SelectionMode.Deep); if (objs.Length > 0 && (objs.Length != lastPhotonViewListLength || (lastFirstElement != objs[0]))) { bool changed = false; foreach (UnityEngine.Object obj in objs) { PhotonView view = obj as PhotonView; if (!PhotonViewInspector.VerifySceneView(view)) { changed = true; } } if (changed) { Debug.Log("PUN: Corrected one or more scene-PhotonViews."); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } lastPhotonViewListLength = objs.Length; lastFirstElement = objs[0]; } // Check the newly opened scene for wrong PhotonViews // This can happen when changing a prefab while viewing scene A. Instances in scene B will not be corrected. if (lastScene != EditorApplication.currentScene && EditorApplication.currentScene != string.Empty) { lastScene = EditorApplication.currentScene; PhotonViewInspector.VerifyAllSceneViews(); } } }