Advertisement
Elec0

Untitled

Aug 30th, 2012 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Xml;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class WebLoader : MonoBehaviour
  7. {
  8.     private string url = "http://elec0.comeze.com/XML/Unity/elec0.comeze.com.xml";
  9.     private XmlDocument xmldoc = new XmlDocument();
  10.    
  11.     private List<string> toplayerLinks = new List<string>();
  12.     private List<XmlNode> links = new List<XmlNode>();
  13.     private List<string> linksNames = new List<string>();
  14.    
  15.     private WWW WebStart()
  16.     {
  17.         WWW www = new WWW(url);
  18.         return www;
  19.         //renderer.material.mainTexture = www.texture;
  20.     }
  21.    
  22.     void Start()
  23.     {      
  24.         loadWWW();
  25.         getTopLevelLinks();
  26.        
  27.         foreach(string curS in toplayerLinks)
  28.         {
  29.             XmlNode curNode = findName(curS, xmldoc);
  30.             if(curNode != null)
  31.             {
  32.                 links.Add(curNode);
  33.                 linksNames.Add(curS);
  34.             }
  35.         }
  36.        
  37.         /* At this point the names and nodes are indexed and stored. Now the selection has to begin.
  38.          * When a domino is selected, it has to grab it's name and XmlNode and action from in here, then the action can be executed
  39.          * That itself will take another reading of the XML, I think. I'm not sure about that, though, since all the navdatas are read and stored
  40.          *  in this pass.
  41.         */
  42.        
  43.         // Draw the firstlayer dominos
  44.         drawDomino();
  45.     }
  46.    
  47.     void drawDomino()
  48.     {
  49.         drawPlane(transform.TransformPoint(new Vector3(0, 0, 0)), transform.TransformPoint(new Vector3(1, 1, 0)), Color.magenta);
  50.     }
  51.    
  52.     void drawPlane(Vector3 position, Vector3 cornerPos, Color color)
  53.     {
  54.         gameObject.AddComponent("MeshFilter");
  55.         gameObject.AddComponent("MeshRenderer");
  56.         Mesh mesh = GetComponent<MeshFilter>().mesh;
  57.        
  58.         mesh.Clear();
  59.         //position = new Vector3(-size/2, 0, -size/2);
  60.        
  61.         mesh.vertices = new Vector3[]
  62.         {
  63.             new Vector3(position.x, position.y, position.z),
  64.             new Vector3(position.x, position.y+cornerPos.y, position.z+position.z),
  65.             new Vector3(position.x+cornerPos.x, position.y+cornerPos.y, position.z+cornerPos.z),
  66.             new Vector3(position.x+cornerPos.x, position.y, position.z+cornerPos.z)
  67.         };
  68.        
  69.         mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1)};
  70.        
  71.         mesh.triangles = new int[] {0, 1, 2, 0, 2, 3};
  72.        
  73.         mesh.RecalculateNormals();
  74.        
  75.         MeshFilter mf = (MeshFilter)gameObject.GetComponent(typeof(MeshFilter));
  76.         MeshRenderer mr = (MeshRenderer)gameObject.GetComponent(typeof(MeshRenderer));
  77.  
  78.         mf.mesh = mesh;
  79.  
  80.         mr.renderer.material.color = color;
  81.     }
  82.    
  83.     // TODO: Better name for this method
  84.     private XmlNode findName(string nameToFind, XmlDocument findXmlDoc)
  85.     {
  86.         XmlNodeList nodeList = findXmlDoc.GetElementsByTagName("navdata");
  87.        
  88.         // Loop over each node in the nodelist, then loop over each node in the childnodes of that node
  89.         for(int i = 0; i < nodeList.Count; ++i)
  90.         {
  91.             XmlNodeList curNodeList = nodeList.Item (i).ChildNodes;
  92.             foreach(XmlNode curNode in curNodeList)
  93.             {
  94.                 if(curNode.InnerText.Equals(nameToFind))
  95.                     return curNode;
  96.             }
  97.         }
  98.        
  99.         return null;
  100.     }
  101.    
  102.     private void getTopLevelLinks()
  103.     {
  104.         // Grab firstlayer's childnodes, there should always only be one firstlayer tag
  105.         XmlNodeList nodeList = xmldoc.GetElementsByTagName("firstlayer").Item (0).ChildNodes;
  106.        
  107.         foreach(XmlNode x in nodeList)
  108.         {
  109.             toplayerLinks.Add(x.InnerText);
  110.         }
  111.     }
  112.    
  113.     private void loadWWW()
  114.     {
  115.         WWW www = WebStart();
  116.        
  117.         while(www.isDone == false) // This should work because I think the www does loading asyc. If not, it doesn't matter
  118.         { }
  119.         xmldoc.LoadXml(www.text);
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement