-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ElympicsLobby to "Add GameObject" context menu
- Loading branch information
Dawid Sygocki
committed
Aug 28, 2023
1 parent
3b5938f
commit 624214b
Showing
5 changed files
with
47 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,58 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Elympics | ||
{ | ||
public static class ElympicsMouseActions | ||
{ | ||
private const string PathToElympicsSystem = "Elympics"; | ||
private const string PathToElympicsLobby = "ElympicsLobby"; | ||
|
||
[MenuItem(ElympicsEditorMenuPaths.MOUSE_ACTION_CREATE_ELYMPICS_SYSTEM, priority = 11)] | ||
[MenuItem(ElympicsEditorMenuPaths.MOUSE_ACTION_CREATE_ELYMPICS_SYSTEM)] | ||
private static void AddElympicsSystemToScene() | ||
{ | ||
var elympicsSystemPrefabReference = Resources.Load<GameObject>(PathToElympicsSystem); | ||
const string name = "Elympics System"; | ||
if (HasActiveSceneAnyObjectOfType<ElympicsLobbyClient>()) | ||
{ | ||
Debug.LogError($"{name} cannot be placed in the menu scene. Use a separate game scene."); | ||
return; | ||
} | ||
AddUniquePrefabToScene<GameSceneManager>(PathToElympicsSystem, name); | ||
} | ||
|
||
[MenuItem(ElympicsEditorMenuPaths.MOUSE_ACTION_CREATE_ELYMPICS_LOBBY)] | ||
private static void AddElympicsLobbyToScene() | ||
{ | ||
const string name = "Elympics Lobby"; | ||
if (HasActiveSceneAnyObjectOfType<GameSceneManager>()) | ||
{ | ||
Debug.LogError($"{name} cannot be placed in the game scene. Use a separate menu scene."); | ||
return; | ||
} | ||
AddUniquePrefabToScene<ElympicsLobbyClient>(PathToElympicsLobby, name); | ||
} | ||
|
||
if (elympicsSystemPrefabReference != null) | ||
_ = PrefabUtility.InstantiatePrefab(elympicsSystemPrefabReference); | ||
else | ||
Debug.LogError("Cannot instantiate elympics system - prefab reference is null!"); | ||
private static void AddUniquePrefabToScene<T>(string path, string name) | ||
where T : Component | ||
{ | ||
if (HasActiveSceneAnyObjectOfType<T>()) | ||
{ | ||
Debug.LogError($"{name} is already present in the current scene."); | ||
return; | ||
} | ||
var prefabReference = Resources.Load<T>(path); | ||
if (prefabReference == null) | ||
{ | ||
Debug.LogError($"Cannot instantiate {name} - prefab reference is null!"); | ||
return; | ||
} | ||
var instance = PrefabUtility.InstantiatePrefab(prefabReference.gameObject); | ||
Undo.RegisterCreatedObjectUndo(instance, $"Instantiate {name}"); | ||
} | ||
|
||
private static bool HasActiveSceneAnyObjectOfType<T>() where T : Object => | ||
SceneObjectsFinder.FindObjectsOfType<T>(SceneManager.GetActiveScene(), true).Count > 0; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters