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

C# List advice request

Discussion in 'Scripting' started by will_brett, Jun 30, 2014.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Hi everyone,

    I want to create a scriptable object that will hold a list of variables that can be accessed from other scripts.

    What I want to do is save multiple variables per list item as I want to keep them grouped together as there will probably be many entry's which may get confusing.

    e.g.

    Name, Color, Color Code

    Is this the right way to do this?

    Code (CSharp):
    1. List<MyList> myList = new List<MyList>(Name, Color, Color Code);
    2.  
    If it is... is this the way to add a new list item?

    Code (CSharp):
    1. myList.Add( new MyList(Name, Color, Color Code));
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    You should create addiotional class for storing your data structure in one item
    Code (csharp):
    1.  
    2. /// Item
    3. class MyListItem
    4. {
    5.    public string Name {get;set;}
    6.    public Color Color {get;set;}
    7.    public int ColorCode {get;set;}
    8.  
    9.    public MyListItem(string name , color Color, int colorCode)
    10.    {
    11.      Name = name;
    12.      Color = color;
    13.      ColorCode = colorCode;
    14.    }
    15. }
    16.  
    Usage:
    Code (csharp):
    1.  
    2. /// create new collection
    3. List<MyListItem> myList = new List<MyListItem>();
    4.  
    5. /// populate collection with new items
    6. myList.Add( new MyListItem("Name", Color.red, 1));
    7. myList.Add( new MyListItem("Name1", Color.green, 2));
     
    will_brett and Magiichan like this.
  3. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Thanks Patico. Thats a huge help
     
  4. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Also should the class be:

    class MyListItem : ScriptableObject ?
     
  5. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
  6. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    No, in this case it just defines a structure of item in your list. But if you want to show/edit items in Inspector panel, you have to mark MyListItem class with Serializable attribute.

    Code (CSharp):
    1. [Serializable]
    2. class MyListItem
    3. {
    4. ...
    5. }
     
  7. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Thanks again for replying.

    I may have gotten confused here but I think I need to use a scriptableObject as I am trying to create a very basic editor extension and the list will be used to hold the variables created in that editor window. Can this be done with a serializable object?
     
  8. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    You should use Serializable attribute when you making object for editor, especially when you want to show object's properties easy. I propose to use this case:
    Code (csharp):
    1. [Serializable]
    2. public classs MyEdatableObject : ScriptableObject
    3. {
    4.    [SerializedField]
    5.    List<MyListItem> myList = new List<MyListItem>();
    6.    
    7.    [Serializable]
    8.    class MyListItem
    9.    {
    10.     public string Name {get;set;}
    11.     public Color Color {get;set;}
    12.     public int ColorCode {get;set;}
    13.    
    14.     public MyListItem(string name , color Color, int colorCode)
    15.     {
    16.       Name = name;
    17.       Color = color;
    18.       ColorCode = colorCode;
    19.     }
    20.    }
    21.    
    22.    
    23.    void Start()
    24.    {
    25.      /// populate collection with new items
    26.      myList.Add( new MyListItem("Name", Color.red, 1));
    27.      myList.Add( new MyListItem("Name1", Color.green, 2));
    28.      ...
    29.    }
    30. }
     
  9. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Thank you for your help. I really appreciate it. I'll try that out now and then try and recreate it again.