Skip to content

Instantly share code, notes, and snippets.

@hakanai
Created June 2, 2018 03:00
Show Gist options
  • Save hakanai/20c57f4512cee1ca9df33fd9cab1fa16 to your computer and use it in GitHub Desktop.
Save hakanai/20c57f4512cee1ca9df33fd9cab1fa16 to your computer and use it in GitHub Desktop.
VRC_SyncVideoPlayer updater script that runs in Unity. Currently very tied to loading my own database from YAML and updating a video player and a separate component with a list of videos in it, but modify as you wish.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Video;
using VRCSDK2;
using YamlDotNet.RepresentationModel;
public class VideoUpdater : MonoBehaviour
{
public VRC_SyncVideoPlayer VideoPlayer;
public GameObject RemoteControlVideoList;
public GameObject ListEntryPrefab;
void Start ()
{
}
void Update ()
{
}
public void UpdateVideos()
{
clearVideoPlayer();
clearSongList();
var entries = loadDatabase();
addToVideoPlayer(entries);
foreach (DatabaseEntry entry in entries)
{
addToSongList(entry);
}
}
void clearVideoPlayer()
{
VideoPlayer.Videos = new VRC_SyncVideoPlayer.VideoEntry[0];
}
void addToVideoPlayer(List<DatabaseEntry> entries)
{
var videoEntries = new List<VRC_SyncVideoPlayer.VideoEntry>(entries.Count);
foreach (DatabaseEntry entry in entries)
{
var videoEntry = new VRC_SyncVideoPlayer.VideoEntry();
videoEntry.URL = entry.MyURL;
videoEntry.AspectRatio = VideoAspectRatio.FitInside;
videoEntries.Add(videoEntry);
}
VideoPlayer.Videos = videoEntries.ToArray();
}
void clearSongList()
{
// Two passes because Transform's foreach is dodgy.
List<GameObject> toDestroy = new List<GameObject>();
foreach (Transform child in RemoteControlVideoList.transform)
{
toDestroy.Add(child.gameObject);
}
foreach (GameObject gameObject in toDestroy)
{
GameObject.DestroyImmediate(gameObject);
}
RectTransform listRectTransform = (RectTransform) RemoteControlVideoList.transform;
listRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
}
void addToSongList(DatabaseEntry entry)
{
RectTransform listRectTransform = (RectTransform) RemoteControlVideoList.transform;
Rect listRect = listRectTransform.rect;
GameObject newChild = Instantiate(ListEntryPrefab, listRectTransform);
RectTransform newChildTransform = (RectTransform) newChild.transform;
float listHeight = listRect.height;
Vector3 newChildPosition = newChildTransform.localPosition;
newChildPosition.y = -listHeight;
newChildTransform.localPosition = newChildPosition;
Text newChildText = (Text) newChild.GetComponentInChildren(typeof(Text));
newChildText.text = entry.Name;
float entryHeight = newChildTransform.rect.height;
listRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, listHeight + entryHeight);
}
List<DatabaseEntry> loadDatabase()
{
string file = Path.Combine(getHomeFolder(), "Google Drive/Lamaze/lamazedb.yml");
YamlSequenceNode entries;
using (StreamReader input = new StreamReader(file)) //todo wrong file
{
var yaml = new YamlStream();
yaml.Load(input);
entries = (YamlSequenceNode) yaml.Documents[0].RootNode;
}
List<DatabaseEntry> results = new List<DatabaseEntry>();
foreach (YamlNode untypedEntry in entries)
{
IDictionary<YamlNode, YamlNode> yamlEntry = ((YamlMappingNode) untypedEntry).Children;
DatabaseEntry entry = new DatabaseEntry();
if (yamlEntry.ContainsKey(new YamlScalarNode("DISABLED")) &&
((YamlScalarNode) yamlEntry[new YamlScalarNode("DISABLED")]).Value == "true")
{
continue;
}
entry.Name = ((YamlScalarNode) yamlEntry[new YamlScalarNode("name")]).Value;
if (yamlEntry.ContainsKey(new YamlScalarNode("my_url")))
{
entry.MyURL = ((YamlScalarNode) yamlEntry[new YamlScalarNode("my_url")]).Value;
}
else
{
continue;
}
results.Add(entry);
}
return results;
}
string getHomeFolder()
{
return (Environment.OSVersion.Platform == PlatformID.Unix ||
Environment.OSVersion.Platform == PlatformID.MacOSX)
? Environment.GetEnvironmentVariable("HOME")
: Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
}
class DatabaseEntry
{
public string Name { get; set; }
public string MyURL { get; set; }
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(VideoUpdater))]
public class VideoUpdaterEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
VideoUpdater script = (VideoUpdater) target;
if (GUILayout.Button("Update Videos"))
{
script.UpdateVideos();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment