728x90
728x170
CSVDemo
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CSVDemo : MonoBehaviour {
public TextAsset csv;
void Start () {
csv = (TextAsset)Resources.Load<TextAsset>("ExampleCSV");
CSVReader.DebugOutputGrid( CSVReader.SplitCsvGrid(csv.text) );
PlayerPrefs.SetString("Player Name", "Foobar");
}
}
CSVReader
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Text.RegularExpressions;
public class CSVReader : MonoBehaviour {
public TextAsset csvFile;
public void Start()
{
string[,] grid = SplitCsvGrid(csvFile.text);
Debug.Log("size = " + (1+ grid.GetUpperBound(0)) + "," + (1 + grid.GetUpperBound(1)));
DebugOutputGrid(grid);
}
// outputs the content of a 2D array, useful for checking the importer
static public void DebugOutputGrid(string[,] grid)
{
string textOutput = "";
for (int y = 0; y < grid.GetUpperBound(1); y++) {
for (int x = 0; x < grid.GetUpperBound(0); x++) {
textOutput += grid[x,y];
textOutput += "|";
}
textOutput += "\n";
}
Debug.Log(textOutput);
}
// 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 = Mathf.Max(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 Match m in Regex.Matches(line,
@"(((?<x>(?=[,\r\n]+))|""(?<x>([^""]|"""")+)""|(?<x>[^,\r\n]+)),?)",
RegexOptions.ExplicitCapture)
select m.Groups[1].Value).ToArray();
}
}
TinyXmlReader
using UnityEngine;
using System.Collections;
public class TinyXmlReader
{
private string xmlString = "";
private int idx = 0;
public TinyXmlReader(string newXmlString)
{
xmlString = newXmlString;
}
public string tagName = "";
public bool isOpeningTag = false;
public string content = "";
// properly looks for the next index of _c, without stopping at line endings, allowing tags to be break lines
int IndexOf(char _c, int _i)
{
int i = _i;
while (i < xmlString.Length)
{
if (xmlString[i] == _c)
return i;
++i;
}
return -1;
}
public bool Read()
{
if (idx > -1)
idx = xmlString.IndexOf("<", idx);
if (idx == -1)
{
return false;
}
++idx;
// skip attributes, don't include them in the name!
int endOfTag = IndexOf('>', idx);
int endOfName = IndexOf(' ', idx);
if ((endOfName == -1) || (endOfTag < endOfName))
{
endOfName = endOfTag;
}
if (endOfTag == -1)
{
return false;
}
tagName = xmlString.Substring(idx, endOfName - idx);
idx = endOfTag;
// check if a closing tag
if (tagName.StartsWith("/"))
{
isOpeningTag = false;
tagName = tagName.Remove(0, 1); // remove the slash
}
else
{
isOpeningTag = true;
}
// if an opening tag, get the content
if (isOpeningTag)
{
int startOfCloseTag = xmlString.IndexOf("<", idx);
if (startOfCloseTag == -1)
{
return false;
}
content = xmlString.Substring(idx+1, startOfCloseTag-idx-1);
content = content.Trim();
}
return true;
}
// returns false when the endingTag is encountered
public bool Read(string endingTag)
{
bool retVal = Read();
if (tagName == endingTag && !isOpeningTag)
{
retVal = false;
}
return retVal;
}
}
Usage
using UnityEngine;
using System.Collections;
public class Usage : MonoBehaviour {
private string text = "";
public GUISkin skin;
void OnGUI()
{
GUILayout.Label(text, skin.label);
}
void Start()
{
string xmlText = System.IO.File.ReadAllText(Application.dataPath + "/Rifleman.xml");
TinyXmlReader reader = new TinyXmlReader(xmlText);
while (reader.Read())
{
if (reader.isOpeningTag)
{
text += (reader.tagName + " \"" + reader.content + "\"\n");
}
if (reader.tagName == "Skills" && reader.isOpeningTag)
{
while(reader.Read("Skills")) // read as long as not encountering the closing tag for Skills
{
if (reader.isOpeningTag)
{
text += ("Skill: " + reader.tagName + " \"" + reader.content + "\"\n");
}
}
}
}
}
}
MyWindow
using UnityEngine;
using UnityEditor;
using System.Collections;
public class MyWindow : EditorWindow {
string myString = "Hello World";
bool groupEnabled;
bool myBool = true;
float myFloat = 1.23f;
Color myColor = Color.white;
GameObject myGameObject = null;
[MenuItem ("My Window/Hello World")]
static void Init () {
MyWindow window = (MyWindow)EditorWindow.GetWindow (typeof (MyWindow));
}
void OnGUI () {
GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
myString = EditorGUILayout.TextField ("Text Field", myString);
myBool = EditorGUILayout.Foldout(myBool,"Fold Out");
if(myBool)
{
myFloat = EditorGUILayout.Slider(myFloat,0.0f,10.0f);
myColor = EditorGUILayout.ColorField(myColor);
myGameObject = EditorGUILayout.ObjectField(myGameObject,typeof(GameObject)) as GameObject;
}
}
}
728x90
그리드형
'IT > 유니티' 카테고리의 다른 글
유니티 게임 프로그래밍 16장 예제 구현 (0) | 2021.05.30 |
---|---|
유니티 게임 프로그래밍 12장 예제 구현 (0) | 2021.05.30 |
유니티 게임 프로그래밍 10장 예제 구현 (0) | 2021.05.30 |
유니티 게임 프로그래밍 9장 예제 구현 (0) | 2021.05.30 |
유니티 게임 프로그래밍 8장 예제 구현 (0) | 2021.05.30 |
댓글