Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

IU Prefabs list from xml string, Help me

Discussion in 'Scripting' started by Morpheus-Tao, Sep 9, 2015.

  1. Morpheus-Tao

    Morpheus-Tao

    Joined:
    Sep 4, 2014
    Posts:
    3
    Hi, I am days that I try in every way to make a list of messages, but I can not imagine the mechanism.

    Then I created a php function that draws from the message list and returns this structure in xml:

    Code (CSharp):
    1. <messages>
    2. <count value="2">
    3. <message>
    4. <idmsg>1</idmsg>
    5. <sender>Morpheus Tao</sender>
    6. <title>Title text message</title>
    7. <body>Message</body>
    8. <readed>0</readed>
    9. <date>2015-09-06 01:52:30</date>
    10. </message>
    11. <message>
    12. <idmsg>2</idmsg>
    13. <sender>Tirion Namir</sender>
    14. <title>Title text message</title>
    15. <body>Message</body>
    16. <readed>0</readed>
    17. <date>2015-09-06 01:52:30</date>
    18. </message>
    19. </count>
    20. </messages>
    With this function:
    Code (CSharp):
    1. public IEnumerator LoadMessage(string username){
    2.         string MessageUrl= config.ServerUri + "Etc....";
    3.         var www = new WWW (MessageUrl);
    4.        
    5.         if (!www.isDone)
    6.             yield return www;
    7.        
    8.         if (!string.IsNullOrEmpty (www.error)) {
    9.             Debug.LogException(new Exception("NetworkManager.Messages error" + www.error));
    10.             IsLoggedIn = true;
    11.             yield break;
    12.         }
    13.        
    14.         Debug.Log ("result:" + www.text);
    15.        
    16.         XmlDocument xmlDoc = new XmlDocument ();
    17.         xmlDoc.LoadXml (www.text);
    18.         int count = 0;
    19.         XmlNodeList xnl = xmlDoc.SelectNodes("/messages/count");
    20.         foreach (XmlNode node in xnl)
    21.         {
    22.             Debug.Log( count = Convert.ToInt32(node.Attributes["value"].InnerText));
    23.             if (count != 0)
    24.             {
    25.                 XmlNodeList xnl2 = xmlDoc.SelectNodes("/messages/count/message");
    26.                 foreach (XmlNode node2 in xnl2)
    27.                 {
    28.                   //Test Debug info loaded
    29.                     Debug.Log(node2["idmsg"].InnerText);
    30.                     Debug.Log(node2["sender"].InnerText);
    31.                     Debug.Log(NetworkSecurity.Decrypt(node2["title"].InnerText));
    32.                     Debug.Log(NetworkSecurity.Decrypt(node2["body"].InnerText));
    33.                     Debug.Log(node2["readed"].InnerText);
    34.                     Debug.Log(node2["date"].InnerText);
    35.                 }
    36.             }
    37.  
    38.         }
    39.        
    40.        
    41.     }
    Now I would like the messages loaded from XML
    They were listed in the UI of my game (with canvas)
    example:


    How do I program the UI so that it works, you can help me, maybe with some examples?
    Still they are not very practical new UI 4.6
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using UnityEngine.UI; // need this to use Text type
    5. using System.Collections;
    6. using System.Collections.Generic; // needed for using list, you might not need this one
    7.  
    8.  
    9. public class TestScript : MonoBehaviour
    10. {
    11.     List<string> myMessages; // I need something to show
    12.  
    13.  
    14.     public Transform canvasElementToPutMessagesIn;
    15.     public Transform messageTextPrefab;
    16.  
    17.     void Start()
    18.     {
    19.         myMessages = new List<string>();
    20. // impart wise message on the world... :P
    21.         myMessages.Add("hello");
    22.         myMessages.Add("world");
    23.         myMessages.Add("my");
    24.         myMessages.Add("name");
    25.         myMessages.Add("is");
    26.         myMessages.Add("bob");
    27. // it isn't really :D
    28.  
    29.         // the bit you need to look at
    30.         foreach(string message in myMessages)
    31.         {
    32.             Transform clone = Instantiate(messageTextPrefab);
    33.             clone.SetParent(canvasElementToPutMessagesIn);
    34. // UI elements must be parented to a canvas, or a descendant of a canvas or they wont show
    35.  
    36.             clone.GetComponent<Text>().text = message;
    37.         }
    38.  
    39.     }
    40. }
    41.  
    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.
     
    Morpheus-Tao likes this.
  3. Morpheus-Tao

    Morpheus-Tao

    Joined:
    Sep 4, 2014
    Posts:
    3
    Thanks for the help, do experiments and tell you how it went
     
  4. Morpheus-Tao

    Morpheus-Tao

    Joined:
    Sep 4, 2014
    Posts:
    3
    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.

    Correct order:
    Code (CSharp):
    1. <messages>
    2. <count value="3">
    3. <message>
    4. <idmsg>1</idmsg>
    5. <sender>Ashma Daeva</sender>
    6. <title>5H/pt/8kK3wI40fwiaxP0r5IsSrE1t6fhN2tjVYSngM=</title>
    7. <body>8/+Qod7sfRZL6eE32rl7wg==</body>
    8. <sendlv>3</sendlv>
    9. <readed>0</readed>
    10. <date>2015-09-10 20:59:35</date>
    11. </message>
    12. <message>
    13. <idmsg>2</idmsg>
    14. <sender>Ashma Daeva</sender>
    15. <title>KmZNz46Ny4CiNeCeti2zVQ==</title>
    16. <body>h99DsUhkdI4n7zNA7WxXrA==</body>
    17. <sendlv>3</sendlv>
    18. <readed>0</readed>
    19. <date>2015-09-10 20:59:31</date>
    20. </message>
    21. <message>
    22. <idmsg>3</idmsg>
    23. <sender>Ashma Daeva</sender>
    24. <title>e8vv+nuyuGG1X5MIFbAxZw==</title>
    25. <body>
    26. qvVgFlKQeFK/LRVXa5CdJO50zZsw+j3whW3kEn0iyjTNo8aKoXyvXr0Pn9IzvH0szaPGiqF8r169D5/SM7x9LM2jxoqhfK9evQ+f0jO8fSzNo8aKoXyvXr0Pn9IzvH0szaPGiqF8r169D5/SM7x9LM2jxoqhfK9evQ+f0jO8fSzNo8aKoXyvXr0Pn9IzvH0srLcwkfVXHySC1YYDEcCSVJJcXUNxQCfUyFIIzEg6tzCSXF1DcUAn1MhSCMxIOrcwklxdQ3FAJ9TIUgjMSDq3MJJcXUNxQCfUyFIIzEg6tzCSXF1DcUAn1MhSCMxIOrcwp0iii5doz9+rkrfw+WjwUJT5oK2zjXEZ1fH3jVV1B6kgGLQk9sTCNTFhXUuzTr5CVw153YCzKwknkr0xf+W8v1LyIGKuF1g+S8rDuD8DFTM=
    27. </body>
    28. <sendlv>3</sendlv>
    29. <readed>0</readed>
    30. <date>2015-09-10 20:58:55</date>
    31. </message>
    32. </count>
    33. </messages>
    Result:

    How could I improve the script so that it works well?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI; // need this to use Text type
    3. using System.Collections;
    4. using System.Collections.Generic; // needed for using list, you might not need this one
    5.  
    6.  
    7. public class pm_MessageListUI : MonoBehaviour {
    8.  
    9.     public GameObject MessageUIPrefab = null;
    10.     public Transform MessageList = null;
    11.     public Text MessageCountText = null;
    12.     public Text senderText = null;
    13.     public Text titleText = null;
    14.     public Text bodyText = null;
    15.     public Text statusText = null;
    16.     public Text dateText = null;
    17.  
    18.  
    19.     pm_MessageClass  rdr;
    20.     // Use this for initialization
    21.     void Awake ()
    22.     {
    23.         rdr = new  pm_MessageClass("http://link"); //Not work ssl :'(
    24.    
    25.    
    26.         // now display the feed items
    27.         foreach(pm_MessageClass.messages itm in rdr.rowNews.message)
    28.         {
    29.             GameObject f = Instantiate(MessageUIPrefab) as GameObject;
    30.             f.transform.SetParent(MessageList, false);
    31.             senderText.text = itm.sender;
    32.             titleText.text = itm.title;
    33.             statusText.text = itm.readed;
    34.             dateText.text = itm.date;
    35.             // UI elements must be parented to a canvas, or a descendant of a canvas or they wont show
    36.         }
    37.  
    38.  
    39.     }
    40.     }
    This is the class:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Xml;
    6. using SecureConnection;
    7.  
    8. public class pm_MessageClass{
    9.  
    10.     XmlTextReader PMReader;
    11.     XmlDocument PMDoc;
    12.     XmlNode nodePM;
    13.     XmlNode nodeMessages;
    14.     XmlNode nodeMessage;
    15.     public channel rowNew;
    16.  
    17.     public struct channel
    18.     {
    19.         public string count;
    20.  
    21.         public List<messages> message;
    22.     }
    23.  
    24.  
    25.     public struct messages
    26.     {
    27.         public string idmsg;
    28.         public string sender;
    29.         public string title;
    30.         public string body;
    31.         public string readed;
    32.         public string date;
    33.     }
    34.  
    35.     // our constructor takes the URL to the feed
    36.     public pm_MessageClass (string feedURL){
    37.  
    38.  
    39.  
    40.         // setup the messages structure
    41.         rowNew = new channel ();
    42.         // make the list available to write to
    43.         rowNew.message = new List<messages>();
    44.         PMReader = new XmlTextReader (feedURL);
    45.         PMDoc = new XmlDocument ();
    46.         PMDoc.Load (PMReader);
    47.         // Loop for the <messages> tag
    48.         for (int i = 0; i < PMDoc.ChildNodes.Count; i++)
    49.         {
    50.             // If it is the messages tag
    51.             if (PMDoc.ChildNodes[i].Name == "messages") {
    52.                 // <messages> tag found
    53.                 nodePM = PMDoc.ChildNodes[i];
    54.             }
    55.         }
    56.         // Loop for the <count> tag
    57.         for (int i = 0; i < nodePM.ChildNodes.Count; i++) {
    58.             // If it is the count tag
    59.             if (nodePM.ChildNodes[i].Name == "count") {
    60.                 // <count> tag found
    61.                 nodeMessages = nodePM.ChildNodes[i];
    62.             }
    63.         }
    64.  
    65.  
    66.         for (int i = 0; i < nodeMessages.ChildNodes.Count; i++) {
    67.             if (nodeMessages.ChildNodes[i].Name == "message") {
    68.                 nodeMessage = nodeMessages.ChildNodes[i];
    69.  
    70.                 messages pm = new messages();
    71.                 pm.idmsg = nodeMessage["idmsg"].InnerText;
    72.                 pm.sender = nodeMessage["sender"].InnerText;
    73.                 pm.title = NetworkSecurity.Decrypt(nodeMessage["title"].InnerText);
    74.                 pm.body = NetworkSecurity.Decrypt(nodeMessage["body"].InnerText);
    75.                 pm.readed = nodeMessage["readed"].InnerText;
    76.                 pm.date = nodeMessage["date"].InnerText;
    77.  
    78.                 rowNew.message.Add(pm);
    79.             }
    80.         }
    81.     }
    82. }
    83.  
    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
     
    Last edited: Sep 10, 2015