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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Automatically define class as list possible?

Discussion in 'Scripting' started by padami, Jul 31, 2020.

  1. padami

    padami

    Joined:
    May 19, 2015
    Posts:
    8
    Hello forum!

    Quick question, probably fairly easy, but not getting results on google/unity forum.
    I have a custom class, which is straight forward:

    Code (CSharp):
    1. // This works!
    2. public class TestData
    3. {
    4.     public string ID;
    5.     public Vector3 position;
    6.     public Vector3 rotation;
    7. }
    8.  
    9. public class Test : MonoBehaviour
    10. {
    11.     public List<TestData> testData = new List<TestData>();
    12. }
    13.  
    I'm using the class TestData very much as a list, and every time In my scripts I have to write:

    Code (CSharp):
    1. public List<TestData> {NAME} = new List<TestData>();
    I'm wondering if it's possible to automatically define the TestData class as a list?
    For example something like this:

    Code (CSharp):
    1. public class TestData
    2. {
    3.     public string ID;
    4.     public Vector3 position;
    5.     public Vector3 rotation;
    6. }
    7.  
    8. // Is this possible?
    9. public class TestDataList
    10. {
    11.     public List<TestData>(); // Define automatically as list?
    12. }
    13.  
    14. public class Test: MonoBehaviour
    15. {
    16.     public TestDataList testData = new TestDataList(); // So i can skip the List<TestData> at every declaration?
    17. }
    Thanks in advance! :)
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    In theory yes, by making TestDataList inherit from List<TestData> like so:
    Code (csharp):
    1.  
    2. public class TestDataList : List<TestData>
    3. {
    4. }
    5.  
    BUT

    There is a problem where the Unity serialization engine and inspector won't treat this object as a list. Which sucks.

    In the end though I don't see the difference between saying "TestDataList" and "List<TestData>". They're nearly the same number of chars (2 extra with the < >).

    You could also just use an array if you don't expect to resize it in code:
    Code (csharp):
    1.  
    2. public class Test : MonoBehaviour
    3. {
    4.     public TestData[] testData;
    5. }
    6.  
    Also note that your example with the List as a member TestDataList will actually work:
    Code (csharp):
    1.  
    2. public class TestDataList
    3. {
    4.     public List<TestData> lst = new List<TestData>(); //needs to be defined as a member
    5. }
    6.  
    The only problem is that it'll be in a nested object. Which will look weird in the inspector, and you'll need to access it as 'testData.lst', or whatever you named it.

    Lastly, when you say:
    I want to clarify... you're not using TestData as a list. You're using TestData in a list. TestData is still a single object, you just generally use TestData as a member of a list because you expect multiple TestData instances.
     
    Last edited: Jul 31, 2020
    padami and Brathnann like this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    What's wrong with defining it as a list?

    Maybe if you defined a use case for why you'd want to do this. Just because you think it's less typing? Intellisense helps you fill in pretty much all code. As soon as you type new and hit space, it gives you a solution that you can select to fill in the rest of it.

    I'm honestly trying to think of when I might use something like that. So I'm just curious.
     
    padami likes this.
  4. padami

    padami

    Joined:
    May 19, 2015
    Posts:
    8
    Meh, I was just wondering if it could be written shorter; it's a case of programmer's laziness, thanks for the answers! haha! :)
     
  5. padami

    padami

    Joined:
    May 19, 2015
    Posts:
    8
    Agreed! I used the wrong terminology :)
     
  6. padami

    padami

    Joined:
    May 19, 2015
    Posts:
    8
    Yes i tried that aswell, but in the end declaring
    Code (CSharp):
    1. public List<TestData> {NAME} = new List<TestData>();
    makes more sense to me than writing
    Code (CSharp):
    1. public TestDataList {NAME};
    and accessing testData.lst!

    Thanks!