- Home /
How do I spawn a random unit from my XML file?
Hey guys,
What I'm trying to do is:
Start the game, when the game starts you spawn 4 random champions from a database of hundreds onto a panel as it's a 2D game, and the champions will be more of a card form.
What I'm having problems with is how do I spawn 4 random champions with attributes from an XML file? I don't understand how to link the scripts to the xml.
Here's what I have so far:
This is the xml (the full list of champions is far longer):
<?xml version="1.0" encoding="UTF-8"?>
<ChampionCollection>
<Champions>
<Champion Name="Bob">
<HP>5</HP>
<MP>5</MP>
<Attack>5</Attack>
<Armor>2</Armor>
</Champion>
<Champion Name="Alex">
<HP>10</HP>
<MP>5</MP>
<Attack>4</Attack>
<Armor>1</Armor>
</Champion>
<Champion Name="Jess">
<HP>20</HP>
<MP>5</MP>
<Attack>5</Attack>
<Armor>0</Armor>
</Champion>
</Champions>
</ChampionCollection>
The XmlSerializer script:
using UnityEngine;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
[XmlRoot("ChampionCollection")]
public class ChampionContainer {
[XmlArray("Champions")]
[XmlArrayItem("Champion")]
public List<Champion> champions = new List<Champion>();
public static ChampionContainer Load(string path)
{
TextAsset _xml = Resources.Load<TextAsset>(path);
XmlSerializer serializer = new XmlSerializer(typeof(ChampionContainer));
StringReader reader = new StringReader(_xml.text);
ChampionContainer champions = serializer.Deserialize(reader) as ChampionContainer;
reader.Close();
return champions;
}
}
and another:
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
public class Champion {
[XmlAttribute("Name")]
public string Name;
[XmlAttribute("HP")]
public float HP;
[XmlAttribute("MP")]
public float MP;
[XmlAttribute("Attack")]
public float Attack;
[XmlAttribute("Armor")]
public float Armor;
}
Here's the spawn script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class SpawnScript : MonoBehaviour
{
public GameObject[] obj;
// Use this for initialization
void Start()
{
Spawn();
Spawn();
Spawn();
Spawn();
}
void Spawn()
{
GameObject newCard = (GameObject)Instantiate
(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
newCard.transform.parent = transform;
}
}
I'm not sure how I should really go about this, currently I have a prefab instantiated and I want to the prefab to pull from the XML, but I have a feeling that's not how it should be done, So I'm down for any suggestions to change it up. Heads up this is my 1st project so all I really know is basic code and what I've learned from watching a bunch of tutorials.
Any help would be greatly appreciated, I've been stuck on this part for a few weeks now and just really need some help so I can finally move on.
Your answer
Follow this Question
Related Questions
Confusion with process of XML setup 0 Answers
unity and xml question 1 Answer
Parse XML to Vector3 (and other objects you don't control) 0 Answers
Need Help With XML Reading Class Initialization 1 Answer
Create animation from script 0 Answers