Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

System.Serializable Array Setting Incorrect Length?

Discussion in 'Scripting' started by renman3000, Jul 17, 2018.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Hi there,
    I have an editor script using System.Serializable, to allow me to use a 2D array in a custom script, where I can manipulate the settings of the 2D array in the editor.

    Gernally it works, but no matter what I do, despite the Length of each axis of the 2D array being what ever I set it to be in code (* with the proper results showing in the editor), at run, at print to console, the Length of each axis prints as 11. This, as such is causing some "funkiness" in my results.

    This is perplexing and I cant figure out why. I have attached images of the two scripts that should be all, that is relevant.

    Any help is appreciated!
    Thanks!



     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,419
    If the array has a fixed width, you can "simulate" a 2D array rather easily with an 1D array like below (a 2d array is pretty much this in memory anyway):
    Code (CSharp):
    1. item = array[y][x]; // 2d array
    2. item = array[y*width+x]; // 1d array
    This, of course, does not work if the width is variable per row.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Sorry,
    I was used to posting on Facebook.

    As for posting the runtime code, you mean the console log?


    edit.
    Here is current code also code that I am using to layout out tiles, in a grid. A implemented a hack solution to end the process at a set int value.

    Thanks

    Editor Script.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. [CustomPropertyDrawer(typeof(CustomUnitType))]
    7. public class CustomTileData : PropertyDrawer
    8. {
    9.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    10.     {
    11.         EditorGUI.PrefixLabel(position, label);
    12.        
    13.         Rect newPosition = position;
    14.         newPosition.y += 20f;
    15.         SerializedProperty rows = property.FindPropertyRelative("rows");
    16.  
    17.         int LengthOfArray = 7;
    18.        
    19.         for(int i = 0; i < LengthOfArray; i++)
    20.         {
    21.             //Debug.Log("-------------- ED: i " + i);
    22.             SerializedProperty row = rows.GetArrayElementAtIndex(i).FindPropertyRelative("row");
    23.             newPosition.height = 20;
    24.  
    25.             if (row.arraySize != LengthOfArray){
    26.                 //Debug.Log("row array size != Length of Array... in is/ " + row.arraySize);
    27.             }
    28.                
    29.             newPosition.width = 20;
    30.  
    31.             for(int j=0; j < LengthOfArray; j++)
    32.             {
    33.                 //Debug.Log("ED: j " + j);
    34.                 EditorGUI.PropertyField(newPosition, row.GetArrayElementAtIndex(j), GUIContent.none);
    35.                 newPosition.x += newPosition.width;
    36.             }
    37.  
    38.             newPosition.x = position.x;
    39.             newPosition.y += 20;
    40.         }
    41.     }
    42.  
    43.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    44.     {
    45.         return 20 * 9;
    46.     }
    47. }
    48.  


    Reference to editor script (much of these two scripts are copy pasted from online references)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [System.Serializable]
    6. public class CustomUnitType
    7. {
    8.     [System.Serializable]
    9.     public struct rowData
    10.     {
    11.         public bool[] row;
    12.     }
    13.  
    14.     public rowData[] rows = new rowData[7];
    15. }
    16.  

    The script I am using in game to create the floor tiles.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class GroundsUnit : MonoBehaviour {
    7.  
    8.  
    9.  
    10.     public GameObject tilePrefab;
    11.     [Space]
    12.     public CustomUnitType customUnitType;
    13.     /// <summary>
    14.     /// hack value to lock the tile layout at xLengthLimit x aLengthLimit.
    15.     /// </summary>
    16.     int xLengthLimit = 7;
    17.  
    18.  
    19.     float x_factor = 2;
    20.     float z_factor = 1.8f;
    21.  
    22.    
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.  
    27.         Invoke("setUnit", 1);
    28.  
    29.  
    30.        
    31.     }
    32.    
    33.  
    34.     void setUnit(){
    35.  
    36.         for (int i = 0; i < customUnitType.rows.Length; i++)
    37.         {
    38.  
    39.             if (i < xLengthLimit)
    40.             {
    41.                 //print(" --===  rows legth (i = " + i + ")  ====--");
    42.                 for (int j = 0; j < customUnitType.rows[i].row.Length; j++)
    43.                 {
    44.                     if (j < xLengthLimit)
    45.                     {
    46.                         createUnit(i, j);
    47.                     }
    48.                 }
    49.             }
    50.         }
    51.  
    52.     }
    53.  
    54.     void createUnit(int x, int z){
    55.        
    56.         bool unit = customUnitType.rows[x].row[z];
    57.         if(unit){
    58.  
    59.             return;
    60.  
    61.         }
    62.         else{
    63.  
    64.  
    65.             //create prefab.
    66.             GameObject gm = Instantiate(tilePrefab, transform);
    67.  
    68.             //get and set local position.
    69.             float xPos = ((float)x * x_factor) - (3 * x_factor);
    70.             float zPos = ((float)z * z_factor)  -(3 * z_factor);
    71.             Vector3 pos = new Vector3(xPos, 0, zPos);
    72.             gm.transform.localPosition = pos;
    73.  
    74.         }
    75.  
    76.     }
    77. }
    78.  
     
    Last edited: Jul 17, 2018