AceLand Unity Packages
  • Home
  • Getting Started
    • Installation
    • Project Settings
    • Architecture Graph
    • Development Level
  • Tutorial
    • Create Your Package
    • Create Project Settings
  • Packages
    • Library
      • Change Log
      • Editor Tools
      • Mono
        • Follow Object
        • Singleton
      • Attributes
        • Conditional Show
        • Inspector Button
        • ReadOnly Field
      • Build Leveling
      • CVS
      • DataTools
      • Disposable Object
      • Extensions
      • Json
      • Kalman Filter
      • Optional
      • Project Setting
      • Serialization Surrogate
      • Utils
    • Event Driven
      • Change Log
      • Event Bus
      • Signal
    • Input
      • Change Log
    • Memento Service
      • Change Log
    • Node Framework
      • Change Log
      • Mono Node
    • Node For Mono (dev)
    • Player Loop Hack
      • Change Log
    • Pool
      • Change Log
    • Security (dev)
    • States
      • Change Log
    • Task Utils
      • Change Log
    • WebRequest
      • Change Log
Powered by GitBook
On this page
  • What is CSV?
  • Features
  • Reading from File
  • Reading from String
  • Reading from Unity TextAsset
  • Reading Single Line
  • Features Details
  • Best Practices
  1. Packages
  2. Library

CVS

A lightweight extension that provides CVS functionality.

What is CSV?

CSV (Comma-Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. Each line of the file is a data record consisting of one or more fields, separated by commas. CSV files are widely used for data exchange between different applications and systems.

Name,Age,City
John,25,New York
Alice,30,"Los Angeles, CA"
Bob,28,Chicago

Features

  • Read CSV from file path

  • Parse CSV from string content or Unity TextAsset

  • Header row handling

  • Proper handling of quoted fields

  • Empty line skipping

  • Regex-based reliable parsing

Reading from File

string filePath = "path/to/your/file.csv";

// Without header
foreach (string[] row in filePath.ReadAsCsvFile())
{
    // Process each row
    string firstColumn = row[0];
    string secondColumn = row[1];
}

// With header
foreach (string[] row in filePath.ReadAsCsvFile(hasHeader: true))
{
    // First row (header) is automatically skipped
}

Reading from String

string csvContent = "Name,Age\nJohn,25\nAlice,30";

foreach (string[] row in csvContent.ReadAsCsv(hasHeader: true))
{
    // Process each row
}

Reading from Unity TextAsset

TextAsset csvFile;

foreach (string[] row in csvFile.ReadAsCsv(hasHeader: true))
{
    // Process each row
}

Reading Single Line

string line = "Field1,Field2,\"Field,3\"";
string[] fields = line.ReadCsvLine();
// fields = ["Field1", "Field2", "Field,3"]

Features Details

  • Quoted Field Support: Properly handles fields containing commas (e.g., "Los Angeles, CA")

  • Empty Line Handling: Automatically skips empty or whitespace-only lines

  • Header Row Option: Easily skip header row when needed

  • Multiple Input Sources: Support for file paths, strings, and Unity TextAssets

  • Memory Efficient: Uses IEnumerable for lazy evaluation

Best Practices

  • Always specify hasHeader explicitly for better code readability

  • Use appropriate method based on your data source (file/string/TextAsset)

  • Handle potential file not found exceptions when using ReadAsCsvFile

  • Consider line endings when working with different platforms

Last updated 6 months ago