- Home /
The question is answered, right answer was accepted
How to declare a multidimensional array of strings in c#?
I've tried some stuff like
public string[,] = new string[0,0];
and this line doesn't give me any errors in monodev or unity console, but it doesn't show up in the inspector, even if I place serialize field. I can still dynamically set strings with code, but can't give a predefined item in the inspector.
Is this supposed to happen?
Is there any way I can get into the inspector some kind of dynamic 2d array of type string?
This is exactly what I want, but this answer was for javascript.
Thanks
Answer by Mortennobel · May 14, 2011 at 06:26 AM
You can create your own string array class and create an array of this:
using UnityEngine; using System.Collections;
[System.Serializable] public class MultidimensionalString { public string[] stringArray = new string[0];
public string this[int index] {
get {
return stringArray[index];
}
set {
stringArray[index] = value;
}
}
public int Length {
get {
return stringArray.Length;
}
}
public long LongLength {
get {
return stringArray.LongLength;
}
}
}
Usage:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class ArrayTest : MonoBehaviour {
public MultidimensionalString[] multidimensional = new MultidimensionalString[10];
public void Start(){
multidimensional[1].stringArray = new string[4];
multidimensional[1][1] = "test";
Debug.Log(multidimensional[1][1]);
}
}
This answer is based on the following answer:
http://answers.unity3d.com/questions/47049/visializing-a-multidimensional-array
An alternative is to create a custom editor for that gives access to a specific GameObject that contains either multidimensional array of arrays-of-arrays. Note that this is a less general approach that the MultidimensionalString.
http://unity3d.com/support/documentation/ScriptReference/Editor.html
Thanks very much, I like the first option it is quite perfect for my needs
I added support for index-operator as well. Then it will work more like a real array.
Updated again - I added a Length property ... like the array :-)
Thanks :) This allowed me to create an array of a class to use to store 'Alien Race' data for my project that $$anonymous$$ono would not allow.
Answer by grovalmitch · May 16, 2013 at 06:30 AM
here yo can see a simple multidimesional array c# sample code
http://net-informations.com/csprj/statements/cs-array.htm
groval
recently i tried to use multidimensional string array to store color of an object for that i have used this code
private string[,,] colorArray; colorArray[0,0, 0] = obj.color.ToString();
that thing works fine in visual studio and returns color as well
Follow this Question
Related Questions
String array not working 1 Answer
How to convert a string to int array in Unity C# 1 Answer
convert string to byte array 3 Answers
Javascript - Calling a random String 2 Answers
Array of Array 1 Answer