Deozaan

FanLink.cs by S0phieH

Aug 8th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Text;
  4.  
  5. /*********************************
  6.  * FanLink.cs
  7.  * by @S0phieH
  8.  * https://vine.co/v/Mdej0H9zImJ
  9.  *********************************/
  10. public class FanLink : MonoBehavior {
  11.  
  12.     //newsURL should be a link to a text file with the following format:
  13.     // LINE1: News Title
  14.     // Line2: News Image (needs to be PNG or JPG)
  15.     // Line3: News Link (the website link that will open if the player asks)
  16.     // Lines4+: text of the news, keep it short and to the point if you want it loading as fast as possible
  17.     //
  18.     // the text file (apart from it's links) can have standard unity markup so feel free to bold whatever
  19.  
  20.     public string newsURL = "";
  21.  
  22.     public TextMesh titleText;
  23.     public TextMesh newsText;
  24.  
  25.     private string newstitle = "Loading news...";
  26.     //private string newsContent = "...";
  27.     private string imgstring = "";
  28.     private string newslink = "";
  29.  
  30.     public Collider linkCollider;
  31.  
  32.     public Renderer imageRenderer;
  33.  
  34.     void Start() {
  35.         LoadStuff();
  36.     }
  37.  
  38.  
  39.     void Update() {
  40.         if (!string.IsNullOrEmpty(newslink) && Input.GetKeyDown("mouse 0")) {
  41.             Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  42.             RaycastHit mouseHit = new RaycastHit();
  43.             if (linkCollider.Raycast(mouseRay, out mouseHit, 999f)) {
  44.                 Application.OpenURL(newslink);
  45.             }
  46.         }
  47.     }
  48.  
  49.     //load the data
  50.     void LoadStuff() {
  51.         StartCoroutine(LoadStuffCoroutine());
  52.     }
  53.  
  54.     IEnumerator LoadStuffCoroutine() {
  55.         WWW www = new WWW(newsURL);
  56.  
  57.         yield return www;
  58.  
  59.         string[] loadedData = www.text.Split('\n');
  60.         newstitle = loadedData[0];
  61.         imgstring = loadedData[1];
  62.         newslink = loadedData[2];
  63.  
  64.         LoadImage();
  65.  
  66.         var content = new StringBuilder();
  67.         for (int i = 3; i < loadedData.Length; i++) {
  68.             if (i < loadedData.Length - 1) {
  69.                 content.AppendLine(loadedData[i]);
  70.             } else {
  71.                 content.Append(loadedData[i]);
  72.             }
  73.         }
  74.  
  75.         titleText.text = newstitle;
  76.         newsText.text = content.ToString();
  77.  
  78.     }
  79.  
  80.     //load the image (if there is one)
  81.     void LoadImage() {
  82.         StartCoroutine(LoadImageCoroutine());
  83.     }
  84.  
  85.     IEnumerator LoadImageCoroutine() {
  86.         if (string.IsNullOrEmpty(imgstring)) {
  87.             Debug.Log("Empty image URL!");
  88.         } else {
  89.  
  90.             WWW www = new WWW(imgstring);
  91.  
  92.             yield return www;
  93.  
  94.             if (string.IsNullOrEmpty(www.error)) {
  95.                 Debug.Log(www.error);
  96.             }
  97.  
  98.             imageRenderer.material.mainTexture = www.texture;
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment