Unity makes small games

foreword

This is the second assignment of the elective course 3D game programming and design, including short answer questions and making a small game with unity. If there are mistakes, welcome and thank you for correcting.

short answer questions

The difference and connection between game objects (GameObjects) and resources (Assets)

Game object : the basic object representing characters, props or scenes in Unity is a container that can accommodate various components (Component) to achieve various functions.
Resources refer to various resource files that may be used in the project, such as models, sound files, texture files, and so on.
Difference : Game objects are objects that exist when the game is running, and assets are resources that can be used in the workspace when making games.
Contact : We can use resources to create game objects. Resources can be a template for us to instantiate a specific game object, or they can be used as an attribute in a game object and used by multiple game objects at the same time.

game case study

insert image description here
Directory organization structure of resources: resources with similar functions are placed in the same directory.
Hierarchical structure of game object tree: the relationship between game objects can be parent or child, and the overall tree structure is formed.

Write a code that uses debug statements to verify the basic behavior of MonoBehaviour or the conditions for event triggers

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class intbesh : MonoBehaviour {
    
    
 
    private void Awake()
    {
    
    
        Debug.Log("awake!");
    }
 
    void Start () {
    
    
        Debug.Log("start!");
	}
	
    void Update () {
    
    
        Debug.Log("update!");
	}
 
    private void FixedUpdate()
    {
    
    
        Debug.Log("fixedupdate!");
    }
 
    private void OnGUI()
    {
    
    
        Debug.Log("ONGUI!");
    }
 
    private void OnDisable()
    {
    
    
        Debug.Log("OnDisable!");
    }
 
    private void OnEnable()
    {
    
    
        Debug.Log("OnEnable!");
    }
}

The console is not displayed because there are too many update outputs. The summary is as follows:

  • Start: Called when the game loop is first entered
  • Update: When the behavior is enabled, update is called every frame
  • Awake: when a script instance is loaded
  • FixedUpdate: Called every time slice when the behavior is enabled
  • OnGUI: Called when rendering and handling GUI events
  • OnEnable: called when the object becomes available or activated
  • OnDisable: Called when the object becomes unavailable or inactive

Find the scripting manual to learn about GameObject, Transform, Component objects

Translate the official descriptions of the three objects respectively (Description)

GameObject is the basic object in Unity, representing characters, props and game scenes. They don't do much by themselves, but they can act as containers for components, and thus achieve real functionality.
The Transform component determines the position, rotation and scale of each object in the scene. Every GameObject has a Transform.
Components are the details of objects and behavior in the game. They are the functional part of every GameObject.

Describe the properties of the table object (entity) in the figure below, the properties of the Transform of the table, and the components of the table

insert image description here
The attributes of the table object: name, activeSelf (the local active state of the GameObject), isStatic, layer (the layer where the game object is located.), tag (the label of the game object)

The properties of the Transform of the table are: Position, Rotation, Scale

The components of the table are: Mesh Filter, Mesh Renderer, Box Collider, First Beh

Resource presets (Prefabs) and object cloning (clone)

What are the benefits of Prefabs?

Presets can quickly create objects with the same properties, and modifying preset properties can modify all corresponding instances at the same time, improving efficiency

What is the relationship between presets and object cloning (clone or copy or Instantiate of Unity Object)?

Instances of object clones do not affect each other, but changes to a preset will affect all instances of the preset.

Make table presets, write a piece of code to instantiate table prefabricated resources into game objects


public class FirstBeh : MonoBehaviour {
    
    

    public GameObject table;

    void Awake()
    {
    
    
    	Debug.Log("awake!");
    }
    void Start () {
    
    
        Debug.Log("Start");
        GameObject aTable = (GameObject)Instantiate(table.gameObject);
        aTable.name = "newTable";
        aTable.transform.position = new Vector3(0,Random.Range(0,5),0);
        aTable.transform.parent = this.transform;
    }

    void Update () {
    
    
        //Debug.Log("Init Update");
    }
}

Programming Practice: Tic Tac Toe Mini Game

Write a c# script and mount it on the camera.
The script code is as follows:

using UnityEngine;
using System.Collections;

public class TTT : MonoBehaviour {
    
    
	//用二位数组存棋盘,0代表空,1代表O,2代表O
	private int[,] chess = new int[3,3] {
    
    {
    
    0,0,0},{
    
    0,0,0},{
    
    0,0,0}};
	//0是O的回合,1是X的回合
	private int turn = 0;
    private int res=0;
	void Start () {
    
    
		
	}
    
	//OnGUI会自动刷新
	void OnGUI() {
    
    
        //x,y,w,h
		if (GUI.Button(new Rect(310,300,100,50),"重新开始"))  reset();
		
		for (int i = 0; i < 3; i++) {
    
    
			for (int j = 0; j < 3; j++) {
    
    
                
				if (chess [i,j] == 1) {
    
    
					
					GUI.Button (new Rect (70 * i+250, 70 * j, 70, 70), "O");
					Debug.Log(i+" "+j+":"+chess[i,j]);
					res=check();
					Debug.Log(res);
				} else if (chess [i,j] == 2) {
    
    
					GUI.Button (new Rect (70 * i+250, 70 * j, 70, 70), "X");
					Debug.Log(i+" "+j+":"+chess[i,j]);
					res=check();
					Debug.Log(res);
				} else {
    
    
					if (GUI.Button (new Rect (70 * i+250, 70 * j, 70, 70), "")) {
    
    
                        
						if (res == 0) {
    
    
							if (turn == 0) {
    
    
								chess [i, j] = 1;
								turn = 1;
							} else {
    
    
								chess [i, j] = 2;
								turn = 0;
							}
						}
                    
					}
				}
                
			}
		}
        res = check();
		if (res == 1) {
    
    
			GUI.Label (new Rect (340, 230, 100, 50), "O赢");
		} else if (res == 2) {
    
    
			GUI.Label (new Rect (340, 230, 100, 50), "X赢");
		} else if(res==3){
    
    
            GUI.Label (new Rect (340, 230, 100, 50), "平局");
        } else{
    
    
            if(turn==0) GUI.Label (new Rect (335, 230, 100, 50), "O的回合");
            else GUI.Label (new Rect (335, 230, 100, 50), "X的回合");
        }
	}
	void reset() {
    
    
		//重开置空
        turn=0;
        res=0;
		for (int i = 0; i < 3; i++) {
    
    
			for (int j = 0; j < 3; j++) {
    
    
				chess [i,j] = 0;
			}
		}
		turn = 0;
	}
	int check() {
    
    
		//检查行
		for (int i = 0; i < 3; i++) {
    
    
			if (chess[i,0]!=0&&chess [i,0] == chess [i,1] && chess [i,1] == chess [i,2]) {
    
    
				return chess [i,0];
			}
		}
		//检查列
		for (int j = 0; j < 3; j++) {
    
    
			if (chess[0,j]!=0&&chess [0,j] == chess [1,j] && chess [1,j] == chess [2,j]) {
    
    
				return chess [0,j];
			}
		}
		//检查对角线
		if ((chess[0,0]!=0&&chess [0, 0] == chess [1, 1] && chess [1, 1] == chess [2, 2]) ||
		    (chess[0,2]!=0&&chess [0, 2] == chess [1, 1] && chess [1, 1] == chess [2, 0])) {
    
    
			return chess [1, 1];
		}
        //检查满
        int count = 0;
        for (int i = 0; i < 3; i++) {
    
    
            for (int j = 0; j < 3; j++) {
    
    
                if (chess [i, j] != 0)
                    count++;
            }
        }
        if(count==9)return 3;
		return 0;
	}
}

The effect is as follows
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/lydsera/article/details/127195573