Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /*********************************
- * FanLink.cs
- * by @S0phieH
- * https://vine.co/v/Mdej0H9zImJ
- *********************************/
- public class FanLink : MonoBehaviour {
- //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(){
- StartCoroutine(LoadStufff());
- }
- void Update(){
- if (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
- IEnumerator LoadStufff () {
- WWW www = new WWW(newsURL);
- yield return www;
- string[] loadedData = www.text.Split('\n');
- newstitle = loadedData[0];
- imgstring = loadedData[1];
- newslink = loadedData[2];
- newsContent = "";
- for (int i=3; i<loadedData.Length; i++){
- newsContent += loadedData[i];
- if (i<loadedData.Length-1) newsContent += "\n";
- }
- titleText.text = newstitle;
- newsText.text = newsContent;
- StartCoroutine(LoadImage());
- }
- //load the image (if there is one)
- IEnumerator LoadImage () {
- if (imgstring == ""){
- Debug.Log("Empty image URL!");
- return false;
- }
- WWW www = new WWW(imgstring);
- yield return www;
- imageRenderer.material.mainTexture = www.texture;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment