public static void LoadMapFromXML() { TextAsset asset = Resources.Load <TextAsset>("BackgroundSplit"); MemoryStream stream = new MemoryStream(asset.bytes); XmlSerializer serializer = new XmlSerializer(typeof(BackgroundPieceCollection)); BackgroundPieceCollection collection = (BackgroundPieceCollection)serializer.Deserialize(XmlReader.Create(stream)); List <BackgroundPiece> piecesToLoad = collection.FindWithin(0, 8640, 0, 19200); foreach (BackgroundPiece piece in piecesToLoad) { if (piece.shown != true) { var bundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/background/backgroundsplit-" + piece.x + "-" + piece.y + ".normal"); if (bundle == null) { Debug.Log("Fail to Load Bundle"); return; } var backgroundTexture = bundle.LoadAsset <Texture2D>(piece.resName); Sprite sprite = Sprite.Create(backgroundTexture, new Rect(0, 0, backgroundTexture.width, backgroundTexture.height), new Vector2(0.5f, 0.5f)); GameObject obj = new GameObject(); SpriteRenderer renderer = obj.AddComponent <SpriteRenderer>(); renderer.sprite = sprite; int posx = 0; int posy = 0; for (int i = 0; i <= 37 - piece.y; i++) { if (i == 0) { posy += 128; } if (i == 1) { posy += 128 + 256; } if (i > 1) { posy += 512; } } for (int i = 0; i <= piece.x; i++) { if (i == 16) { posx += 224 + 256; } else { posx += 512; } } obj.transform.position = new Vector3(posx / 100.0f, posy / 100.0f, 0f); Debug.Log(obj.transform.position); obj = PrefabUtility.InstantiatePrefab(obj) as GameObject; bundle.Unload(false); } } }
// Use this for initialization void Start() { TextAsset asset = Resources.Load <TextAsset>("BackgroundSplit"); MemoryStream stream = new MemoryStream(asset.bytes); XmlSerializer serializer = new XmlSerializer(typeof(BackgroundPieceCollection)); collection = (BackgroundPieceCollection)serializer.Deserialize(XmlReader.Create(stream)); //Debug.Log("Unity :" + Application.dataPath + "!/assets/BackgroundSplit.xml"); //var bundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "backgroundsplit-0-0.normal")); //if (bundle == null) //{ // Debug.Log("Fail to Load Bundle"); // return; //} //var bg00 = bundle.LoadAsset<Texture2D>("BackgroundSplit-0-0"); //Sprite sprite = Sprite.Create(bg00, new Rect(0, 0, bg00.width, bg00.height), new Vector2(0.5f, 0.5f)); //bgSprite.GetComponent<SpriteRenderer>().sprite = sprite; bgObjects = new GameObject[36]; availableBackground = new Stack <GameObject>(); for (int i = 0; i < bgObjects.Length; i++) { bgObjects[i] = new GameObject(); bgObjects[i].AddComponent <SpriteRenderer>(); bgObjects[i].GetComponent <SpriteRenderer>().sortingLayerName = "Background"; availableBackground.Push(bgObjects[i]); } }
public static void ReleaseFiles() { TextAsset asset = Resources.Load <TextAsset>("BackgroundSplit"); MemoryStream stream = new MemoryStream(asset.bytes); XmlSerializer serializer = new XmlSerializer(typeof(BackgroundPieceCollection)); BackgroundPieceCollection collection = (BackgroundPieceCollection)serializer.Deserialize(XmlReader.Create(stream)); List <BackgroundPiece> piecesToLoad = collection.FindWithin(0, 8640, 0, 19200); foreach (BackgroundPiece piece in piecesToLoad) { var bundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/background/backgroundsplit-" + piece.x + "-" + piece.y + ".normal"); bundle.Unload(true); } }