SHOW:
|
|
- or go back to the newest paste.
| 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 | - | public class FanLink : MonoBehaviour {
|
| 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 newsContent = "..."; |
| 25 | + | |
| 26 | //private string newsContent = "..."; | |
| 27 | private string imgstring = ""; | |
| 28 | private string newslink = ""; | |
| 29 | ||
| 30 | public Collider linkCollider; | |
| 31 | ||
| 32 | public Renderer imageRenderer; | |
| 33 | - | void Start(){
|
| 33 | + | |
| 34 | - | StartCoroutine(LoadStufff()); |
| 34 | + | void Start() {
|
| 35 | LoadStuff(); | |
| 36 | } | |
| 37 | ||
| 38 | - | void Update(){
|
| 38 | + | |
| 39 | - | if (newslink != "" && Input.GetKeyDown("mouse 0")){
|
| 39 | + | void Update() {
|
| 40 | if (!string.IsNullOrEmpty(newslink) && Input.GetKeyDown("mouse 0")) {
| |
| 41 | Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); | |
| 42 | - | if (linkCollider.Raycast(mouseRay, out mouseHit, 999f)){
|
| 42 | + | |
| 43 | if (linkCollider.Raycast(mouseRay, out mouseHit, 999f)) {
| |
| 44 | Application.OpenURL(newslink); | |
| 45 | } | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | //load the data | |
| 50 | - | IEnumerator LoadStufff () {
|
| 50 | + | void LoadStuff() {
|
| 51 | StartCoroutine(LoadStuffCoroutine()); | |
| 52 | - | |
| 52 | + | |
| 53 | ||
| 54 | - | |
| 54 | + | IEnumerator LoadStuffCoroutine() {
|
| 55 | WWW www = new WWW(newsURL); | |
| 56 | ||
| 57 | yield return www; | |
| 58 | ||
| 59 | - | |
| 59 | + | |
| 60 | - | newsContent = ""; |
| 60 | + | |
| 61 | - | for (int i=3; i<loadedData.Length; i++){
|
| 61 | + | |
| 62 | - | newsContent += loadedData[i]; |
| 62 | + | |
| 63 | - | if (i<loadedData.Length-1) newsContent += "\n"; |
| 63 | + | |
| 64 | LoadImage(); | |
| 65 | ||
| 66 | var content = new StringBuilder(); | |
| 67 | - | newsText.text = newsContent; |
| 67 | + | for (int i = 3; i < loadedData.Length; i++) {
|
| 68 | if (i < loadedData.Length - 1) {
| |
| 69 | - | StartCoroutine(LoadImage()); |
| 69 | + | content.AppendLine(loadedData[i]); |
| 70 | } else {
| |
| 71 | content.Append(loadedData[i]); | |
| 72 | } | |
| 73 | - | IEnumerator LoadImage () {
|
| 73 | + | |
| 74 | - | if (imgstring == ""){
|
| 74 | + | |
| 75 | titleText.text = newstitle; | |
| 76 | - | return false; |
| 76 | + | newsText.text = content.ToString(); |
| 77 | ||
| 78 | } | |
| 79 | - | WWW www = new WWW(imgstring); |
| 79 | + | |
| 80 | - | |
| 80 | + | |
| 81 | void LoadImage() {
| |
| 82 | StartCoroutine(LoadImageCoroutine()); | |
| 83 | - | imageRenderer.material.mainTexture = www.texture; |
| 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 | } |