/Assets/SimpleCollada/SimpleXML/Example Procedural Mini Town/TownCreator.cs

https://bitbucket.org/timur_rakhmanov/cloudbuild · C# · 103 lines · 66 code · 22 blank · 15 comment · 13 complexity · 8a20415aca1184aca405aeda3e66cf5e MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using OrbCreationExtensions;
  5. public class TownCreator : MonoBehaviour {
  6. public TextAsset xmlFile;
  7. public GameObject wallPrototype;
  8. public GameObject roofPrototype;
  9. private List<GameObject> houses = new List<GameObject>();
  10. void Start () {
  11. SetupTownFromFile();
  12. // Here are some example that use an external file.
  13. // SetupTownFromServer("http://myserver.com/town.xml"); // from webserver
  14. // SetupTownFromServer("file:///path1/path2/town.xml"); // on mac
  15. // SetupTownFromServer("file://C:/path1/path2/town.xml"); // on windows (i believe)
  16. }
  17. void OnGUI() {
  18. GUI.Label(new Rect(2,2,400,400), "Very simple example of generating objects that are defined by XML files at runtime.\n\nIt uses 2 prototype of a house and a roof (ok, they are simple cubes in this example, but thats not the point).\n\nAn XML file is downloaded, the XML is parsed and gameobject are created and configured.\nHave a look in the file TownCreator.cs and Town.xml");
  19. }
  20. private void SetupTownFromFile() {
  21. // read xml file
  22. Hashtable townDefinition = SimpleXmlImporter.Import( xmlFile.text );
  23. // build town
  24. SetupTown(townDefinition);
  25. }
  26. private IEnumerator SetupTownFromServer(string url) {
  27. string xmlString = null;
  28. yield return StartCoroutine( DownloadFile ( url, retval => xmlString = retval) );
  29. if(xmlString!=null && xmlString.Length>0) {
  30. Hashtable townDefinition = SimpleXmlImporter.Import( xmlString );
  31. SetupTown( townDefinition );
  32. }
  33. }
  34. private void SetupTown(Hashtable townDefinition) {
  35. // get house definitions
  36. ArrayList houseDefinitions = townDefinition.GetArrayList( "town" );
  37. // process each house definition
  38. for(int i=0;i<houseDefinitions.Count;i++) {
  39. Hashtable houseDefinition = houseDefinitions.GetHashtable(i);
  40. Debug.Log("building house:" + houseDefinition.JsonString());
  41. // create house gameObject
  42. GameObject house = new GameObject( houseDefinition.GetString( "name" ) );
  43. // get elements
  44. ArrayList elementDefinitions = houseDefinition.GetArrayList( "archelements" );
  45. for(int j=0;j<elementDefinitions.Count;j++) {
  46. Hashtable elementDefinition = elementDefinitions.GetHashtable(j);
  47. GameObject prototype = null;
  48. if(elementDefinition.GetString("type") == "wall") {
  49. prototype = wallPrototype;
  50. } else if(elementDefinition.GetString("type") == "roof") {
  51. prototype = roofPrototype;
  52. }
  53. if(prototype != null) {
  54. // create element
  55. GameObject element = (GameObject)GameObject.Instantiate( prototype );
  56. // element.transform.SetParent( house.transform );
  57. element.transform.parent = house.transform;
  58. element.transform.localPosition = elementDefinition.GetVector3( "offset" );
  59. element.GetComponent<MeshRenderer>().material.color = elementDefinition.GetColor( "color" );
  60. }
  61. }
  62. // position the house
  63. house.transform.position = houseDefinition.GetVector3( "position" );
  64. // scale the house
  65. house.transform.localScale = houseDefinition.GetVector3( "scale" );
  66. // add to houses array for future access
  67. houses.Add( house );
  68. }
  69. }
  70. private IEnumerator DownloadFile(string url, System.Action<string> result) {
  71. Debug.Log("Downloading "+url);
  72. WWW www = new WWW(url);
  73. yield return www;
  74. if(www.error!=null) {
  75. Debug.Log(www.error);
  76. } else {
  77. Debug.Log("Downloaded "+www.bytesDownloaded+" bytes");
  78. }
  79. result(www.text);
  80. }
  81. }