- Home /
see C# 2D Generic List in inspector
Hi everybody,
I've got a "containerList" that have 10 Lists with 45 items in it. The items are made from this class (which is serialized) :
using System;
using UnityEngine;
using System.Collections;
[Serializable]
public class SaveTalentClass {
[SerializeField]
public int talId;
public int talCurRank;
public int talMaxRank;
public SaveTalentClass(int id, int cRank, int mRank)
{
talId = id;
talCurRank = cRank;
talMaxRank = mRank;
}
public SaveTalentClass()
{
}
}
And I initialize my lists through a for loop like this :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Linq;
[System.Serializable]
public class PlayerData : MonoBehaviour {
[SerializeField]
public List<List<SaveTalentClass>> containerList = new List<List<SaveTalentClass>>();
void Start ()
{
for(int i = 0; i < 10; i++)
{
containerList.Add (new List<SaveTalentClass>());
for(int j = 0; j < 45; j++)
{
containerList[i].Add (new SaveTalentClass(j,0,0));
}
}
}
But I can't see my "containerList" in the inspector... I found this thread on a similar issue http://answers.unity3d.com/questions/289692/serialize-nested-lists.html but wasn't able to adapt my script to make it work.
Can anyone be kind enough with a beginner and explain me how can I do ?
Answer by Shark-Boy · Jun 17, 2015 at 04:35 PM
So I had the same problem a little while ago and it took quit the search to find a solution. But I did get a working solution here it is.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour
{
public List<ListableClass> containerList = new List<ListableClass>();
}
[System.Serializable]
public class ListableClass
{
public List<SaveTalentClass> innerList = new List<SaveTalentClass>();
}
I did use a array insted of a list in my code so if it still wil not work you could try that but it should work fine the way it is.
Thank you for the tips but unfortunately it didn't work. It seems that your solution isn't using a multidimensionnal List like I do for holding the 10 initialized List in it.
$$anonymous$$aybe you made a type error in your provided code, Or (another highly probable possibility) I'm really a beginner and wasn't able to implement it right ;)
In fact I didn't understood the concept behind that class List. When do I have to use the "innerList" ? During initialization in the for loop ?
I tried this (I commented stuff that we don't need yet):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Linq;
public class PlayerData : $$anonymous$$onoBehaviour {
int activeTree;
// Talent$$anonymous$$anager manager;
TalentDatabase database;
public List<List<ListableClass>> containerList = new List<List<ListableClass>>();
[System.Serializable]
public class ListableClass
{
public List<SaveTalentClass> innerList = new List<SaveTalentClass>();
}
void Start ()
{
// manager = GameObject.Find ("Talent$$anonymous$$anager").GetComponent<Talent$$anonymous$$anager>();
database = GameObject.Find ("TalentDatabase").GetComponent<TalentDatabase>();
/* if(File.Exists(Application.persistentDataPath + "/talentList.dat"))
{
var b = new BinaryFormatter();
var f = File.Open(Application.persistentDataPath + "/talentList.dat", File$$anonymous$$ode.Open);
containerList = (List<ListableClass>)b.Deserialize(f);
f.Close();
}
else
{
*/ for(int i = 0; i < 10; i++)
{
containerList.Add (new List<ListableClass>());
for(int j = 0; j < 45; j++)
{
containerList[i].Add (new SaveTalentClass(j,0,database.talents[j].tal$$anonymous$$axRank));
}
}
// SaveLists();
//}
}
/* public void SaveLists ()
{
var b = new BinaryFormatter();
var f = File.Create(Application.persistentDataPath + "/talentList.dat");
b.Serialize(f, containerList);
f.Close();
}
public void ResetLists ()
{
GameObject.Find ("TalentsPanel").Broadcast$$anonymous$$essage("ResetRank");
}
void Update ()
{
activeTree = manager.activeTree;
if(Input.Get$$anonymous$$eyUp("space"))
{
Test();
}
}
void Test()
{
print (containerList[activeTree][0].talCurRank);
}*/
}
But as expected, it didn't work, errors are :
error CS1502: The best overloaded method match for `System.Collections.Generic.List.Add(PlayerData.ListableClass)' has some invalid arguments
error CS1503: Argument `#1' cannot convert `SaveTalentClass' expression to type `PlayerData.ListableClass'
So the listable class must go outside of the public class PlayerData like this.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// and so on
public class PlayerData : $$anonymous$$onoBehaviour { // opening bracket for class
int activeTree;
// and so on
// a list of the listableclass
public List<ListableClass> containerList = new List<ListableClass>();
void Update() { // a function
}
} //a closing bracket for the class
// a new class
[System.Serializable]
public class ListableClass { //a class with a list in it
public List<SaveTalentClass> innerList = new List<SaveTalentClass>();
}
the listableclass has a list in it so if you make a list of listableclass's (like the one above) then you have a list of lists that will show up in the unity editor. So that should work now :)
Thank you for helping me but it still doesn't work. Am I suppose to run the for loop to create the 10 Lists into the InnerList in a Start function inside the ListableClass ?
That really puzzle me... Once again, thank you for helping me and sorry for my lack of knowledges :(
Answer by florinel2102 · Apr 21 at 10:27 PM
[System.Serializable]
public class SimpleList<T> where T: class
{
public List<T> myList;
}
After you import the class in your project , you can simply reuse it whenever you want by List<SimpleList<SaveTalentClass>>
or SimpleList<SaveTalentClass>[]
Your answer
Follow this Question
Related Questions
Multidimensional PlayerPrefs? 1 Answer
Generic List.Count always gives 0 2 Answers
Unity Editor Memory Spike Generic List 0 Answers
Push Custom Class to Array (JavaScript) 1 Answer
convert string to list of lists 1 Answer