Advertisement
infinite_ammo

ParseTwine.cs

Nov 8th, 2013
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class ParseTwine : MonoBehaviour
  6. {
  7.     public class Node
  8.     {
  9.         public string title;
  10.         public string text;
  11.     }
  12.  
  13.     public TextAsset text;
  14.  
  15.     public List<Node> nodes = new List<Node>();
  16.  
  17.     Node currentNode;
  18.  
  19.     void Start()
  20.     {
  21.         Parse(text.text);
  22.     }
  23.  
  24.     void Parse(string text)
  25.     {
  26.         nodes.Clear();
  27.  
  28.         // 0 - searching for start, 1 - reading nodes
  29.         int mode = 0;
  30.         int phase = 0;
  31.         string[] lines = text.Split('\n');
  32.  
  33.         Node node = null;
  34.  
  35.         foreach (string line in lines)
  36.         {
  37.             if (mode == 0)
  38.             {
  39.                 if (line.IndexOf("Tiddler") != -1)
  40.                 {
  41.                     mode = 1;
  42.                 }
  43.             }
  44.             else if (mode == 1)
  45.             {
  46.                 if (line[0] == 'V')
  47.                 {
  48.                     if (phase == 0)
  49.                     {
  50.                         node = new Node();
  51.                         node.text = line.Substring(1, line.Length-1);
  52.                         node.text = node.text.Replace("\\u000a", "\n");
  53.                         phase = 1;
  54.                     }
  55.                     else if (phase == 1)
  56.                     {
  57.                         node.title = line.Substring(1, line.Length-1);
  58.                         phase = 0;
  59.                         nodes.Add(node);
  60.  
  61.                         Debug.Log("------------------------------------");
  62.                         Debug.Log("Title: " + node.title);
  63.                         Debug.Log("Text: " + node.text);
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement