e.g. public class Strategy : List{}
how to serialize Strategy including List
My use case:
A Unit that has a List field
I Instantiate this Unit for battle
The Strategy of the copied Unit has the same count of List, but all the Strategy is empty List
Sorry I don’t understand your question all that well.
Any serialisable members of a base class will be available in any derived classes. They should be copied when you instantiate a prefab with these data.
foreach (var unit in unitList)
{
var unitGo = Instantiate(unit.gameObject, gm.unitTransform);
Unit newUnit = unitGo.GetComponent<Unit>();
playerGroup.Add(newUnit);
//newUnit.strategys = unit.strategys;//I must manual set this. if not, it will be picture 2 situation
newUnit.equipItemList = unit.equipItemList;
}
foreach (var unit in unitList)
{
Unit newUnit = Instantiate(unit, gm.unitTransform);
playerGroup.Add(newUnit);
}
You don’t need to instantiate the game object, you can instantiate the component, which will copy the everything on the game objects its attached to but return the instance of the new component.
Is this unitList a list of prefab references? Also, you aren’t defining the same member in both the base and derived classes?
Just tried Instantiate(unit, gm.unitTransform), but still empty List. like picture 2
The unitList is not a list of prefab references. They have been already instantiated in scene.
I don’t understand you last question. but the Strategy class does not has any members. The base is List, I can’t change that.
Oh you literally are inheriting from List as per your original post. That’s going to be the source of your problem, as that’s akin to doing List<List<string>>, and Unity does not support jagged lists like that. I’m surprised it shows up in the inspector, and would also be surprised if the values survive domain reloads.
You need to make a plain wrapper class whenever you need a list of lists:
[System.Serializable]
public class Stragegy
{
public List<string> Strategies = new List<string>();
}
//usage
public List<Strategy> Strategies = new List<Strategy>();
Oh I see. Thanks for your time.
It’s painful to change it now since I have used this Strategy many times in my code.
I will just set it manually.
Thanks
Before you go too far, make sure this data is being properly serialised. Ergo, does it survive domain reloads, does it end up in builds, does it survive closing and opening the editor.
I have a strong feeling you will have to change it anyway because Unity does not support this method of serialisation.
The units and the strategies will load from save file each time game starts. And will not destroy on loadind scene.
Just each time starting battle, copy the units. It shound be ok.
Sorry ,too much code to change. I prefered set it manually on Instantiation.
I just wanted to know is there a way to serialize it in the way I was using.
I have tried using odin plugin to do this. I don’t remember why I stop using it.
Not using Unity’s serialisation. Like I said, Unity doesn’t support anything beyond basic collections.
You can do it using Odin, yes, though the Odin serialiser should only be used when absolutely necessary.
But often times a wrapper class is better as you generally don’t want to open up every method that a List provides and instead restrict access to only what is needed.