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. Dismiss Notice

Editor, 2D Arrays and Serialization

Discussion in 'Scripting' started by image28, Aug 14, 2014.

  1. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    First time posting a question.

    I'm working on an editor script and ran into what I think may be a bug. But I'm new to serialization so it may be my mistake.

    Code works fine untill I add a 2d array...

    Then when switching from unity to monodevelop with my editor extension open and back I get lots of null references for the array, and my extension window goes blank and wont close.

    Here is some code that reproduces the issue, doesn't do it every time though.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System;
    5. using System.Linq;
    6. using System.Reflection;
    7. using System.IO;
    8.  
    9.  
    10.  
    11. public class serialize : EditorWindow {
    12.  
    13.     backend current;
    14.  
    15.     // Use this for initialization
    16.     [MenuItem("Custom/serialize")]
    17.     public static void ShowWindow()
    18.     {
    19.         EditorWindow.GetWindow (typeof (serialize));
    20.     }
    21.  
    22.  
    23.     void OnEnable()
    24.     {
    25.         if ( current == null )
    26.         {
    27.             current = new backend();
    28.             current.init ();
    29.         }
    30.     }
    31.  
    32.     void OnGUI()
    33.     {
    34.         if ( current == null )
    35.         {
    36.             current = new backend();
    37.             current.init();
    38.         }
    39.  
    40.         GUILayout.Label(current.expandCurrent[1,1].ToString(), GUILayout.MinWidth(200));
    41.         Debug.LogError(current.expandCurrent[1,1]);
    42.     }
    43. }
    44.  
    45. [Serializable]
    46. public class backend
    47. {
    48.     public bool[,] expandCurrent;
    49.  
    50.     public void init()
    51.     {
    52.         expandCurrent = new bool[10,10];
    53.     }
    54. }
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    I'm writing an editor extension that automatically fixes common errors in code like spelling mistakes variables/methods. Casing mistakes in void Start() etc... missing semicolons, missing f when hard-coding a float value and heaps of other common mistakes. Think of it like spell-check for code....

    Good for the new programmer just getting start and for experienced coder who like to write lots of code quickly and with a lot of minor mistakes that can take time to go through and fix, like we all do sometimes....

    Going to officially announce it in the next few weeks, once i work out this bug and test a little more.... Guess I could use Lists.

    Also has a project code overview window, going to add a code statistics window next week. Fixes individual files or all files in a project with undo and change log in case it messes up your code somehow, although I will do my best to prevent that.
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Unity is unable to serialized multidimensional array, jagged array, nested array, list within array or list of arrays.
     
  4. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Ok I've purchased some assets that do something similar to what I want to achieve, so I'll look into their source to see how they overcome this problem... Thanks a lot for the reply
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Checking out your toolbox creator asset and advanced inspector too... at first glance they seem quite handy...
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Or you could use Visual Studio which already does all that out of the box. Well, no automatic fixing, but highlight of any typo or error.

    Usually, for "nesting" collection, the usually solution is to make a collection of object that contains another collection.

    Code (CSharp):
    1. public class MyBehaviour : MonoBehaviour
    2. {
    3.     public MyObject[] collection;
    4.  
    5.     [Serializable]
    6.     public class MyObject
    7.     {
    8.         public bool[] values;
    9.     }
    10. }
    Happy you think so.

    I work under the assumption that if I have to perform the same task more than 10 times, I better find the best way to do it as quickly as possible. :p
     
  7. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Thanks again... I think the same, basically "Don't work harder, work smarter" .... I don't like doing the same thing over and over...

    The checker asset I'm working on fixes a lot of errors Visual Studio/Monodevelop miss, and it just makes it quicker, in the project overview window there is a list of all classes in the project, all variables and functions in the class, and a list of where the variables/functions are used throughout the entire project and what line they are used on, which you can click on to jump to that line in the editor... Also going to have variables that go unused show up in a different colour, and finally have an optimize code window, that can automatically change code to improve performance....