-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBaseUI.cs
More file actions
146 lines (116 loc) · 3.41 KB
/
BaseUI.cs
File metadata and controls
146 lines (116 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Meta.XR.Samples;
using Meta.XR.MRUtilityKit;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;
using UnityEngine.UI;
[MetaCodeSample("SpaceSharing")]
public class BaseUI : MonoBehaviour
{
//
// Instance interface
public event UnityAction OnDisplayLobby
{
add
{
if (value is null)
return;
m_OnDisplayLobby.AddListener(value);
if (ShowingLobbyPanel)
value();
}
remove => m_OnDisplayLobby.RemoveListener(value);
}
public event UnityAction OnDisplayRoom
{
add
{
if (value is null)
return;
m_OnDisplayRoom.AddListener(value);
if (ShowingRoomPanel)
value();
}
remove => m_OnDisplayRoom.RemoveListener(value);
}
public bool ShowingRoomPanel => m_RoomPanel && m_RoomPanel.activeSelf;
public bool ShowingLobbyPanel => m_LobbyPanel && m_LobbyPanel.activeSelf;
public void DisplayRoomPanel()
{
if (!m_RoomPanel)
return;
if (m_LobbyPanel)
m_LobbyPanel.SetActive(false);
bool wasActive = m_RoomPanel.activeSelf;
m_RoomPanel.SetActive(true);
if (!wasActive)
m_OnDisplayRoom.Invoke();
}
public void DisplayLobbyPanel()
{
if (!m_LobbyPanel)
return;
if (m_RoomPanel)
m_RoomPanel.SetActive(false);
bool wasActive = m_LobbyPanel.activeSelf;
m_LobbyPanel.SetActive(true);
if (!wasActive)
m_OnDisplayLobby.Invoke();
}
//
// Serialized fields
[Header(nameof(BaseUI) + " (base)")]
[FormerlySerializedAs("referencePoint")]
[SerializeField]
protected Transform m_MenuAnchor;
[FormerlySerializedAs("menuPanel")]
[SerializeField]
protected GameObject m_RoomPanel;
[FormerlySerializedAs("lobbyPanel")]
[SerializeField]
protected GameObject m_LobbyPanel;
[Space]
[SerializeField]
protected UnityEvent m_OnDisplayLobby = new();
[SerializeField]
protected UnityEvent m_OnDisplayRoom = new();
//
// MonoBehaviour messages
protected virtual void OnValidate()
{
if (!m_MenuAnchor)
{
var find = GameObject.Find("Ref Point");
if (find)
m_MenuAnchor = find.transform;
}
if (gameObject.scene.IsValid() && !m_MenuAnchor) // avoids erroring in prefab view
{
Debug.LogError($"\"{name}\" seems to be improperly set-up. (no anchor for canvas)", this);
}
}
protected virtual IEnumerator Start()
{
transform.parent = m_MenuAnchor;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
DisplayLobbyPanel();
// lazy state init for MRUK toggles
var find = transform.FindChildRecursive("Toggle: MRUK World Locking");
if (find && find.TryGetComponent<Toggle>(out var toggle))
{
if (MRUK.Instance)
{
toggle.interactable = true;
toggle.SetIsOnWithoutNotify(MRUK.Instance.EnableWorldLock);
}
else
{
toggle.gameObject.SetActive(false);
}
}
yield break;
}
}