NetworkUser.cs 2.44 KB
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.Networking;

public class NetworkUser : NetworkBehaviour
{
    public Transform buttonParent;
    public MoviePlayNode movieButtonNode;

    void Start()
    {
        if (!isServer)
        {
            AppManager.Instance.DestroyServerCanvas();
            StartCoroutine(WaitForFiles());
        }
        else
        {
            AppManager.Instance.Initialize();

            string path = Application.streamingAssetsPath + "/videoSettings.json";
            if (!File.Exists(path))
            {
                Debug.LogError("NetworkUser Error: No videoSettings.json file found");
                return;
            }
            string json = File.ReadAllText(path);
            RpcGetFiles(json);
        }
    }

    public List<Dictionary<string, string>> _files;

    private IEnumerator WaitForFiles()
    {
        while (_files == null)
            yield return null;
        
        foreach(var dict in _files)
        {
            MoviePlayNode mpn = Instantiate<MoviePlayNode>(movieButtonNode);
            mpn.Initialize(dict["title"], dict["filename"], this);
            mpn.transform.SetParent(buttonParent);
            mpn.transform.localPosition = Vector3.zero;
            mpn.transform.localScale = Vector3.one;
        }
        // Do things
    }

    [ClientRpc]
    private void RpcGetFiles(string json)
    {
        var tempList = (IList)MiniJSON.Json.Deserialize(json);

        _files = new List<Dictionary<string, string>>();
        
        foreach (IDictionary dict in tempList)
        {
            var thisDict = new Dictionary<string, string>();
            foreach(DictionaryEntry de in dict)
                thisDict.Add(de.Key.ToString(), de.Value.ToString());

            _files.Add(thisDict);
        }
    }
	
	void Update ()
    {
        if (Input.touchCount > 0)
            CmdResetTimer();

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            var thisDict = _files[0];
            CmdLoadVideo(thisDict["filename"]);
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            var thisDict = _files[1];
            CmdLoadVideo(thisDict["filename"]);
        }
    }

    [Command]
    private void CmdResetTimer()
    {
        AppManager.Instance.ResetTimer();
    }

    [Command]
    public void CmdLoadVideo(string file)
    {
        AppManager.Instance.LoadMovie(file);
    }
}