Catlike Coding - Constructing a Fractal Flashcards

1
Q

What is MeshFilter?

A

A class that inherits from component which is used to access Mesh of the MeshFilter.

Since MeshFilter inherits from Component, we can also say that it is a Unity component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Mesh?

A

A class that allows creating or modifying meshes from scripts.

Mechanically, a Mesh is a construct used by the graphics hardware to draw complex stuff. It’s a 3D object that’s either imported into Unity, is of Unity’s default shapes, or generated by code.

A mesh contains at least a collection of points in 3D space plus a set of triangles – the most basic 2D shapes – defined by these points. The triangles constitute the surface of whatever the mesh represents. Often, you won’t realize that you’re looking at a bunch of triangles instead of a real object.

The triangle arrays are simply indices into the vertex arrays; three indices for each triangle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is MeshRederer?

A

Renders meshes inserted by the MeshFilter or TextMesh.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s a Material?

A

Materials are used to define the visual properties of objects. They can range from very simple, like a constant color, to very complex.

Materials consist of a shader and whatever data the shader needs. Shaders are basically scripts that tell the graphics card how an object’s polygons should be drawn.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When is Start invoked?

A

The Start method is called by Unity after the component (the script itslef) is created, once it’s active, and just before the first time its Update method would be called, if it had one.

It’s only called once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does AddComponent work?

A

Adds a component class of type to the game object - either current object or a specified object.

Remember that all components must be attached to a gameObject.

Current game object is always referred to within script as gameObject, without the need to create a variable/instantiate an object as it has already been done for you in UnityEngine namespace under the component class.

Not sure if the underlined is correct (ignore it for now)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Generic Method?

A

A method template that can work with a range of types. You tell it what type to use by mentioning it between angle brackets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is one way you create a cube from script and add it to the scene? - Assume that you have an empty game object

A

Any primitive you want to add to your scene will need a mesh and a material

You add “your” specified mesh and material to an object by accessing the mesh and material properties (of type Mesh and Material) from the MeshFilter class and the MeshRenderer Class respectively.

Finally you select the mesh and material you want from the “Unity properties” in the Unity editor.

Mesh mesh;

Material material;

GameObject.AddComponent<meshfilter>().mesh = mesh;</meshfilter>

GameObject.AddComponent<meshrenderer>().material = material;</meshrenderer>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a GameObject, call it “Le New” and add a component to it (Do it in both ways).

A

new GameObject(“Le New”).AddComponent();

new GameObject(“Le New”, typeof(Script));

REMEMBER THAT ANY SCRIPT ATTACHED TO A GAMEOBJECT IS IN ITSLEF A COMPONENT!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does “new” do?

A

The new keyword is used to construct a new instance of an object or a struct.

It is followed by a special constructor method, which has the same name as the class or struct it belongs to.

Below are some example constructors from the class GameObject:

<em>public GameObject(string name, params Type[] components);</em>

<em>public GameObject(string name);</em>

To instantiate the above, you just add the new keyword followed by the constructor in your “sketch”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is “this”?

A

This is the current object/struct whose method is being called.

This is the instance of the script you’re in.

It is being used implicitly the whole time when refering to stuff from the same class

Layman’s terms:

Lets say you create an object from a class

MyObject myObject_1 = new MyObject;

Now when refering to object in another class you use myObject_1 to do so. But lets say you’re in MyObject class, and you want to refer to the instance of MyObject, in this case you use the keyword “this”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create a Recursive GameObject that re-initilizes itself 4 times only and make each object the child of the previous.

A

int depth = 0, maxDepth = 4;

if (depth < maxDepth) {

new GameObject(“MyRecursiveObject”).AddComponent.Initialize(this)

}

void Initialize(Sketch sketch) {

maxDepth = sketch.maxDepth;

depth = sketch.depth + 1;

transform.parent = sketch.transform;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Decribe what this line does and the method below

new GameObject(“Child of Fractal”).AddComponent < sketch > (). Initialize(this);

private void Initialize(Test sketch) {

myMesh = mesh.sketch;

}

A

This line does 4 things

  • Creates a new game object
  • gives it a name
  • Adds a component to it
  • Does whatever the initialize method tells it to do.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe what each one of these lines does:

transfrom. localScale
transform. localPosition

Vector3.up

Vector3.one

A

Scale relative to parent

Position relative to parent

Where is up in the scene compared to the real world? (up in this case - also this is a shorthand for new Vector 3 (0,1,0)

Shorthand for new Vector3 (1,1,1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the concept of creating smaller cubes from a larger one positioning each one incrementally further the the previous.

A

Each cube needs to change size at a fixed scale relative to the parent cube.

Each cube also needs to change position relative to the previous position.

Our first step is to create a scalar variable which we will use to:

Multiply the size of each cube, where each cube’s size is assumed to be one.

As for the position, we also use the scalar to multiply the poistion of each cube to get the position of the new one. In this case of distances, however, we also add a fixed distance to each new cube in order to offset the distance relative to the size of the cube.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In Layman’s terms, how would you create multiple children positioned at different places.

A

Put simply, just get the line that detrmines the local position in the initialize method and instead of using “vector3.up/left etc), just use a variable called direction.

Next just call the “new GameObject” as many as the number of directions you would want your fractal direction to move in, adding the direction you want as an argument to the direction parameter in the initialize method.

17
Q

What is a coroutine?

A

Think of coroutines as methods in which you can insert pause statements. While the method invocation is paused, the rest of the program continues. Though this point of view is too simplistic, it is all we need to make use of it right now.

18
Q

What is an IEnumerator?

A

Enumeration is the concept of going through some collection, one item at a time, like looping over all elements in an array. An enumerator – or iterator – is an object that provides an interface for this functionality. System.Collections.IEnumerator describes such an interface.

Why do we need this? Because coroutines use them. This is also why Unity includes System.Collections in their default script template, and why I included it as well.

19
Q

What does yield do?

A

The yield statement is used by iterators to make life easy for them. To make enumeration possible, you’d need to keep track of your progress. This involves some boilerplate code that is essentially always the same. What you’d really want is to just write something like return firstItem; return secondItem; until you are done. The yield statement allows you to do exactly that.

So whenever you’re using yield, an enumerator object is created behind the scenes to take care of the tedious bits. That’s why our CreateChildren method has IEnumerator as its return type.

By the way, you can also yield another iterator. In that case this other iterator will be processed completely, so you can stitch them together in creative ways.

20
Q

Where do you call coroutines?

A

In Unitiy’s startCoroutine method (I have ony seen it called in start).

21
Q

What is WaitForSeconds?

A

Suspends the coroutine execution for the given amount of seconds using scaled time.

WaitForSeconds can only be used with a yield statement in coroutines.

You use it by creating it as a new object:

yield return new WaitForSeconds(0.5f);

22
Q

Recreate the corouitine from http://catlikecoding.com/unity/tutorials/constructing-a-fractal/

A

private void Start () {

if (depth < maxDepth) {

<strong>StartCoroutine(CreateChildren()); </strong>

}

}

private <strong>IEnumerator </strong>CreateChildren () {

yield return new WaitForSeconds(0.5f);

new GameObject(“Fractal Child”). AddComponent().Initialize(this, Vector3.up);

yield return new WaitForSeconds(0.5f);

new GameObject(“Fractal Child”). AddComponent().Initialize(this, Vector3.right);

}

23
Q

Briefly how was the code simplified for creating multiple children?

A
  • The initialize function takes 2 arguments (2nd arg is iterator which is an int)
  • int parameter is used by initialize function to determine which of the directions / orientations is used.
  • directions / orientations are arrays containing each direction / orientation required
  • We put the new Gameobject in a for loop in order to have an object created for each orientation / direction
24
Q

How would you change color of a material?

A

GetComponent<meshrenderer>().material.color = Color.red</meshrenderer>

25
Q
A