// Update is called once per frame void Update() { // Debug.Log("CTchartOptions showMenu: " + showMenu + ", localControl: "+ctclient.isLocalControl()); if (ctclient != null && ctclient.enabled && !ctclient.isLocalControl()) { showMenu = false; // only local-control objects set menu } foreach (Transform child in transform) { if (child.name == "ChartCanvas") { child.gameObject.SetActive(showMenu); } } // keep from moving when typing if (rbc != null) { if (showMenu) { rbc.constraints = RigidbodyConstraints.FreezeAll; } else { rbc.constraints = RigidbodyConstraints.None; } } }
//---------------------------------------------------------------------------------------------------------------- // Update is called once per frame void Update() { if (ctplayer.isLocalControl()) { Quaternion targetRotation = Quaternion.Euler(new Vector3(15, 30, 45)) * transform.rotation; transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * 1F); if (transform.localScale.x < 0.1F) { sizeFactor = 1.001F; } else if (transform.localScale.x > 0.5F) { sizeFactor = 0.999F; } transform.localScale *= sizeFactor; } // transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime); }
IEnumerator getData() { float ymax = -10000F; // force first-pass init float ymin = 10000F; while (true) { yield return(new WaitForSeconds(ctunity.pollInterval)); if (chartOptions == null || chartOptions.showMenu) { continue; } // Debug.Log("CTchart chartOptions.showMenu: " + chartOptions.showMenu); string urlparams = "?f=d"; // drop this to get time,data pairs... // two channels = two HTTP GETs // WWW www1=null; // WWW www2=null; // notta if (Chan1.Length == 0) { lineR1.positionCount = 0; yield return(null); } if (Chan2.Length == 0 || Mode == "CrossPlot") { lineR2.positionCount = 0; } string url1 = "", url2 = ""; if (ctclient != null && ctclient.enabled && ctclient.link != null && !ctclient.isLocalControl()) // if (ctunity.isReplayMode()) { if (ctclient.link.Equals(oldCustom)) { continue; } string[] customparts = ctclient.link.Split(','); url1 = customparts[0]; if (customparts.Length > 1) { url2 = customparts[1]; numChan = 2; } else { numChan = 1; } oldCustom = ctclient.link; } else { // figure out xplot and chart1/2 situation if (Chan2.Length > 0) { numChan = 2; } else { numChan = 1; } // urlparams += "&t=" + (ctunity.ServerTime() - Duration) + "&d=" + Duration; urlparams += "&t=" + (ctunity.replayTime - Duration) + "&d=" + Duration; // live or replay url1 = Server + "/CT/" + Source + "/" + Chan1 + urlparams; url2 = Server + "/CT/" + Source + "/" + Chan2 + urlparams; oldCustom = ""; } // fetch data UnityWebRequest www1 = null, www2 = null; try { www1 = UnityWebRequest.Get(url1); www1.SetRequestHeader("AUTHORIZATION", ctunity.CTauthorization()); // www1 = new WWW(url1); if (numChan > 1) { www2 = UnityWebRequest.Get(url1); www2.SetRequestHeader("AUTHORIZATION", ctunity.CTauthorization()); // www2 = new WWW(url2); if (ctclient != null) { ctclient.link = url1 + "," + url2; } } else { if (ctclient != null) { ctclient.link = url1; } } } catch (Exception e) { Debug.Log("CTchart Exception on WWW fetch: " + e); continue; } // yield return www1; yield return(www1.SendWebRequest()); // if (numChan > 1) yield return www2; if (numChan > 1) { yield return(www2.SendWebRequest()); } // Debug.Log("CTchart url1: " + url1); if (!string.IsNullOrEmpty(www1.error)) { Debug.Log("www1.error: " + www1.error + ", url1: " + url1); } else { try { // fetch time-interval info from header (vs timestamps) // Dictionary<string,string> whead = www1.responseHeaders; Dictionary <string, string> whead = www1.GetResponseHeaders(); double htime = 0, hdur = 0; try { if (whead.ContainsKey("time")) { htime = double.Parse(whead ["time"]); } if (whead.ContainsKey("duration")) { hdur = double.Parse(whead ["duration"]); } } catch (Exception) { Debug.Log("Exception on htime parse!"); } // parse data into value queues // string[] xvals = www1.text.Split ('\n'); string[] xvals = www1.downloadHandler.text.Split('\n'); string[] yvals = null; // if (numChan > 1) yvals = www2.text.Split('\n'); if (numChan > 1) { yvals = www2.downloadHandler.text.Split('\n'); } double ptsPerSec = xvals.Length / hdur; // deduce queue size from apparent sample rate MaxPts = (int)(Duration * ptsPerSec); // Debug.Log("xvals.len: " + xvals.Length); if (Mode == "CrossPlot") // cross plots presume data scaled 0-1 coming in { // yvals = www2.text.Split('\n'); yvals = www2.downloadHandler.text.Split('\n'); int maxCount = Math.Min(xvals.Length, yvals.Length); p1 = new Vector3[maxCount]; p2 = null; Ngot = 0; for (int i = 0; i < maxCount; i++) { try { float xv = float.Parse(xvals[i]) - 0.5f; float yv = float.Parse(yvals[i]) - 0.5f; p1[Ngot] = new Vector3(xv, yv, -1f); } catch (Exception) {}; Ngot++; } lineR1.positionCount = Ngot - 2; // why ratty end??? lineR1.SetPositions(p1); } else // stripchart { int maxCount = xvals.Length; p1 = new Vector3[maxCount]; if (numChan > 1) { // yvals = www2.text.Split('\n'); yvals = www2.downloadHandler.text.Split('\n'); maxCount = Math.Min(xvals.Length, yvals.Length); p2 = new Vector3[maxCount]; } else { p2 = null; } float x1 = -0.5f; float dx = 1.0f / (maxCount - 1); Ngot = 0; for (int i = 0; i < maxCount; i++) { try { float xv = float.Parse(xvals[i]) - 0.5f; // if (numChan == 1) xv = float.Parse(xvals[i]) / 65536.0f; // cluge: audio scaling p1[Ngot] = new Vector3(x1, xv, -0.8f); if (xv > ymax) { ymax = xv; } if (xv < ymin) { ymin = xv; } if (numChan > 1) { float yv = float.Parse(yvals[i]) - 0.5f; p2[Ngot] = new Vector3(x1, yv, -1f); if (yv > ymax) { ymax = yv; } if (yv < ymin) { ymin = yv; } } } catch (Exception) { x1 += dx; } Ngot++; x1 += dx; } if (autoScale) // scale to nominal +/- 0.5F range { p1 = doScale(p1, ymin, ymax); if (numChan > 1) { p2 = doScale(p2, ymin, ymax); } } lineR1.positionCount = lineR2.positionCount = Ngot - 2; // why ratty end??? lineR1.SetPositions(p1); if (numChan > 1) { lineR2.SetPositions(p2); } } } catch (FormatException) { Debug.Log("Error parsing values! " + www1.downloadHandler.text); } } // www1.Dispose (); // www1 = null; // if (numChan > 1) { // www2.Dispose (); // www2 = null; // } } }
IEnumerator DownloadImage() { while (true) { float pollInterval = (ctunity == null) ? 0.1F : ctunity.pollInterval; // wait for ctunity init yield return(new WaitForSeconds(pollInterval)); if (showImage) { if (ctclient != null && ctclient.enabled && ctclient.link != null && !ctclient.isLocalControl()) { // remote control if (ctclient.link.Equals("")) { continue; } if (ctclient.link.Equals(oldCustom)) { continue; } url = ctclient.link; oldCustom = ctclient.link; } else // local control { // url = ctunity.Server + "/CT/"+ctunity.Session+"/Video/" + ctunity.Player + "/webcam.jpg"; url = ctunity.Server + "/CT/" + ctunity.Session + "/ScreenCap/" + ctunity.Player + "/screen.jpg"; url = url + "?t=" + ctunity.replayTime; // live or replay oldCustom = ""; } // Debug.Log("CTvideo, url: " + url); UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); www.SetRequestHeader("AUTHORIZATION", ctunity.CTauthorization()); yield return(www.SendWebRequest()); /* * WWW www; * try * { * www = new WWW(url); * } catch (Exception e) { * UnityEngine.Debug.Log("CTvideo exception: " + url+", Exception: "+e); * continue; * } * yield return www; */ if (ctclient != null) { ctclient.link = url; // Debug.Log("ctclient: " + ctclient.name + ", url: " + url); } Texture2D tex = DownloadHandlerTexture.GetContent(www); // Texture2D tex = new Texture2D (www.texture.width, www.texture.height, TextureFormat.DXT1, false); // www.LoadImageIntoTexture (tex); GetComponent <Renderer> ().material.mainTexture = tex; // www.Dispose (); // www = null; } else { GetComponent <Renderer> ().material.mainTexture = startTexture; } } }