- Home /
C#: Serialize a class as Field
I want to make a class which will edit a material on runtime.
I started by making a class called EditMaterial. In that class I created a subclass called Textures. I posted the whole script here:
using UnityEngine;
using System.Collections;
public class EditMaterial : MonoBehaviour {
public Color color;
public Shader shader;
[SerializeField]
public class Textures{
[SerializeField] public Texture mainTexture;
[SerializeField] public Texture bumpMap;
[SerializeField] public Texture cube;
}
// Use this for initialization
void Awake () {
renderer.material.color = color;
renderer.material.shader = shader;
}
}
Here you can see my screenshot of unity.
The color and the shader are just fine. But the class Textures won't appear!
Answer by gjf · Jul 05, 2014 at 10:43 AM
think your [SerializeField]
on line 9 needs to be [Serializable]
EDIT: and some instance of the Textures
class such as:
public List<Textures> MyTextures = new List<Textures>();
Thats the wierd thing, I tried that too. I also tried to create a struct. Nothing works! Not even the test script in the unity documentation.
I did it now:
[System.Serializable]
public class TextureFields {
[SerializeField] public Texture mainTexture;
[SerializeField] public Texture bump$$anonymous$$ap;
[SerializeField] public Texture cube;
}
public TextureFields textures = new TextureFields();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
Thanks a lot!
not near a machine to test, but i don't think you need [SerializeField]
either
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Serialization - Variables won't change on original construction 1 Answer
Distribute terrain in zones 3 Answers
Why is my C# array of objects fully populated when some of them should be null? 3 Answers
Serialized Field on Scriptable object is lost on editor reload 1 Answer