Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Splitting a String Results in Less Pieces?

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Jan 11, 2019.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,293
    So, a bit of backstory.

    I'm trying to populate a 34 x 26 (884 tile) grid using a text array (a series of characters in a text file representing that grid), as a way of building levels.

    The file has numbers (currently all 0s with a 1 at the very beginning and end), seperated by 3 spaces (just for visual purposes) each.

    When I split the file I only get 859 elements in the array I'm populating.

    What exactly have I done wrong? I'm sure it's something stupid and simple but I just can't see it, I'm hoping someone here who is better at programming can work out what I have done wrong.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class GameGridMapper : MonoBehaviour
    8. {
    9.  
    10.     public TextAsset asciiArray;
    11.     public string[] arrayContents;
    12.  
    13.     private GridLayoutGroup gameGrid;
    14.     private Image tilePanel;
    15.     public int gridSize;
    16.  
    17.     void Start()
    18.     {
    19.         BuildGrid();
    20.     }
    21.  
    22.     public bool BuildGrid()
    23.     {
    24.         //Standard grid: 34l_x_26h
    25.  
    26.         gameGrid = FindObjectOfType<GridLayoutGroup>();
    27.         tilePanel = gameGrid.gameObject.GetComponentInChildren<Image>();
    28.         tilePanel.gameObject.SetActive(false);
    29.  
    30.         string array = asciiArray.text;
    31.         arrayContents = array.Split(new string[] { "   " }, StringSplitOptions.None);
    32.  
    33.         StartCoroutine(PopulateGrid());
    34.  
    35.         return true;
    36.     }
    37.  
    38.     IEnumerator PopulateGrid()
    39.     {
    40.         if (gridSize < 884)
    41.         {
    42.             Image newTile = Instantiate(tilePanel, gameGrid.transform);
    43.             newTile.gameObject.SetActive(true);
    44.             gridSize++;
    45.             newTile.name = "Tile " + gridSize;
    46.             yield return new WaitForSeconds(0.0001f);
    47.             StartCoroutine(PopulateGrid());
    48.         }
    49.     }
    50. }
     

    Attached Files:

    • 1.txt
      File size:
      3.4 KB
      Views:
      12
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    104
    Do you have newlines in the text that might be tricking your split into have elements like "0001\n0001"?
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,061
    That looks fine to me... any chance of seeing the actual data you're parsing? My guess would be that some of those " " are actually " " or " ", and it's just hard to tell by looking at it.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    4,890
    looks like the text file only has 33 spacing per row, so 33*26=858
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,061
    The issue is your newlines. Notice the result of parsing this:

    upload_2019-1-11_13-11-47.png

    So, you can either first merge all the lines together on " ", or process your text one line at a time.
     
  6. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,293
    I've changed my code to this:
    Code (CSharp):
    1. string array = asciiArray.text;
    2.         array.Replace("\n", "   ");
    3.         arrayContents = array.Split(new string[] { "   " }, StringSplitOptions.None);
    which I thought might replace the new lines with three spaces, but I think it's looking for the characters \n, not a new line.
     
  7. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    104
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,061
    It's easier just to split on whitespace instead of messing with the newlines explicitly:

    Code (CSharp):
    1. var array = Regex.Split(input, "\\s+");
    Regex.Split(input, "\\s+");

    That will split on any whitespace (spaces, tabs, newlines), which also conveniently means if you ever put two spaces instead of 3, that'll still work.
     
    Kurt-Dekker likes this.