3d game homework 2

3D Game Assignment 2

1. Short answer questions

1. Explain the difference and connection between GameObjects and Assets.

Unity's explanation of resources and game objects is as follows

Asset : Any media or data that can be used in a game or project. Assets may come from files created outside of Unity, such as 3D models, audio files, images. There are also asset types that can be created in Unity, such as Animator Controllers, Audio Mixers, or Render Textures.

Game object : The basic object in the Unity scene, which can represent characters, props, scenery, cameras, path points, etc. The functionality of a game object is defined by the components attached to the game object.

From this, we can conclude that the difference and connection between the two is: the game object is an independent unit in the game, which can be composed of multiple resources. For example, the scenery in the above game object is composed of 3D models, images and other resources. Resources can be said to be templates that constitute game objects, and can be used by multiple game objects. For example, different characters in the game may be formed by modifying the same 3D model.

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

The resource directory organization structure is as follows, which generally includes folders such as fonts, materials, models, scenes, etc., and various resources are classified into these folders

The game object tree contains all game objects in the current scene, and it is a tree structure, that is, the parent node will branch out to child nodes, and the child nodes will have new child nodes, forming an object tree layer by layer.

3. Write a code and use debug statements to verify the basic behavior of MonoBehaviour or the conditions for event triggering

  • Basic behaviors include Awake() Start() Update() FixedUpdate() LateUpdate()

  • Common events include OnGUI() OnDisable() OnEnable()

Execute the code first

As can be seen from the figure, Start(), Awake() and OnEnable() are only executed once, while FixedUpdate(), Update(), LateUpdate(), OnGUI() are always executed, and the number of times FixedUpdate() is executed Less, while Update () and LateUpdate () are executed more times, and OnGUI () is executed the most. Check the official website for explanations of related events, and you can know the conditions for triggering these behaviors and events.

start() and awake() : These two behaviors have some similarities and are easily confused, for example, they are only called once and both are called before update(). But they are different, awake() is called immediately after the object is initialized, and start() is only guaranteed to be called before update(), generally after awake(), and if it is called immediately after awake() Set enable to false to disable update(), then start() will not be called.

OnEnable : Called after the object is enabled in the activated state (that is, after the wake() call). The difference from start() and awake() is that after the object is deactivated and reactivated, start() and awake() will not be activated again Called, and OnEnable() will be called again. The image below is the display after reactivation.

 Update() and LateUpdate() : These two basic behaviors are called every frame. lateupdate() is called after update() is fully executed. A common use of lateupdate() is to follow a third-person camera. If you move and turn your character within update(), you can perform all camera movement and rotation calculations in lateupdate(). This ensures that the character is fully moved before the camera tracks its position.

OnGui() : This function is called multiple times per frame in response to GUI events.

Fixedupdate() : With the frequency of the physics system, this function is called every fixed frame rate frame.

Ondisable() : This function is called when the behavior is disabled or inactive.

4. Find the script manual to understand GameObject, Transform, Component objects

  • Translate the official descriptions of the three objects respectively (description)

Gameobject:Base class for all entities in Unity Scenes.1

GameObject : the base class of all entity classes in the unity interface

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

Transfrom : Used to store the position, rotation angle, and scaling of game objects.

Component:Base class for everything attached to GameObjects.3

Component : The base class for all content attached to GameObjects.

  • 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

The first check box indicates whether the object exists in the scene. If the object is unchecked, the object will disappear in the scene; the text box on the right is the name of the object, and the right is whether the object is set to static or not. If it is checked, Objects just don't move during gameplay. The tag below can set whether to add a tag to the object, the layer can set the coating for the object, and the prefab is prefabricated, which can be prefabricated for the object. overrides is coverage.

 There are three attributes in transform, Position is the position of the object, Rotation is the rotation angle, and Scale is the size of the object.

There are four parts of the table as follows.

 

Resource presets (Prefabs) and object cloning (Clone)

  • What are the benefits of prefabs

    Prefabrication is to set a general game object as a prefab. The advantage is that you can use this general game object as a class template to make some modifications and expansions on the basis of it, so as to continuously obtain new objects, which can save a lot of repeated creation of objects and The time required to set properties greatly improves work efficiency.

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

    Presets themselves do not need to have an instanced GameObject, while cloning requires a copy of the instanced GameObject. And if you want to centrally modify the objects created by instantiating the presets, you only need to modify the presets to modify them all, which is convenient for batch modification. And if you want to modify the cloned objects, you can only modify them one by one.

  • Make a table prefab, write a piece of code to instantiate the table prefab resource into a game object

The relationship among GameObject, Transform, and Component objects is as follows

 

two calculators

running result:

code show as below

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
​
public class cccal : MonoBehaviour {
    public GUIStyle buttonstyle;
    string str="";
    void OnGUI(){
        //创建GUI
        GUI.Box(new Rect (Screen.width/2-100,Screen.height/2-160, 235, 50), str); 
        
        if (GUI.Button (new Rect (Screen.width/2-100,Screen.height/2-45,55,55), "1")) {
            str += "1";
        }
        if (GUI.Button (new Rect (Screen.width/2-40,Screen.height/2-45,55,55), "2")) {
            str += "2";
        }
        if (GUI.Button (new Rect (Screen.width/2+20,Screen.height/2-45,55,55), "3")) {
            str += "3";
        }
        if (GUI.Button (new Rect (Screen.width/2-100,Screen.height/2+10,55,55), "4")) {
            str += "4";
        }
        if (GUI.Button (new Rect (Screen.width/2-40,Screen.height/2+10,55,55), "5")) {
            str += "5";
        }
        if (GUI.Button (new Rect (Screen.width/2+20,Screen.height/2+10,55,55), "6")) {
            str += "6";
        }
        if (GUI.Button (new Rect (Screen.width/2-100,Screen.height/2+65,55,55), "7")) {
            str += "7";
        }
        if (GUI.Button (new Rect (Screen.width/2-40,Screen.height/2+65,55,55), "8")) {
            str += "8";
        }
        if (GUI.Button (new Rect (Screen.width/2+20,Screen.height/2+65,55,55), "9")) {
            str += "9";
        }
        if (GUI.Button (new Rect (Screen.width/2+80, Screen.height/2+10 , 55, 55), "C")) {
            str ="";
        }
        if (GUI.Button (new Rect (Screen.width/2-100,Screen.height/2+120,115,55), "0")) {
            str +="0";
        }
        if (GUI.Button (new Rect (Screen.width/2+20,Screen.height/2+120,55,55), ".")) {
            str +=".";
        }
        if (GUI.Button (new Rect (Screen.width/2-100,Screen.height/2-100,55,55), "+")) {
            str +="+";
        }
        if (GUI.Button (new Rect (Screen.width/2-40,Screen.height/2-100,55,55), "-")) {
            str +="-";
        }
        if (GUI.Button (new Rect (Screen.width/2+20,Screen.height/2-100,55,55), "x")) {
            str +="*";
        }
        if (GUI.Button (new Rect (Screen.width/2+80,Screen.height/2-100,55,55), "/")) {
            str +="/";
        }
        if (GUI.Button (new Rect (Screen.width/2+80, Screen.height/2-45 , 55, 55), "back")) {
            if (str.Length > 0)
                str = str.Remove(str.Length-1,1) ;
        }
        if (GUI.Button (new Rect (Screen.width/2+80,Screen.height/2+65,55,115), "=")) {
            mycocluator c = new mycocluator ();
            str = c.calculate(str); 
        } 
    } 
    public class mycocluator{
        float[] num = new float[100];
        int top1 = -1;//used to record the number used in num 
        Stack<double> num2=new Stack<double>(); 
        Stack<char> cal2=new Stack<char>(); 
        char[] cal = new char[100]; 
        int top2 = -1;//used to record the number used in cal 
        bool flag=false;//used to mark the first number Is it negative? 
        public string calculate(string s) { 
            int i = 0; 
            if(i==0 && s[i]=='-') 
            { 
                flag=true; 
                i++; 
            } 
            //Traverse the string first, find out of which the operator 
            while (i < s.Length) { 
                switch (s [i]) {
                case '+': 
                    cal [top2 + 1] = '+'; 
                    top2++; 
                    break; 
                case '-': 
                    cal [top2 + 1] = '-'; 
                    top2++; 
                    break; 
                case '*': 
                    cal [top2 + 1 ] = '*'; 
                    top2++; 
                    break; 
                case '/': 
                    cal [top2 + 1] = '/'; 
                    top2++; 
                    break; 
                } 
                int j = 0; 
                //used to record the currently traversed number
                string tmpnum = ""; 
                //Find the number in the string 
                 
                while (((i+j) < s.Length) && (s [ i + j] == '.' || (s [i + j] <= '9' && s [i + j] >= '0'))) {
                    tmpnum += s [i + j]; 
                    j++; 
                } 
                if (j != 0) { 
                    float temp = float.Parse (tmpnum); 
                    top1++;    
                    if( top1==0 && flag==true) 
                    { 
                        num [top1] = 0-temp; 
                    } 
                    else 
                    { 
                        num [top1] = temp; 
                    } 
                    i += j; 
                } 
                else {
                    i++; 
                } 
            } 
        //Start calculation, if you encounter * or /, first calculate the product or quotient and then put it on the stack. 
        int l=0; 
        num2.Push(num[0]); 
        for(int k=1;k<=top1;k++) 
        { 
            if(cal[l]=='*') 
            { 
                double tmp=num2.Peek( ); 
                num2.Pop(); 
                double tmp2=num[k]; 
                
                num2.Push(tmp*tmp2); 
            } 
            if(cal[l]=='/') 
            { 
                double tmp=num2.Peek(); 
                num2. Pop(); 
                double tmp2=num[k]; 
                
                num2.Push(tmp/tmp2);
            } 
            else if(cal[l]=='+'||cal[l]=='-') 
            { 
                cal2.Push(cal[l]); 
                num2.Push(num[k]); 
            } 
            l++;
        }
        //Because The stack is reversed, so 1-3-3 may be regarded as 1-(3-3), so reverse the stack 
        ArrayList nnum=new ArrayList(); 
        ArrayList ccal=new ArrayList(); 
        while(num2. Count>0) 
        { 
            nnum.Add(num2.Peek()); 
            num2.Pop(); 
        } 
        while(cal2.Count>0) 
        { 
            ccal.Add(cal2.Peek()); 
            cal2.Pop() 
        ; 
        for(int k=0;k<nnum.Count;k++) 
        { 
            num2.Push((double)nnum[k]); 
        } 
        for(int k=0;k<ccal.Count;k++) 
        { 
            cal2. Push((char)ccal[k]); 
         } 
         //Do stack calculation for only + and -
        while(num2.Count>1)
        {
            double a=num2.Peek();
            num2.Pop();
            double b=num2.Peek();
            num2.Pop();
            char c=cal2.Peek();
            cal2.Pop();
            if(c=='+')
            {
                double d=a+b;
                num2.Push(d);
            }
            if(c=='-')
            {
                double d=a-b;
                num2.Push(d);
            }
        }
            flag=false;
            return num2.Peek().ToString();
        }
    }
​
​
    // Use this for initialization
    void Start () {
​
    }
​
    // Update is called once per frame
    void Update () {
​
    }
​
​
}
​
​

[1]  Unity - Scripting API: GameObject

[2]  Unity - Scripting API: Transform

[3]  Unity - Scripting API: Component

Guess you like

Origin blog.csdn.net/weixin_52801288/article/details/127164629