FeedMe

From Unify Community Wiki
Revision as of 14:34, 8 July 2010 by CorruptedHeart (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author: Corrupted Heart, CorruptedHeart

I thought I should put some of my scripts on here since it provides a more centralized area for them than on a blog who knows where.

Info

FeedMe is a simple XML displayer that utilizes TinyXmlReader to parse the document which is downloaded from a server. FeedMeSample.png

FeedMe.cs

<csharp>//<author>Garth "Corrupted Heart" de Wet</author> //<email>mydeathofme[at]gmail[dot]com</email> //<summary>Contains methods in order to get feeds from an xml document located on a server</summary> //<license>Creative Commons: //http://creativecommons.org/licenses/by/3.0/ //FeedMe by Garth "Corrupted Heart" de Wet is licensed under a Creative Commons Attribution 3.0 Unported License. //Permissions beyond the scope of this license may be available at www.CorruptedHeart.co.cc //</license> using UnityEngine; using System.Collections;   public class FeedMe : MonoBehaviour {

  1. region Variables
  2. region URLS

    // The URL to where the feed is located     public string getFeedURL = "http://localhost/feed.xml";

  1. endregion

    // A Struc of what every feed is made up of     private struct feedThis     {                 public string Title;             public string Message;         public string Date;         public string Author;     }

  1. region Feed Variables

    // Should the feed only update once per game play     public bool updateOnce = false;     // How long till the next update     public int minutesToUpdate = 10;     // This is used to make sure that the text loaded doesn't fill more than the array     private int feedLength = 9;     // This is the array of a struc of variables used to hold the feeds     private feedThis[] theFeed = new feedThis[9];     // When the feed is updated the time is set here plus the minutes to update     private float timeSinceLastUpdate = 0;     // if the feed must be updated     private bool updateFeed = true;     // if the feed is updating     private bool updating = false;     // if there is no errors from the WWW connection     private bool errorFree = true;     // used to measure the number of elements within the array     private int i = 0;

  1. endregion

 

  1. region GUI

    // Skin for gui     public GUISkin mySkin;     // Keeps position of the scroll position     private Vector2 scrollPosition;     // The size and position of the feed window - The first two fields are the only ones required     public Rect feedWinRect = new Rect(0,30,250,50);     // the actual width of the feed window     public int winWidth = 250;     // the size of the feed button     public int btnWidth = 250;     // whether the feed window is being displayed or not     private bool showFeed = false;     // The icon to display on the feed button     public Texture2D feedIcon;     // The icon to display on the reload button     public Texture2D reloadIcon;

  1. endregion
  2. endregion

     

  1. region Standard Methods

    void Start()     {         // Starts the getFeed Coroutine         StartCoroutine(getFeed());     }           void Update()     {         // If it is set to update only once then it skips the next piece         if(!updateOnce)         {             // Checks whether the feed needs to be updated             if((Time.time >= timeSinceLastUpdate) && (!updateFeed))             {                 // Resets the vars                 updateFeed = true;                 i = 0;                 // Starts the getFeed Coroutine                 StartCoroutine(getFeed());             }         }     }       void OnGUI()     {         // creates an empty string for the feed button title         string Feed = "";         // If there was a skin set then set it else leave it as default         if(mySkin != null)         {             GUI.skin = mySkin;         }         //To show that the feed is updating         if(updating)         {             Feed =  "Is updating...";         }         // if the feed isn't updating show the latest feed title as button caption         else         {             Feed = theFeed[0].Title;         }         // Sets an icon and the feed string to a GUIContent         GUIContent feedBtn = new GUIContent("   " + Feed, feedIcon);         // Button for showing the field         if(GUI.Button(new Rect(0,0,btnWidth, 30),feedBtn))         {             // On click it simply switches the feed window on and off             showFeed = !showFeed;         }               // Shows the feed window based upon the value of showFeed         if(showFeed)         {             feedWinRect = GUILayout.Window(1, feedWinRect, theFeedWindow, "Feeds", GUILayout.Width(winWidth));         }         // An update now button         if(GUI.Button(new Rect(btnWidth, 0, 30, 30), reloadIcon))         {             // Resets the vars             updateFeed = true;             i = 0;             // Starts the getFeed Coroutine             StartCoroutine(getFeed());         }     }

  1. endregion
  2. region Custom Functions

    IEnumerator getFeed()     {         // if the feed must be updated run the function         if(updateFeed)         {             // Sets updating to true             updating = true;             // Gets the data from the url set in the inspector                     WWW feedPost = new WWW(getFeedURL);             // Returns control to rest of program until it is finished downloading                     yield return feedPost;             // if there are no errors runs through the function             if(feedPost.error == null)             {                 // Creates a instance of TinyXmlReader to loop through the xml document                 // loops through the document and extracts data from each <feed></feed>                 // For my use, it extracts the title, date, author and message.                 // it stores this in an array of struc type declared above                 TinyXmlReader reader = new TinyXmlReader(feedPost.data);                 while(reader.Read("Feeds") && (i < feedLength))                 {                     while(reader.Read("Feed"))                     {                         if(reader.tagName == "Title" && reader.isOpeningTag)                         {                             theFeed[i].Title = reader.content;                         }                         if(reader.tagName == "Date" && reader.isOpeningTag)                         {                             theFeed[i].Date = reader.content;                         }                     if(reader.tagName == "Author" && reader.isOpeningTag)                         {                             theFeed[i].Author = reader.content;                         }                         if(reader.tagName == "Message" && reader.isOpeningTag)                         {                             theFeed[i].Message = reader.content;                         }                     }                     // Increments the counter variable for the array                     i++;                 }             }             // if there was an error             else             {                 // sets all the error messages                 errorFree = false;                 theFeed[0].Title = "Error connecting to Feed...";                 theFeed[0].Message = feedPost.error;                 // Logs an error                 Debug.LogError(feedPost.error);             }             // sets the values of the variables to there off state             updateFeed = false;             updating = false;             // sets the last update time             timeSinceLastUpdate = Time.time + (60 * minutesToUpdate);             // Destroys the feedPost...             feedPost.Dispose();         }     }

  1. endregion
  2. region GUI Functions

    // The feed window GUI     void theFeedWindow(int WindowID)     {         // Creates a empty string to hold all the feeds         string Feeds = "";         // Starts a scroll view         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(winWidth), GUILayout.Height(400));         // if there are no errors, shows the feed as is         if(errorFree)         {             // loops through the array adding the feed data to the feed string             for(int x=0;x<=i-1;x++)             {                 Feeds = Feeds + theFeed[x].Title + " - " + theFeed[x].Date + "\n"                               + "Author: " + theFeed[x].Author + "\n" + theFeed[x].Message + "\n\n";             }         }         // if there is an error sets the feed to just 0 and not a loop         else         {             Feeds = theFeed[0].Title + "\n" + theFeed[0].Message;         }         // Creates a label from the feed string         GUILayout.Label(Feeds);         // ends the view         GUILayout.EndScrollView();         // Makes the window dragable         GUI.DragWindow();     }

  1. endregion

}</csharp>

XML Structure

<xml><Feeds>     <Feed>          <Title></Title>          <Date></Date>          <Author></Author>          <Message></Message>     </Feed> </Feeds></xml> I was going to post the php script however it seems to be causes errors for the wiki so you can find the script at my blog: [1]

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox