Load variable from XML and apply to a function

Hi!

Im trying to load a variable from an XML and then apply it in a function.

For example:

// load the rotation velocity from an .xml and then apply it into a simple function

var loaded_value = rotateSpeed

function Update () {
transform.Rotate(Vector3.rightTime.deltaTimerotateSpeed);
}

Anyone has done it before?

Thanks in advance!!!

Hello,

You need to use XmlSerializer.

You can create a “paramClass”, that will store parameters, such as rotation velocity.

Pseudo code example :

  [Serializable()]
  public class MyParameters
  {
     public float RotationVelocity {get;set;}
  }

  public class Test
  {
     public Test()
    {

        MyParameters myParam = new MyParameters();
        XmlSerializer serializer;
        myParam.RotationVelocity = 6.0f;

        // Save -> That will create a xml file holding your class in the resources folder
        serializer = new XmlSerializer(myParam.GetType());
        using (TextWriter writer = new StreamWriter(Application.dataPath + "/Resources/myparmeters.xml" ))
        {
           serializer.Serialize(writer, myParam);
        }

        myParam = null;
        //Load -> Load the result from the stored file   
        TextAsset textAsset = (TextAsset)Resources.Load("myparmeters.xml");
        serializer = new XmlSerializer(myParam.GetType());
        XmlReader reader = XmlReader.Create(new StringReader(textAsset.text));
        myParam = (serializer.Deserialize(reader) as MyParameters);
     }
  }

Depending on your need you can also save a List of parameters.
You will also find some info here :
http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer