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:

     {
         get
         {
             if (currentSettings == null)
             {
                 // find out if ServerSettings can be instantiated (existing script check)
                 ScriptableObject serverSettingTest = CreateInstance("ServerSettings");
                 if (serverSettingTest == null)
                 {
                     Debug.LogError(CurrentLang.ServerSettingsMissingLabel);
                     return null;
                 }
                 DestroyImmediate(serverSettingTest);

                 // try to load settings from file
                 ReLoadCurrentSettings();

                 // if still not loaded, create one
                 if (currentSettings == null)
                 {
                     string settingsPath = Path.GetDirectoryName(PhotonNetwork.serverSettingsAssetPath);
                     if (!Directory.Exists(settingsPath))
                     {
                         Directory.CreateDirectory(settingsPath);
                         AssetDatabase.ImportAsset(settingsPath);
                     }

                     currentSettings = (ServerSettings)ScriptableObject.CreateInstance("ServerSettings");
                     if (currentSettings != null)
                     {
                         AssetDatabase.CreateAsset(currentSettings, PhotonNetwork.serverSettingsAssetPath);
                     }
                     else
                     {
                         Debug.LogError(CurrentLang.ServerSettingsMissingLabel);
                     }
                 }

                 // settings were loaded or created. set this editor's initial selected region now (will be changed in GUI)
                 if (currentSettings != null)
                 {
                     selectedRegion = currentSettings.PreferredRegion;
                 }
             }

             return currentSettings;
         }

         protected set
         {
             currentSettings = value;
         }
     }