Created
April 3, 2018 19:02
-
-
Save jgiovanni/cbe432dbd56e069076bee680b358996b to your computer and use it in GitHub Desktop.
Raycaster snippet used for Sky Lantern Festival Project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
using TMPro; | |
using Twity.DataModels.Core; | |
using Twity.DataModels.Responses; | |
public class LanternIdentifier : MonoBehaviour | |
{ | |
private Camera cam; | |
private TextMeshProUGUI TMPGUI; | |
private GameObject Lanterns; | |
private string captionFadeState = "none"; | |
public bool enableCaptions = true; | |
public float timeAlive = 7.0f; | |
private float currentTimeAlive; | |
public float timeToFade = 1.0f; | |
private Tweet[] backupTweets; | |
private string backupDataFileName = "data.json"; | |
public bool showCrosshair = false; | |
// Use this for initialization | |
void Start () | |
{ | |
// load fallback data | |
LoadBackupData(); | |
cam = this.GetComponent<Camera> (); | |
TMPGUI = GameObject.Find ("GUI Text").GetComponent<TextMeshProUGUI> (); | |
TMPGUI.canvasRenderer.SetAlpha (0f); | |
Lanterns = GameObject.Find ("Lanterns").gameObject; | |
currentTimeAlive = timeAlive; | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
// enable/disable captions | |
if (OVRInput.GetDown(OVRInput.Button.One)) { | |
enableCaptions = !enableCaptions; | |
Debug.Log ("Captions " + (enableCaptions ? "Enabled!" : "Disabled!")); | |
} | |
// toggle crosshair visibility | |
if (OVRInput.GetDown(OVRInput.Button.Two)) | |
showCrosshair = !showCrosshair; | |
// Check if captions are enabled | |
if (enableCaptions) { | |
switch (captionFadeState) { | |
case "in": | |
currentTimeAlive -= Time.deltaTime; | |
TMPGUI.CrossFadeAlpha (1f, timeToFade, false); | |
if (currentTimeAlive <= timeToFade) { | |
captionFadeState = "out"; | |
} | |
break; | |
case "out": | |
currentTimeAlive -= Time.deltaTime; | |
TMPGUI.CrossFadeAlpha (0f, timeToFade, false); | |
if (currentTimeAlive <= 0) { | |
captionFadeState = "none"; | |
currentTimeAlive = timeAlive; | |
TMPGUI.SetText (""); | |
} | |
break; | |
default: | |
Ray ray = cam.ViewportPointToRay (new Vector3 (.5f, .5f)); | |
RaycastHit hit; | |
// This is a bitwise operation that makes layer 8 the selected layer | |
// layer 8 is where the lanterns are. | |
int layerMask = 1 << 8; | |
if (Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask)) { | |
// Get tweets form Lanterns GameObject | |
if (Lanterns.GetComponent<GenerateLanterns>().tweets.Length > 0) | |
{ | |
var tweets = new List<Tweet>(Lanterns.GetComponent<GenerateLanterns>().tweets); | |
int tCount = tweets.Count; | |
int randIndex = Random.Range(0, tCount - 1); | |
Tweet someTweet = tweets[randIndex]; | |
ShowTweet(someTweet); | |
// remove tweet from array | |
tweets.RemoveAt(randIndex); | |
Tweet[] tweetsArr = tweets.ToArray(); | |
Lanterns.GetComponent<GenerateLanterns>().tweets = tweetsArr; | |
} else { | |
var tweets = new List<Tweet>(backupTweets); | |
int tCount = tweets.Count; | |
int randIndex = Random.Range(0, tCount - 1); | |
Tweet someTweet = tweets[randIndex]; | |
ShowTweet(someTweet); | |
} | |
} | |
break; | |
} | |
} else { | |
TMPGUI.canvasRenderer.SetAlpha(0f); | |
captionFadeState = "none"; | |
currentTimeAlive = timeAlive; | |
TMPGUI.SetText(""); | |
} | |
} | |
void ShowTweet (Tweet tweet) | |
{ | |
string tHandle = tweet.user.screen_name; | |
int maxChars = 105 - tHandle.Length; | |
string text = (tweet.text.Length > (maxChars) ? tweet.text.Substring (0, maxChars) + "..." : tweet.text) + " @" + tHandle; | |
TMPGUI.SetText (text); | |
captionFadeState = "in"; | |
} | |
void OnGUI () | |
{ | |
if (showCrosshair) | |
{ | |
GUI.color = new Color(255, 255, 255, 1f); | |
GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 10, 10), ""); | |
} | |
} | |
private void LoadBackupData() | |
{ | |
string filePath = Path.Combine (Application.streamingAssetsPath, backupDataFileName); | |
if (File.Exists(filePath)) | |
{ | |
string dataAsJson = File.ReadAllText(filePath); | |
SearchTweetsResponse Response = JsonUtility.FromJson<SearchTweetsResponse>(dataAsJson); | |
backupTweets = Response.statuses; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment