Unity learning record - discrete simulation engine basics

Preface


​ This article is an assignment 2 for the 2020 class of 3D game programming and design at the School of Software Engineering, Sun Yat-sen University

short answer questions


1. The difference and connection between GameObjects and Assets

years:

​ In the unity official website we can find the following two definitions:

Assets:
A Unity asset is an item that you can use in your game or Project. An asset may come from a file created outside of Unity, such as a 3D model, an audio file, an image, or any of the other types of file that Unity supports. There are also some asset types that you can create within Unity, such as an Animator Controller, an Audio Mixer or a Render Texture.1

GameObject:
GameObjects are the fundamental objects in Unity that represent characters, props and scenery. They do not accomplish much in themselves but they act as containers for Components, which implement the functionality.2

From the above definition, we can understand:

​ Resources are various materials that can be used in the game, while game objects refer to the basic objects that contain materials and play their roles in the game. It can be seen that the difference and connection between the two is that: the game object does not bear the responsibility of realizing various attributes and functions in the game. It is just a basic object that integrates various materials; while the various attributes and functions in the game Functions are all implemented by relying on resources attached to game objects. In a certain sense, it can be understood that the game object is several instantiated resources.

2. Download several game cases and summarize the resource and object organization structures respectively (referring to the directory organization structure of resources and the hierarchical structure of the game object tree)

years:

​ Download the official tutorial of UnityPlayGround3 here.

The directory organization structure of resources is as shown below:

Resource directory organizational structure

The game object tree is as shown below:

game object tree

Through a simple game example, it can be concluded:

​ The resource organization structure includes all materials in this project, including Font (font files), Prefabs (presets), Resources (dynamically loaded resource files), Materials (material files), Scripts (script code files), etc. . These material files are classified into different folders according to their functions.

The object organization structure includes all the materials in the designed game instance, and the materials are stored in an inheritance relationship.

3. Write a code and use debug statements to verify the basic behavior of MonoBehaviour or the conditions for event triggering
  • Basic behaviors includeAwake() Start() Update() FixedUpdate() LateUpdate()

  • Common events includeOnGUI() OnDisable() OnEnable()

years:

The code is as follows:

void Awake()
{
    Debug.Log("Awake");
}
void Start()
{
    Debug.Log("Start");
}
void Update()
{
    Debug.Log("Update");
}
void FixedUpdate()
{
    Debug.Log("FixedUpdate");
}
void LateUpdate()
{
    Debug.Log("LateUpdate");
}
void OnGUI()
{
    Debug.Log("onGUI");
}
void OnDisable()
{
    Debug.Log("onDisable");
}
void OnEnable()
{
    Debug.Log("onEnable");
}

After running, you can see:

running result

In the unity official manualExecution sequence of event functions4, you can see the unity script The execution sequence of the built-in functions and their triggering conditions are as follows:

Execution sequence: Awake ->OnEable-> Start -> FixedUpdate-> Update -> LateUpdate ->OnGUI ->OnDisable ->OnDestroy

Awake: This function is always called before any Start function and after the prefab is instantiated. (If the game object is inactive during startup, Awake will not be called until it is activated.)

Start: Start is called before the first frame update only when the script instance is enabled.

Update: Update is called once per frame. This is the main function used for frame updates.

FixedUpdate: FixedUpdate is often called more frequently than Update. If the frame rate is low, this function may be called multiple times per frame; if the frame rate is high, the function may not be called at all between frames. All physics calculations and updates will occur immediately after FixedUpdate. When applying motion calculations within FixedUpdate, there is no need to multiply the value by Time.deltaTime. This is because the call to FixedUpdate is based on a reliable timer (independent of frame rate).

LateUpdate: LateUpdate is called once per frame (after Update is completed). When LateUpdate begins, all calculations performed in Update are complete. A common use of LateUpdate is to follow a third-person camera. If you have your character moving and turning within Update, you can perform all camera movement and rotation calculations in LateUpdate. This ensures that the character has fully moved before the camera tracks its position.

OnGUI: Called multiple times per frame in response to GUI events. Layout and redraw events are handled first, then layout and keyboard/mouse events for each input event.

OnDisable: This function is called when the behavior is disabled or inactive.

OnEnable: (Only called when the object is active) Call this function immediately after enabling the object. This call is made when a MonoBehaviour instance is created, such as when a level is loaded or a game object with a script component is instantiated.

4. Find the script manual and learn about GameObject, Transform, and Component objects
  • Translate the official descriptions of the three objects respectively (Description)
  • Describe the properties of the table object (entity), the Transform property of the table, and the components of the table
  • Use UML diagrams to describe the relationship between the three

years

  • The official description of the three objects in the script manual, as well as the author's translation, is as follows:

    GameObject: Base class for all entities in Unity Scenes.5

    GameObject is the base class of all entity classes in Unity scenes.

    Transform: Position, rotation and scale of an object.6

    Transform is the position, rotation and scaling attribute values ​​of the object.

    Component: Base class for everything attached to GameObjects.7

    Component is the base class for all objects attached to GameObjects.

  • The object of Table is GameObject, and its properties are as follows:

The first selection box in the first line has the activeSelf attribute, the second text box has the object name, and the third selection box has the static attribute. The second line has the Tag attribute and Layer attribute, and the third line has the prefabs (default) attribute.

Table properties

The Transform properties of Table include: position, Rotation, and Scale.

Table's Transform property

Table components include: Transform, Cube (Mesh Filter), Mesh Renderer, Box Collider

Table parts

  • The relationship between GameObject, Transform, and Component objects is as follows:

UML diagram

5. Resource prefabs and object cloning (clone)
  • What are the benefits of Prefabs?
  • What is the relationship between preset and object clone (clone or copy or Instantiate of Unity Object)?
  • Make a table prefab and write a piece of code to instantiate the table prefab resource into a game object.

years

  • Presets in Unity have the following two benefits:

    1) Template function: Prefab allows us to create a template based on a certain game object. Through this template, we can repeatedly create game objects with the same structure very conveniently and quickly.

    2) Variety of changes: When there are multiple game objects created by the same preset in the scene, if you want to change one of their attributes, you only need to change the corresponding attribute of the preset.

  • Both presets and clones generate game objects through copying, and cloning requires that game objects already exist in the current scene. There is no need for corresponding game objects after the presets are set. In addition, the game objects generated by cloning are independent of each other and can Modify one of the objects arbitrarily, and if you modify the preset, all objects will change.

  • code show as below

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadBeh : MonoBehaviour
{
    public Transform res;

    // Start is called before the first frame update
    void Start()
    {
        GameObject newobj =
            Instantiate<Transform>(res, this.transform).gameObject;
        newobj.transform.position = new Vector3(0, Random.Range(-5, 5), 0);
    }

    // Update is called once per frame
    void Update()
    {
    }
}

Programming Practice - Calculator


The calculator code has been uploaded tohw2/Calculator.cs · XiaoChen04_3/3D_Computer_Game_Programming

Demo:

Demonstration effect

Quote


  1. Unity - Manual: GameObjects (unity3d.com) ↩︎

  2. Unity - What is a Unity Asset? ↩︎

  3. Unity Playground | Video Tutorial Project Resources | Unity Asset Store ↩︎

  4. Execution order of event functions - Unity Manual (unity3d.com) ↩︎

  5. Unity - Scripting API: GameObject (unity3d.com) ↩︎

  6. Unity - Scripting API: Transform (unity3d.com) ↩︎

  7. Unity - Scripting API: Component (unity3d.com) ↩︎

Guess you like

Origin blog.csdn.net/jmpei32534/article/details/127000212