1. Brief Answer Questions
(1) Explain the differences and connections between Game Objects and Assets.
Game object is a real object in the game, which can be selected and operated, while resources are a modification of the game object, such as a cube is the game object, but the specific information such as the pattern on its surface is set by resources.
(2) Download several game cases and summarize the structure of resource and object organization respectively (referring to the directory structure of resource and the hierarchy structure of game object tree)
In unity, the directory structure of the game's resources generally includes scripts and various material packages, which are similar to the types used by different objects, such as color, material and so on.
The directory structure of object organization is generally subordinate structure, control of some objects, control of resources and environment, etc.
(3) Write a code that uses debug statements to verify the basic behavior of MonoBehaviour or the conditions under which events trigger
The basic behavior includes Awake() Start() Update() FixedUpdate() LateUpdate()
Common events include OnGUI (), OnDisable (), OnEnable ()
First, write script files in C # language
using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { // Start is called before the first frame update void Start() { Debug.Log("start\n"); } // Update is called once per frame void Update() { Debug.Log("update\n"); } private void Awake() { Debug.Log("awake\n"); } private void FixedUpdate() { Debug.Log("fixedupdate\n"); } private void LateUpdate() { Debug.Log("lateupdate\n"); } private void OnGUI() { Debug.Log("ongui\n"); } private void OnDisable() { Debug.Log("ondisable\n"); } private void OnEnable() { Debug.Log("onenable\n"); } }
So whether it is executed or not only needs to observe whether there is corresponding output.
The results are as follows:
You can see that the Awake function is called at the beginning of the program, then Onenable is called, then the Start function is executed, and then Fixed Update is executed before the Update function is started.
So to sum up, at the same time consult the relevant information:
Awake function: Called when the script is loaded.
Start function: Called the first time you enter the game.
FixedUpdate function: Called during game loops.
Update function: When the start function is executed, it is called into the game loop.
LastUpdate function: Called after all Update functions have been called.
OnGUI function: The game loop is called during the rendering process after the scene is rendered.
OnDdiable function: Called when an object is disabled.
OnEnable function: Called when disabled.
(4) Find script manuals and learn about GameObject, Transform, Component objects.
a. Translate official descriptions of three objects separately
GameObject: The most important concept in unit editing. Every object in the game is GameObject, from objects to props to lights, cameras, etc. But it can't do anything by itself. It needs to give it a character, environment or special influence before it becomes a role.
Transform: Used to store the position, rotation angle and size of a game object. Each GameObject has a Transform component attached to it.
Component: Components are nuts and bolts for objects and actions in the game. They are functional components for each game object.
b. Describe the attributes of the table object (entity) in the figure below, the attributes of the table's Transform, and the components of the table.
The requirement of this topic is to match the visual graphical programming interface with the Unity API. When you are on the Inspector panel, you should know the corresponding API.
For example, the object of table is GameObject, and the first selection box is the activeSelf attribute.
The second is Transform, the third is MeshFilter, the fourth is BoxCollider, and the fifth is MeshRenderer.
c. Use UML diagrams to describe the relationship between the three (please use UMLet 14.1.1 stand-alone version to map out).
(5) collate the relevant learning materials and write simple code to verify the implementation of the following technologies:
a. Find objects:
Debug.Log("start\n"); GameObject cube; cube = GameObject.Find("cube"); if(cube!=null) { Debug.Log("find\n"); } else { Debug.Log("did not find\n"); }
The results are as follows.
b. Adding child objects
void Start() { Debug.Log("start\n"); GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.name = "cube1"; cube.transform.position = new Vector3(0, Random.Range(0, 5), 0); cube.transform.parent = this.transform; }
The results are as follows:
c. Traversing Object Tree
void Start() { Debug.Log("start\n"); foreach (Transform child in transform) { Debug.Log(child.name); } }
The catalogue structure is as follows:
The results are as follows:
d. Clear all child objects
void Start() { Debug.Log("start\n"); foreach (Transform child in transform) { Destroy(child.gameObject); } }
The catalogue structure is as follows:
The results are as follows:
(6) Prefabs and clone
a. What are the benefits of Prefabs?
As a reusable game object, it can be understood as a well-configured object, which can be easily added to a game and save work. And using the same presupposition in the game is a clone of it, and when it changes itself, all of its clones will be changed.
b. The relationship between presupposition and clone or copy or Instantiate of Unity Object?
Presupposition and cloning can replicate a large number of identical objects to the same object, but the presupposed cloned object is related to its ontology. When the presupposed object changes, all cloned objects will change. But if the object is created by cloning, the created object will be no longer related to the cloned object. Relations can be changed at will.
c. Make table prefabrication, write a piece of code to instantiate table prefabricated resources into game objects
First, you need to create a Resources folder under the Assets folder
Then drag the cube object in.
void Start() { Debug.Log("start\n"); GameObject cube1 = Instantiate(Resources.Load("cube") as GameObject); cube1.transform.parent = this.transform; }
The object directory is as follows:
The results after operation are as follows:
Programming Practice, Games
Game content: Jingzi or loan calculator or simple calculator, etc.
Technical limitations: Only IMGUI is allowed to build UI
Operational purposes:
Understanding OnGUI() events to enhance debug capabilities
Improve the ability to read API documents
The following is a well-written game of jingzi.
The main idea is to control the whole game process in OnGUI function. Every click will determine who is going to go down, whether the current position can go down, whether someone wins, what should be popped up to win or draw, and restart the game. The logic of the algorithm is not complicated, mainly for some function calls, the implementation code is as follows:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { private int[,] map = new int[3, 3];//Create a chessboard private int turn = 0; private int num = 0; private int size = 50; // Start is called before the first frame update void Start() { num = 0;//Number of chess games turn = 1 - turn;//Take turns to be the first for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { map[i,j] = 0; } } } // Update is called once per frame void Update() { //Debug.Log("update\n"); } private int getwin() { for(int i=0;i<3;i++) { if (map[i, 0] == map[i, 1] && map[i, 1] == map[i, 2]) return map[i, 1]; if (map[0, i] == map[1, i] && map[1, i] == map[2, i]) return map[1, i]; } if (map[1, 1] == map[0, 0] && map[1, 1] == map[2, 2]) return map[1, 1]; if (map[1, 1] == map[0, 2] && map[1, 1] == map[2, 0]) return map[1, 1]; if (num == 9) return 3; return 0; } private void OnGUI() { GUI.skin.button.fontSize = 20; GUI.skin.label.fontSize = 20; if (GUI.Button(new Rect(300, 250, 150, 50), "Reset")) { num = 0; turn = 1 - turn;//Take turns to be the first for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { map[i, j] = 0; } } } int win = getwin(); if(win==1) GUI.Label(new Rect(350, 50, 100, 50), "O wins"); else if(win==2) GUI.Label(new Rect(350, 50, 100, 50), "X wins"); else if(win==3) GUI.Label(new Rect(350, 50, 100, 50), "Draw"); for (int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(map[i,j]==1) { GUI.Button(new Rect(i * size + 300, j * size + 100, size, size), "O"); } else if(map[i,j]==2) { GUI.Button(new Rect(i * size + 300, j * size + 100, size, size), "X"); } else if(GUI.Button(new Rect(i * size + 300, j * size + 100, size, size), "")) { if (win == 0) { if (turn == 0) { map[i, j] = 1; } else map[i, j] = 2; turn = 1 - turn; num++; } } } } } }
The operation interface is as follows:
So far, the requirements have been basically fulfilled.