You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.6 KiB
145 lines
3.6 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Video;
|
|
using OscJack;
|
|
|
|
|
|
public class OscHandler : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField]
|
|
public CanvasGroup intro;
|
|
|
|
[SerializeField]
|
|
public CanvasGroup main;
|
|
|
|
[SerializeField]
|
|
public VideoPlayer videoPlayer;
|
|
|
|
[SerializeField]
|
|
public Image mask;
|
|
|
|
[SerializeField]
|
|
OscConnection _connection = null;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
setupOsc();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void setupOsc()
|
|
{
|
|
Debug.Log("Setting up OSC server...");
|
|
|
|
var port = _connection?.port ?? 0;
|
|
var server = OscMaster.GetSharedServer(port);
|
|
|
|
// if(server.IsRunning){
|
|
server.MessageDispatcher.AddCallback("/reset",
|
|
(string address, OscDataHandle data) => {
|
|
|
|
onReset();
|
|
|
|
}
|
|
);
|
|
server.MessageDispatcher.AddCallback("/go",
|
|
(string address, OscDataHandle data) => {
|
|
Debug.Log("/go message received");
|
|
|
|
videoPlayer.Stop();
|
|
videoPlayer.time = 0;
|
|
videoPlayer.Play();
|
|
videoPlayer.loopPointReached += (VideoPlayer vp) => {
|
|
|
|
StartCoroutine(FadeCanvasGroup(intro, 1, 0, 1f));
|
|
StartCoroutine(FadeCanvasGroup(main, 0, 1, 1f));
|
|
|
|
};
|
|
|
|
}
|
|
);
|
|
server.MessageDispatcher.AddCallback("/inputTime",
|
|
(string address, OscDataHandle data) => {
|
|
|
|
float duration = data.GetElementAsFloat(0);
|
|
Debug.Log("/inputTime message received"+duration);
|
|
onInputTime(duration);
|
|
|
|
}
|
|
);
|
|
|
|
server.MessageDispatcher.AddCallback("/end",
|
|
(string address, OscDataHandle data) => {
|
|
|
|
Debug.Log("/end message received");
|
|
;
|
|
StartCoroutine(FadeCanvasGroup(main, 1, 0, 1f));
|
|
|
|
}
|
|
);
|
|
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
private IEnumerator FadeCanvasGroup(CanvasGroup canvasGroup, float startAlpha, float endAlpha, float duration)
|
|
{
|
|
canvasGroup.alpha = startAlpha;
|
|
float elapsedTime = 0f;
|
|
|
|
while (elapsedTime < duration)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
canvasGroup.alpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / duration);
|
|
yield return null;
|
|
}
|
|
|
|
canvasGroup.alpha = endAlpha;
|
|
}
|
|
|
|
public void onInputTime(float duration)
|
|
{
|
|
Debug.Log("Start decreasing fill over time: " + duration);
|
|
|
|
if (mask != null)
|
|
{
|
|
StopCoroutine("DecreaseFill");
|
|
StartCoroutine(DecreaseFill(duration));
|
|
}
|
|
}
|
|
|
|
private IEnumerator DecreaseFill(float duration)
|
|
{
|
|
float elapsed = 0f;
|
|
mask.fillAmount = 1f;
|
|
|
|
while (elapsed < duration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
mask.fillAmount = Mathf.Lerp(1f, 0f, elapsed / duration);
|
|
yield return null;
|
|
}
|
|
|
|
mask.fillAmount = 0f;
|
|
}
|
|
|
|
public void onReset()
|
|
{
|
|
Debug.Log("/reset message received");
|
|
|
|
videoPlayer.Stop();
|
|
intro.alpha = 0f;
|
|
main.alpha = 0f;
|
|
}
|
|
}
|
|
|