-
Notifications
You must be signed in to change notification settings - Fork 329
FIX: Navigation not being confined to multiplayer playerRoot (case 1306361). #1443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e11a8bc
FIX: Navigation not being confined to multiplayer playerRoot (case 13…
f7ded76
MERGE: develop => fix-multiplayer-ui-navigation.
748d167
CHANGE: Remove CanvasGroup that we added automatically.
175b4f3
MERGE: develop => fix-multiplayer-ui-navigation.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,6 +1,6 @@ | ||
#if PACKAGE_DOCS_GENERATION || UNITY_INPUT_SYSTEM_ENABLE_UI | ||
using UnityEngine; | ||
using UnityEngine.EventSystems; | ||
using UnityEngine.InputSystem.Utilities; | ||
|
||
namespace UnityEngine.InputSystem.UI | ||
{ | ||
|
@@ -19,14 +19,92 @@ public class MultiplayerEventSystem : EventSystem | |
[Tooltip("If set, only process mouse events for any game objects which are children of this game object.")] | ||
[SerializeField] private GameObject m_PlayerRoot; | ||
|
||
/// <summary> | ||
/// The root object of the UI hierarchy that belongs to the given player. | ||
/// </summary> | ||
/// <remarks> | ||
/// This can either be an entire <c>Canvas</c> or just part of the hierarchy of | ||
/// a specific <c>Canvas</c>. | ||
/// | ||
/// Note that if the given <c>GameObject</c> has a <c>CanvasGroup</c> component on it, its | ||
/// <c>interactable</c> property will be toggled back and forth by <see cref="MultiplayerEventSystem"/>. | ||
/// If no such component exists on the <c>GameObject</c>, one will be added automatically. | ||
/// | ||
/// Only the <c>CanvasGroup</c> corresponding to the <see cref="MultiplayerEventSystem"/> that is currently | ||
/// executing its <see cref="Update"/> method (or did so last) will have <c>interactable</c> set to true. | ||
/// In other words, only the UI hierarchy corresponding to the player that is currently running a UI | ||
/// update (or that did so last) can be interacted with. | ||
/// </remarks> | ||
public GameObject playerRoot | ||
{ | ||
get => m_PlayerRoot; | ||
set => m_PlayerRoot = value; | ||
set | ||
{ | ||
m_PlayerRoot = value; | ||
InitializeCanvasGroup(); | ||
} | ||
} | ||
|
||
private CanvasGroup m_CanvasGroup; | ||
private bool m_CanvasGroupWasAddedByUs; | ||
|
||
private static int s_MultiplayerEventSystemCount; | ||
private static MultiplayerEventSystem[] s_MultiplayerEventSystems; | ||
|
||
protected override void OnEnable() | ||
{ | ||
base.OnEnable(); | ||
|
||
ArrayHelpers.AppendWithCapacity(ref s_MultiplayerEventSystems, ref s_MultiplayerEventSystemCount, this); | ||
|
||
InitializeCanvasGroup(); | ||
} | ||
|
||
private void InitializeCanvasGroup() | ||
{ | ||
if (m_PlayerRoot != null) | ||
{ | ||
m_CanvasGroup = m_PlayerRoot.GetComponent<CanvasGroup>(); | ||
if (m_CanvasGroup == null) | ||
{ | ||
m_CanvasGroup = m_PlayerRoot.AddComponent<CanvasGroup>(); | ||
m_CanvasGroupWasAddedByUs = true; | ||
} | ||
else | ||
m_CanvasGroupWasAddedByUs = false; | ||
} | ||
else | ||
{ | ||
m_CanvasGroup = null; | ||
} | ||
} | ||
|
||
protected override void OnDisable() | ||
{ | ||
var index = s_MultiplayerEventSystems.IndexOfReference(this); | ||
if (index != -1) | ||
s_MultiplayerEventSystems.EraseAtWithCapacity(ref s_MultiplayerEventSystemCount, index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case CanvasGroup have been added by OnEnable(), should OnDisable() remove it in that case for symmetry? The user didn't intentionally added it. |
||
|
||
if (m_CanvasGroupWasAddedByUs) | ||
Destroy(m_CanvasGroup); | ||
|
||
m_CanvasGroup = default; | ||
m_CanvasGroupWasAddedByUs = default; | ||
|
||
base.OnDisable(); | ||
} | ||
|
||
protected override void Update() | ||
{ | ||
for (var i = 0; i < s_MultiplayerEventSystemCount; ++i) | ||
{ | ||
var system = s_MultiplayerEventSystems[i]; | ||
if (system.m_PlayerRoot == null) | ||
continue; | ||
|
||
system.m_CanvasGroup.interactable = system == this; | ||
} | ||
|
||
var originalCurrent = current; | ||
current = this; // in order to avoid reimplementing half of the EventSystem class, just temporarily assign this EventSystem to be the globally current one | ||
try | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this add free some side-effects? I assume CanvasGroup has other implications? Reading through the docs I guess this could work just fine if "part of solution" for local multiplayer UI and having two "systems" isn't a viable solution in itself to create two navigation trees that are disjoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a range of implications and side-effects -- most importantly, it has pretty poor performance. This is more a workaround than a proper solution. Unfortunately, with the tools we have available ATM, it doesn't seem like there's a better solution for the moment. Overall, think this is part of some extensions to uGUI that are needed to support this use case properly. Most of
MultiplayerEventSystem
is a workaround.