forked from VisualPinball/VisualPinball.Engine.Mpf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnableDuringMode.cs
More file actions
68 lines (58 loc) · 2.37 KB
/
EnableDuringMode.cs
File metadata and controls
68 lines (58 loc) · 2.37 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
// Visual Pinball Engine
// Copyright (C) 2025 freezy and VPE Team
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using UnityEngine;
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages.Mode;
using Logger = NLog.Logger;
namespace VisualPinball.Engine.Mpf.Unity.MediaController.ObjectToggle
{
/// <summary>
/// Enable the parent game object while the specified mode is active in MPF.
/// </summary>
[AddComponentMenu("Pinball/MPF Media Controller/Enable During MPF Mode")]
[DisallowMultipleComponent]
public class EnableDuringMode : MonoBehaviour
{
[SerializeField] private string _mode;
private ModeMonitor _modeMonitor;
private static Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private void Awake()
{
if (string.IsNullOrWhiteSpace(_mode))
{
Logger.Warn(
"No MPF mode is specified. The component 'Enable During MPF Mode' on game object"
+ $" '{gameObject.name}' won't do anything.");
}
if (!MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface)) return;
_modeMonitor = new ModeMonitor(bcpInterface, _mode);
_modeMonitor.IsModeActiveChanged += OnModeActiveChanged;
}
private void Start()
{
// This is done in Start to give other components like this one attached to children of this game object a
// chance to run their Awake functions.
if (_modeMonitor != null)
gameObject.SetActive(false);
}
private void OnDestroy()
{
if (_modeMonitor != null)
{
_modeMonitor.IsModeActiveChanged -= OnModeActiveChanged;
_modeMonitor.Dispose();
}
}
private void OnModeActiveChanged(object sender, bool isActive)
{
gameObject.SetActive(isActive);
}
}
}