Skip to content

Instantly share code, notes, and snippets.

@jgiovanni
Created April 3, 2018 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgiovanni/cbe432dbd56e069076bee680b358996b to your computer and use it in GitHub Desktop.
Save jgiovanni/cbe432dbd56e069076bee680b358996b to your computer and use it in GitHub Desktop.
Raycaster snippet used for Sky Lantern Festival Project
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