forked from RenderHeads/KlakNDI
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SourceSelector.cs
57 lines (45 loc) · 1.46 KB
/
SourceSelector.cs
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
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using Klak.Ndi;
public class SourceSelector : MonoBehaviour
{
[SerializeField] Dropdown _dropdown = null;
NdiReceiver _receiver;
List<string> _sourceNames;
bool _disableCallback;
// HACK: Assuming that the dropdown has more than
// three child objects only while it's opened.
bool IsOpened => _dropdown.transform.childCount > 3;
void Start() => _receiver = GetComponent<NdiReceiver>();
void Update()
{
// Do nothing if the menu is opened.
if (IsOpened) return;
// NDI source name retrieval
_sourceNames = NdiFinder.sourceNames.ToList();
// Currect selection
var index = _sourceNames.IndexOf(_receiver.ndiName);
// Append the current name to the list if it's not found.
if (index < 0)
{
index = _sourceNames.Count;
_sourceNames.Add(_receiver.ndiName);
}
// Disable the callback while updating the menu options.
_disableCallback = true;
// Menu option update
_dropdown.ClearOptions();
_dropdown.AddOptions(_sourceNames);
_dropdown.value = index;
_dropdown.RefreshShownValue();
// Resume the callback.
_disableCallback = false;
}
public void OnChangeValue(int value)
{
if (_disableCallback) return;
_receiver.ndiName = _sourceNames[value];
}
}