Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Text;
- /*********************************
- * FanLink.cs
- * by @S0phieH
- * https://vine.co/v/Mdej0H9zImJ
- *********************************/
- public class FanLink : MonoBehavior {
- //newsURL should be a link to a text file with the following format:
- // LINE1: News Title
- // Line2: News Image (needs to be PNG or JPG)
- // Line3: News Link (the website link that will open if the player asks)
- // Lines4+: text of the news, keep it short and to the point if you want it loading as fast as possible
- //
- // the text file (apart from it's links) can have standard unity markup so feel free to bold whatever
- public string newsURL = "";
- public TextMesh titleText;
- public TextMesh newsText;
- private string newstitle = "Loading news...";
- //private string newsContent = "...";
- private string imgstring = "";
- private string newslink = "";
- public Collider linkCollider;
- public Renderer imageRenderer;
- void Start() {
- LoadStuff();
- }
- void Update() {
- if (!string.IsNullOrEmpty(newslink) && Input.GetKeyDown("mouse 0")) {
- Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit mouseHit = new RaycastHit();
- if (linkCollider.Raycast(mouseRay, out mouseHit, 999f)) {
- Application.OpenURL(newslink);
- }
- }
- }
- //load the data
- void LoadStuff() {
- StartCoroutine(LoadStuffCoroutine());
- }
- IEnumerator LoadStuffCoroutine() {
- WWW www = new WWW(newsURL);
- yield return www;
- string[] loadedData = www.text.Split('\n');
- newstitle = loadedData[0];
- imgstring = loadedData[1];
- newslink = loadedData[2];
- LoadImage();
- var content = new StringBuilder();
- for (int i = 3; i < loadedData.Length; i++) {
- if (i < loadedData.Length - 1) {
- content.AppendLine(loadedData[i]);
- } else {
- content.Append(loadedData[i]);
- }
- }
- titleText.text = newstitle;
- newsText.text = content.ToString();
- }
- //load the image (if there is one)
- void LoadImage() {
- StartCoroutine(LoadImageCoroutine());
- }
- IEnumerator LoadImageCoroutine() {
- if (string.IsNullOrEmpty(imgstring)) {
- Debug.Log("Empty image URL!");
- } else {
- WWW www = new WWW(imgstring);
- yield return www;
- if (string.IsNullOrEmpty(www.error)) {
- Debug.Log(www.error);
- }
- imageRenderer.material.mainTexture = www.texture;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment