Guest User

FanLink.cs by @S0phieH

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