Skip to main content

Posts

Showing posts from 2012

Read and Write XML using javascript in unity

Here i am posting a script, which saves data to XML and read that saved data from XML. It also shows read data in a GUI.
I would like to thanks http://unitynoobs.blogspot.in/ for there Tutorial on Xml, Though those where on C# but i tried to convert them in unity javascript( js )


Before starting, first always prepare a structure for your xml (the way you want to save your data in XML ), than its much easier to create a script which will follow a pattern to save your data and it will follow same pattern to read your data from XML. For the first timer who don't know about xml I would like to recommend this tutorial on xml. http://www.w3schools.com/xml/xml_whatis.asp
My XML Stock.xml
<Stock> <ABC> <Price>45</Price> <Qnty>54</Qnty> </ABC> <XYZ> <Price>45</Price> <Qnty>3</Qnty> </XYZ> </Stock>


Here is the full script, and you can find the project link below.

#pragma strict import System.Collections.Generic; import S…

Convert String to Float using JavaScript in Unity

#pragma strict
var stringFloatValue : String = "";  // use to store float value inserted by user in TextField

var stringIntValue : String = "";     // use to store int value inserted by user in TextField

var FloatValue : float;
var IntValue : int;

function OnGUI() {

GUI.Box(Rect(0,0,300,500),"");
GUI.Box(Rect(5,5,70,30),"Value");

    stringFloatValue = GUI.TextField (Rect (0, 40, 80, 30), stringFloatValue, 25);



    stringIntValue = GUI.TextField (Rect (0, 80, 80, 30), stringIntValue, 25);


GUI.Box(Rect(110,5,70,30),"Result");

GUI.Box(Rect(105,40,80,30), FloatValue.ToString());

GUI.Box(Rect(105,80,80,30), IntValue.ToString());

if(GUI.Button(Rect(0,200,200,20),"Convert"))
    {
if(float.TryParse(stringFloatValue,FloatValue))  // checking if user entered a valid string
        {
            FloatValue = float.Parse(stringFloatValue);    // converting string to float
        }
if(System.Int32.TryParse(stringIntValue,IntValue))  // checking i…