Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     public static void RunConversion()
     {
         //Ask if user has made a backup.
         int option = EditorUtility.DisplayDialogComplex("Conversion", "Attempt automatic conversion from Unity Networking to Photon Unity Networking \"PUN\"?", "Yes", "No!", "Pick Script Folder");
         switch (option)
         {
             case 0:
                 break;
             case 1:
                 return;
             case 2:
                 PickFolderAndConvertScripts();
                 return;
             default:
                 return;
         }

         //REAAAALY?
         bool 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(), 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
                 PhotonViewHandler.HierarchyChange(); //TODO: most likely this is triggered on change or on save

                 Output("Replaced " + converted2 + " NetworkViews with PhotonViews in scene: " + sceneName);
                 EditorApplication.SaveScene(EditorApplication.currentScene);
             }

         }

         //Convert C#/JS scripts (API stuff)
         List scripts = GetScriptsInFolder("Assets");

         EditorUtility.DisplayProgressBar("Converting..", "Scripts..", 0.9f);
         ConvertScripts(scripts);

         Output(EditorApplication.timeSinceStartup + " Completed conversion!");
         EditorUtility.ClearProgressBar();

         EditorUtility.DisplayDialog("Completed the conversion", "Don't forget to add \"PhotonNetwork.ConnectWithDefaultSettings();\" to connect to the Photon server before using any multiplayer functionality.", "OK");
     }