I'm following a tutorial on procedurally generated meshes and I am wondering why one can .Add() to a List which is a property of a class when that List only has a get accessor. I'm new to C#, what am I missing here?
For example, the tutorial has the user write this class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MeshBuilder
{
private List<Vector3> m_Vertices = new List<Vector3>();
public List<Vector3> Vertices { get { return m_Vertices; } }
private List<Vector3> m_Normals = new List<Vector3>();
public List<Vector3> Normals { get { return m_Normals; } }
private List<Vector2> m_UVs = new List<Vector2>();
public List<Vector2> UVs { get { return m_UVs; } }
private List<int> m_Indices = new List<int>();
public void AddTriangle(int index0, int index1, int index2)
{
m_Indices.Add(index0);
m_Indices.Add(index1);
m_Indices.Add(index2);
}
public Mesh CreateMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = m_Vertices.ToArray();
mesh.triangles = m_Indices.ToArray();
//Normals are optional. Only use them if we have the correct amount:
if (m_Normals.Count == m_Vertices.Count)
mesh.normals = m_Normals.ToArray();
//UVs are optional. Only use them if we have the correct amount:
if (m_UVs.Count == m_Vertices.Count)
mesh.uv = m_UVs.ToArray();
mesh.RecalculateBounds();
return mesh;
}
}
Then we write another script which uses the class:
using UnityEngine;
using System.Collections;
public class ProcQuad : MonoBehaviour {
float m_Width = 1;
float m_Length = 1;
// Use this for initialization
void Start () {
MeshBuilder meshBuilder = new MeshBuilder();
//set up vertices and triangles
meshBuilder.Vertices.Add (new Vector3(0.0f, 0.0f, 0.0f));
meshBuilder.UVs.Add(new Vector2(0.0f, 0.0f));
meshBuilder.Normals.Add(Vector3.up);
meshBuilder.Vertices.Add(new Vector3(0.0f, 0.0f, m_Length));
meshBuilder.UVs.Add(new Vector2(0.0f, 1.0f));
meshBuilder.Normals.Add (Vector3.up);
meshBuilder.Vertices.Add(new Vector3(m_Width, 0.0f, m_Length));
meshBuilder.UVs.Add(new Vector2(1.0f, 1.0f));
meshBuilder.Normals.Add(Vector3.up);
meshBuilder.Vertices.Add (new Vector3(m_Width, 0.0f, 0.0f));
meshBuilder.UVs.Add(new Vector2(1.0f, 0.0f));
meshBuilder.Normals.Add(Vector3.up);
meshBuilder.AddTriangle(0, 1, 2);
meshBuilder.AddTriangle(0, 2, 3);
MeshFilter filter = GetComponent<MeshFilter>();
if (filter != null)
{
filter.sharedMesh = meshBuilder.CreateMesh();
}
}
// Update is called once per frame
void Update () {
}
}
So why are we able to perform:
meshBuilder.Vertices.Add (new Vector3(0.0f, 0.0f, 0.0f));
and why does it modify the private field m_Vertices when the property Vertices only has a get accessor?
public List<Vector3> Vertices { get { return m_Vertices; } }
Thanks for your insight.
If anyone is interested in the tutorial, I found it here: