Home > OS >  Adding a texture to a serialized list
Adding a texture to a serialized list

Time:07-07

I have a list of textures present inside TextureInfo. I would like to add a texture to a list as shown in example below.

[System.Serializable]
 public class TextureInfo
  {
    public List<Texture> textures = new List<Texture>();
  }

public List<TextureInfo> texInfo = new List<TextureInfo>();

public Texture texture;

public void Start(){
 texInfo.textures.Add(new TextureInfo {textures.Add(texture)}); //How do I add one row to texInfo and then one row to textures?
}

Problem is I do not understand how to add to a serialized list.

CodePudding user response:

I would just leave the one liners in this occation and fix it like this.

     [System.Serializable]
     public class TextureInfo
      {
        public List<Texture> textures = new List<Texture>();
      }
    
     public List<TextureInfo> texInfo = new List<TextureInfo>();
    
     public Texture texture;
     public void Start()
     {
        var variableName = new TextureInfo();
        variableName.textures.Add(texture);

        texInfo.Add(variableName);
     }

CodePudding user response:

TextureInfo tinfo = new TextureInfo(); //Create new texture info
tinfo.textures.Add(texture); // Add texture to the list of textures in texture info
texInfo.Add(tinfo); // add texture info to the texture info list
  • Related