- Home /
Question by
TH_Unity · Sep 17, 2013 at 09:49 AM ·
gameobjectserializablecustom-class
Serializable custom class with game object not shown
So I want to make a custom plane with editable tiles for a simple 2D tile game. It is a small thing to play around with custom inspector, editor and serializable things.
So I have a MonoBehviour "ManagerScript", a custom class "TiledPlane" containing a gameObject.
Here is the code for the monobehaviour:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class ManagerScript : MonoBehaviour
{
[SerializeField]
public TiledPlane tiledPlane;
}
This is the code for the custom class:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class TiledPlane
{
<... Some Mesh data ...>
private GameObject m_gameObject;
public GameObject gameObject
{
get
{
return m_gameObject;
}
private set
{
m_gameObject = value;
}
}
public TiledPlane()
{
CreateDefaultMesh();
}
public void CreateDefaultMesh()
{
<... Some Mesh data ...>
m_Mesh = new Mesh();
m_Mesh.name = "TilePlaneMesh";
m_Mesh.vertices = m_Vertices;
m_Mesh.normals = m_Normals;
m_Mesh.uv = m_UVs;
m_Mesh.triangles = m_Triangles;
if (m_gameObject == null)
m_gameObject = new GameObject("TiledPlane", typeof(MeshFilter), typeof(MeshRenderer));
m_gameObject.GetComponent<MeshFilter>().mesh = m_Mesh;
}
}
Evertime I make changes to my code and switch back to Unity a new GameObject is created and the TiledPlane of the ManagerScript is null. Simply the constructor of TiledPlane is called once.
Comment
Your answer