I’ve been getting several questions regarding this, so I’ve decided to share the script with the community. Yes, it really is just one script. I am using Sprite Manager 1, if you use 2 you might need to change some things, I don’t know.
The setup is simple:
- Linked Sprite Manager
- Main Camera
- Galaxy Object
The Galaxy Object has this script attached:
using UnityEngine;
using System.Collections;
using System.Globalization;
using System.Xml;
using System.IO;
public class LoadGalaxySprites : MonoBehaviour {
public SpriteManager spriteMan;
public GameObject Sun;
public float SunSize = 0.5f;
private string MapData;
public string MapURL = "http://your.url";
void Start() {
StartCoroutine(FetchMapData());
}
void SetupSystems() {
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(MapData));
XmlNodeList sys = doc.GetElementsByTagName("system");
int count = 0;
SpriteManager SM;
SM = Instantiate(spriteMan, Vector3.zero, Quaternion.identity) as SpriteManager;
foreach (XmlNode s in sys) {
XmlElement data = (XmlElement) s;
Vector3 position = new Vector3(0f, 0f, 0f);
if (data.HasAttributes) {
float x = float.Parse(data.Attributes["x"].InnerText, CultureInfo.InvariantCulture);
float z = float.Parse(data.Attributes["y"].InnerText, CultureInfo.InvariantCulture);
float y = float.Parse(data.Attributes["z"].InnerText, CultureInfo.InvariantCulture);
position = new Vector3(x, y, z);
}
GameObject NewSun = Instantiate(Sun, position, Quaternion.identity) as GameObject;
// I can only have 65000 vertices per mesh, so I have to use multiple sprite manager objects, a new one about every 10000 sprites
if (++count>=10000) {
count=0;
SM = Instantiate(spriteMan, Vector3.zero, Quaternion.identity) as SpriteManager;
}
SM.AddSprite(NewSun, // The game object to associate the sprite to
SunSize, // The width of the sprite
SunSize, // The height of the sprite
0, // Left pixel
63, // Bottom pixel
64, // Width in pixels
64, // Height in pixels
true); // Billboarded?
}
}
/*
WWW interface functions
*/
IEnumerator FetchMapData() {
WWW download = new WWW(MapURL);
Debug.Log("downloading from "+MapURL);
yield return download;
if (download.error!=null) {
Debug.Log("error downloading: "+download.error);
} else {
MapData = download.text;
Debug.Log("level data download succeeded.");
SetupSystems();
}
}
}
At the MapURL, it expects an XML file with the sun data, which should look like this:
<?xml version="1.0"?>
<galaxy>
<system x="-460.15" y="42.76" z="0.5" />
<system x="-448.63" y="6.56" z="-2.87" />
<system x="-442.75" y="-44.22" z="2.75" />
<system x="-440.29" y="-19.42" z="1.04" />
<system x="-439.72" y="-71.64" z="-0.26" />
...
</galaxy>
And that is really all there is to it. The Sun GameObject that gets instantiated is just an empty game object, its job is to tell SM the position.
As you can see, SM does pretty much all the work for you. If anyone makes something cool with this, please share it as well.