I have a script that generates a super simple mesh (a quad with a texture) and places it on a sphere.
The script meshgenerator is this one:
[RequireComponent(typeof(MeshFilter))]
public class LeafMeshGenerator : MonoBehaviour
{
Mesh mesh;
Vector3[] vertices;
Vector2[] uv;
int[] triangles;
private void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
//Material leafMaterial = new Material(Shader.Find("Standard"));
//leafMaterial.SetColor("_Color", new Color(0f, 0.7f, 0f)); //green main color
CreateShape();
UpdateMesh();
}
void CreateShape()
{
vertices = new Vector3[]
{
new Vector3(0,0,0),
new Vector3(0,0,.2f),
new Vector3(.2f,0,0),
new Vector3(.2f,0,.2f)
};
uv = new Vector2[]
{
new Vector2(1f,0f),
new Vector2(1f,1f),
new Vector2(0f,0f),
new Vector2(0f,1f)
};
triangles = new int[]
{
0,1,2,
1,3,2
};
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}
At a certain moment, a sphere is generated and during this generation, the mesh created with the script above is instantiated at position (see line 6):
if (!geometry && !tree.skeletonOnly)
{
geometry = GameObject.Instantiate(tree.budPrefab);
geometry.transform.position = transform.position;
geometry.transform.parent = transform;
leaf = GameObject.Instantiate(tree.leafPreFab, geometry.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
leaf.transform.position = geometry.transform.position;
leaf.transform.parent = transform;
}
After some iterations, I have a bunch of spheres with generated mesh attached:

I want to “alternate” the position of generated mesh along all consecutive spheres (children).
It should look like these (I’ve manually moved the generated meshes for visualization purpose):
I was thinking about something that “remembers” where the mesh is placed on previous sphere in hierarchy.
Thank you

