- Home /
Question by
benfattino · Jun 29, 2015 at 12:35 PM ·
wwwloadcsv
load csv www
I need to load csv from www I try this:
...
IEnumerator LoadCSV(){
WWW www = new WWW(url);
yield return www;
string str = www.text.ToString();
csvFile.text=str;
}
public void Start(){
StartCoroutine (LoadCSV());
grid = SplitCsvGrid(csvFile.text);
}
...
I give me error message: Assets/CSVReaderWeb.cs(38,41): error CS0200: Property or indexer `UnityEngine.TextAsset.text' cannot be assigned to (it is read only)
Where I am wrong?
Comment
This is the complete script:
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
public class CSVReaderWeb : $$anonymous$$onoBehaviour
{
public TextAsset csvFile;
public string csvText;
public string mat;
public string model;
public string price;
int i ;
int j;
public int x;
public int y;
private bool Found$$anonymous$$at;
private bool Found$$anonymous$$odel;
string[,] grid;
public string url;
public void Awake()
{}
IEnumerator LoadCSV(){
WWW www = new WWW (url);
yield return www;
csvText = www.text;
}
public void Start(){
StartCoroutine (LoadCSV());
grid = SplitCsvGrid(csvFile.text);
}
public void Update (){
x = 0;
y = 0;
for (int i = 0; i < grid.GetUpperBound(0); i++) {
if (grid [i, 0] == mat) {
x = i;
}
}
for (int j = 0; j < grid.GetUpperBound(1); j++) {
if (grid [0, j] == model) {
y = j;
}
}
if (x == 0) {
price = "$$anonymous$$at_null";
}
else if (y == 0) {
price = "$$anonymous$$odel_null";
}
else {
price = grid [x, y];
}
}
// splits a CSV file into a 2D string array
static public string[,] SplitCsvGrid(string csvText)
{
string[] lines = csvText.Split("\n"[0]);
// finds the max width of row
int width = 0;
for (int i = 0; i < lines.Length; i++)
{
string[] row = SplitCsvLine( lines[i] );
width = $$anonymous$$athf.$$anonymous$$ax(width, row.Length);
}
// creates new 2D string grid to output to
string[,] outputGrid = new string[width + 1, lines.Length + 1];
for (int y = 0; y < lines.Length; y++)
{
string[] row = SplitCsvLine( lines[y] );
for (int x = 0; x < row.Length; x++)
{
outputGrid[x,y] = row[x];
// This line was to replace "" with " in my output.
// Include or edit it as you wish.
outputGrid[x,y] = outputGrid[x,y].Replace("\"\"", "\"");
}
}
return outputGrid;
}
// splits a CSV row
static public string[] SplitCsvLine(string line)
{
return (from System.Text.RegularExpressions.$$anonymous$$atch m in System.Text.RegularExpressions.Regex.$$anonymous$$atches(line,
@"(((?<x>(?=[,\r\n]+))|""(?<x>([^""]|"""")+)""|(?<x>[^,\r\n]+)),?)",
System.Text.RegularExpressions.RegexOptions.ExplicitCapture)
select m.Groups[1].Value).ToArray();
}
}
I would only read the CSV file from URL. Thanks.
public TextAsset csvFile;
I only need to load txt or csv file from www...