public static void ConvertItemToMultiplayer(MenuCommand command) { GameObject prefab = (GameObject)Selection.activeObject; string saveLocation = string.Format("Assets{0}Resources{0}", Path.DirectorySeparatorChar); E_Helpers.CreateDirectory(E_Helpers.GetDirectoryPath(saveLocation)); Debug.Log("Converting Prefab: " + prefab.name); try { CB_ConvertPrefabToMultiplayer(prefab.gameObject); if (EditorUtility.DisplayDialog("SUCCESS!", "You have successfully converted: " + prefab.name + ". " + "The multiplayer version can be found in the Assets/Resources folder.", "Great!")) { } Debug.Log("Successfully Converted Prefab: " + prefab.name); } catch (Exception ex) { Debug.Log(ex); if (EditorUtility.DisplayDialog("FAILURE!", "Failed to convert: " + prefab.name + " with error: \n\n " + ex.ToString(), "Okay")) { } } }
void ConvertPrefabsToMultiplayer() { _converted = false; _converting = true; string saveLocation = string.Format("Assets{0}Resources{0}", Path.DirectorySeparatorChar); E_Helpers.CreateDirectory(E_Helpers.GetDirectoryPath(saveLocation)); GameObject _copiedPrefab = null; foreach (KeyValuePair <GameObject, string> prefab in CB_prefabs) { _copiedPrefab = GameObject.Instantiate(prefab.Key, Vector3.zero, Quaternion.identity) as GameObject; _copiedPrefab.name = "MP_" + _copiedPrefab.name.Replace("(Clone)", ""); saveLocation = string.Format("Assets{0}Resources{0}{1}.prefab", Path.DirectorySeparatorChar, _copiedPrefab.name); CB_COMP_vItemCollection(_copiedPrefab); CB_COMP_vBreakableObject(_copiedPrefab); CB_COMP_vHealthController(_copiedPrefab); CB_COMP_vProjectileControle(_copiedPrefab); CB_COMP_vShooterWeapon(_copiedPrefab); if (E_Helpers.FileExists(saveLocation)) { if (EditorUtility.DisplayDialog("Duplicate Detected!", string.Format("Prefab \"{0}\" already exists, would you like to overwrite it or not copy it?", _copiedPrefab.name), "Overwrite", "Not Copy")) { //Overwrite E_Helpers.DeleteFile(saveLocation); PrefabUtility.SaveAsPrefabAsset(_copiedPrefab, saveLocation); } else { Debug.Log("Skipped: " + _copiedPrefab.name); } } else { //Doesn't exist create it saveLocation = AssetDatabase.GenerateUniqueAssetPath(saveLocation); PrefabUtility.SaveAsPrefabAsset(_copiedPrefab, saveLocation); } DestroyImmediate(_copiedPrefab); } //AssetDatabase.SaveAssets(); //AssetDatabase.Refresh(); _converting = false; _converted = true; Debug.Log("Finished converting CB_prefabs"); }
IEnumerator ConvertPlayer(GameObject targetObj) { GameObject _builtPrefab = null; convertLog.Clear(); _converting = true; _errorsOccured = false; if (convertLog != null) { convertLog.Add("Beginning to convert: " + targetObj.name); } if (convertLog != null) { convertLog.Add(""); } if (convertLog != null) { convertLog.Add("Copying " + targetObj.name + " -> MP_" + targetObj.name); } // Copy Player GameObject GameObject _copiedPlayer = GameObject.Instantiate(targetObj, targetObj.transform.position + Vector3.left, Quaternion.identity) as GameObject; _copiedPlayer.name = _copiedPlayer.name.Replace("(Clone)", ""); _copiedPlayer.name = "MP_" + _copiedPlayer.name; //Add Components if (CB_addVoiceChat == true) { CB_COMP_PlayerVoiceChat(_copiedPlayer, ref convertLog); } if (CB_addNameBar == true) { CB_COMP_PlayerNameBar(_copiedPlayer, ref convertLog); } CB_COMP_vMeleeCombatInput(_copiedPlayer, ref convertLog); CB_COMP_vWeaponHolderManager(_copiedPlayer, ref convertLog); CB_COMP_vGenericAction(_copiedPlayer, ref convertLog); CB_COMP_vMeleeManager(_copiedPlayer, ref convertLog); CB_COMP_vHeadTrack(_copiedPlayer, ref convertLog); CB_COMP_vShooterMeleeInput(_copiedPlayer, ref convertLog); CB_COMP_vShooterManager(_copiedPlayer, ref convertLog); CB_COMP_vFreeClimb(_copiedPlayer, ref convertLog); CB_COMP_vSwimming(_copiedPlayer, ref convertLog); CB_COMP_vZipline(_copiedPlayer, ref convertLog); CB_COMP_SyncPlayer(_copiedPlayer, ref convertLog); CB_COMP_vThirdPersonController(_copiedPlayer, ref convertLog); CB_COMP_vItemManager(_copiedPlayer, ref convertLog); CB_COMP_vLadderAction(_copiedPlayer, ref convertLog); // Make sure the file name is unique, in case an existing Prefab has the same name. string saveLocation = string.Format("Assets{0}Resources{0}{1}.prefab", Path.DirectorySeparatorChar, _copiedPlayer.name); if (E_Helpers.CreateDirectory(E_Helpers.GetDirectoryPath(saveLocation))) { if (convertLog != null) { convertLog.Add("Created Assets/Resources directory"); } } E_Helpers.SetObjectIcon(_copiedPlayer, E_Core.h_playerIcon); if (E_Helpers.FileExists(saveLocation)) { if (EditorUtility.DisplayDialog("Duplicate Detected!", "A prefab with the same name already exists, would you like to overwrite it or create a new one, keeping both?", "Overwrite", "Keep Both")) { //Overwrite E_Helpers.DeleteFile(saveLocation); if (convertLog != null) { convertLog.Add("Generating prefab at: " + saveLocation); } PrefabUtility.SaveAsPrefabAsset(_copiedPlayer, saveLocation); } else { //Keep Both saveLocation = AssetDatabase.GenerateUniqueAssetPath(saveLocation); if (convertLog != null) { convertLog.Add("Generating prefab at: " + saveLocation); } PrefabUtility.SaveAsPrefabAsset(_copiedPlayer, saveLocation); } } else { saveLocation = AssetDatabase.GenerateUniqueAssetPath(saveLocation); if (convertLog != null) { convertLog.Add("Generating prefab at: " + saveLocation); } PrefabUtility.SaveAsPrefabAsset(_copiedPlayer, saveLocation); } if (FindObjectOfType <NetworkManager>()) { if (convertLog != null) { convertLog.Add("Adding generated prefab to the \"playerPrefab\" field in the \"NetworkManager\""); } _builtPrefab = E_Helpers.GetPrefabReference(saveLocation); int timeout = 4; int count = 0; while (_builtPrefab == null) { if (count == timeout) { Debug.LogWarning("Unable to locate the player prefab to place into the NetworkManager."); break; } yield return(new WaitForSeconds(0.5f)); count += 1; _builtPrefab = E_Helpers.GetPrefabReference(saveLocation); } FindObjectOfType <NetworkManager>().playerPrefab = _builtPrefab; } else { if (convertLog != null) { convertLog.Add("WARNING: Skipped adding to \"NetworkManager\" component."); } if (EditorUtility.DisplayDialog("Unable To Find NetworkManager", "No NetworkManager was found in the scene so this converted player was not added to the \"playerPrefab\"!" + " When the NetworkManager is added to the scene make sure to add the generated prefab in the \"Resources\" folder to that field.", "Okay")) { Debug.LogWarning("No NetworkManager component was found in the scene so this prefab was not added a spawn object. When the NetworkManager component is added to the scene please add a prefab to it."); } } Selection.activeObject = _copiedPlayer; foreach (string log in convertLog) { Debug.Log(log); } yield return(null); }