Skip to content

Commit

Permalink
feat: Add ElympicsLobby to "Add GameObject" context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawid Sygocki committed Aug 28, 2023
1 parent 3b5938f commit 624214b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
48 changes: 42 additions & 6 deletions Editor/ElympicsMouseActions.cs
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;
}
}

1 change: 1 addition & 0 deletions Runtime/ElympicsSystems/ElympicsLobbyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Elympics
{
[RequireComponent(typeof(AsyncEventsDispatcher))]
public class ElympicsLobbyClient : MonoBehaviour
{
public static ElympicsLobbyClient Instance { get; private set; }
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions Runtime/Tools/ElympicsEditorMenuPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ namespace Elympics
{
public static class ElympicsEditorMenuPaths
{
public const string TOOLS_MENU_PATH = "Tools/Elympics/";
private const string TOOLS_MENU_PATH = "Tools/Elympics/";
public const string RESET_IDS_MENU_PATH = TOOLS_MENU_PATH + "Reset Network Ids";
public const string SETUP_MENU_PATH = TOOLS_MENU_PATH + "Select or Create Config";
public const string BUILD_WINDOWS_SERVER = TOOLS_MENU_PATH + "Build Windows Server";
public const string BUILD_LINUX_SERVER = TOOLS_MENU_PATH + "Build Linux Server";
public const string BUILD_AND_UPLOAD_SERVER = TOOLS_MENU_PATH + "Build and Upload Server";
public const string MANAGE_GAMES_IN_ELYMPICS = TOOLS_MENU_PATH + "Manage games in Elympics";

public const string MOUSE_ACTION_CREATE_ELYMPICS_SYSTEM = "GameObject/ElympicsSystem";
private const string GAMEOBJECT_MENU_PATH = "GameObject/Elympics/";
public const string MOUSE_ACTION_CREATE_ELYMPICS_SYSTEM = GAMEOBJECT_MENU_PATH + "ElympicsSystem";
public const string MOUSE_ACTION_CREATE_ELYMPICS_LOBBY = GAMEOBJECT_MENU_PATH + "ElympicsLobby";
}
}

0 comments on commit 624214b

Please sign in to comment.