using UnityEngine;
using UnityEngine.UI; // need this to use Text type
using System.Collections;
using System.Collections.Generic; // needed for using list, you might not need this one
public class TestScript : MonoBehaviour
{
List<string> myMessages; // I need something to show
public Transform canvasElementToPutMessagesIn;
public Transform messageTextPrefab;
void Start()
{
myMessages = new List<string>();
// impart wise message on the world... :P
myMessages.Add("hello");
myMessages.Add("world");
myMessages.Add("my");
myMessages.Add("name");
myMessages.Add("is");
myMessages.Add("bob");
// it isn't really :smile:
// the bit you need to look at
foreach(string message in myMessages)
{
Transform clone = Instantiate(messageTextPrefab);
clone.SetParent(canvasElementToPutMessagesIn);
// UI elements must be parented to a canvas, or a descendant of a canvas or they wont show
clone.GetComponent<Text>().text = message;
}
}
}
messageTextPrefab just needs to be a standard Text ui element (i.e. create in the hierarchy via the r-click menu and drag it into the project as a prefab), or you can style it as you would like.
if you add a “layout” component onto the UI element you target as the container for the messages you can style them as you need to.
Hi LeflyRighty Thanks for help,
I was able to perform the function private message with your advice, but I would have another question.
As you can see from the screenshot it all works perfectly, except that does not list in the right posts.
How could I improve the script so that it works well?
using UnityEngine;
using UnityEngine.UI; // need this to use Text type
using System.Collections;
using System.Collections.Generic; // needed for using list, you might not need this one
public class pm_MessageListUI : MonoBehaviour {
public GameObject MessageUIPrefab = null;
public Transform MessageList = null;
public Text MessageCountText = null;
public Text senderText = null;
public Text titleText = null;
public Text bodyText = null;
public Text statusText = null;
public Text dateText = null;
pm_MessageClass rdr;
// Use this for initialization
void Awake ()
{
rdr = new pm_MessageClass("http://link"); //Not work ssl :'(
// now display the feed items
foreach(pm_MessageClass.messages itm in rdr.rowNews.message)
{
GameObject f = Instantiate(MessageUIPrefab) as GameObject;
f.transform.SetParent(MessageList, false);
senderText.text = itm.sender;
titleText.text = itm.title;
statusText.text = itm.readed;
dateText.text = itm.date;
// UI elements must be parented to a canvas, or a descendant of a canvas or they wont show
}
}
}
This is the class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using SecureConnection;
public class pm_MessageClass{
XmlTextReader PMReader;
XmlDocument PMDoc;
XmlNode nodePM;
XmlNode nodeMessages;
XmlNode nodeMessage;
public channel rowNew;
public struct channel
{
public string count;
public List<messages> message;
}
public struct messages
{
public string idmsg;
public string sender;
public string title;
public string body;
public string readed;
public string date;
}
// our constructor takes the URL to the feed
public pm_MessageClass (string feedURL){
// setup the messages structure
rowNew = new channel ();
// make the list available to write to
rowNew.message = new List<messages>();
PMReader = new XmlTextReader (feedURL);
PMDoc = new XmlDocument ();
PMDoc.Load (PMReader);
// Loop for the <messages> tag
for (int i = 0; i < PMDoc.ChildNodes.Count; i++)
{
// If it is the messages tag
if (PMDoc.ChildNodes[i].Name == "messages") {
// <messages> tag found
nodePM = PMDoc.ChildNodes[i];
}
}
// Loop for the <count> tag
for (int i = 0; i < nodePM.ChildNodes.Count; i++) {
// If it is the count tag
if (nodePM.ChildNodes[i].Name == "count") {
// <count> tag found
nodeMessages = nodePM.ChildNodes[i];
}
}
for (int i = 0; i < nodeMessages.ChildNodes.Count; i++) {
if (nodeMessages.ChildNodes[i].Name == "message") {
nodeMessage = nodeMessages.ChildNodes[i];
messages pm = new messages();
pm.idmsg = nodeMessage["idmsg"].InnerText;
pm.sender = nodeMessage["sender"].InnerText;
pm.title = NetworkSecurity.Decrypt(nodeMessage["title"].InnerText);
pm.body = NetworkSecurity.Decrypt(nodeMessage["body"].InnerText);
pm.readed = nodeMessage["readed"].InnerText;
pm.date = nodeMessage["date"].InnerText;
rowNew.message.Add(pm);
}
}
}
}
I wanted to use “public IEnumerator LoadMessage (string username)”
but then I would not know how to call up the list with the function Messages
StartCoroutine (NetworkManager.messages (link));
so I might as well use SSL when I am not allowed