unity3D-learing my first game : A easy calculator

1. Short answer questions

  • Explain the difference and connection between GameObjects and Assets.
  •    Difference between the two: 
  •           GameObjects: Every object in the game is a GameObject. They don't do anything by themselves. After we give them their respective attributes, they become the characters or environments that we see in the game. For example, the players and levels we set belong to game objects, and we make them game objects by adding various attributes and fire environments to them. A game object can be thought of as an empty container that becomes different from other game objects after we add components and properties to it.
  •          Assets (Assert): Assets refer to the materials in the project, including our audio, pictures, pictures, images, script files, materials, scenes, action resources, etc. Resources can be imported into the game's procedural project, and then the project provides raw materials and is added to the game object.
  •    The connection between the two: 
  •     The resources can be added to the game object by importing into the game, and the corresponding design can be completed with the game object. At the same time resources can create game instances from components or entire game objects.

  • 2. Download several game cases and summarize the structure of resources and object organization respectively (referring to the directory organization structure of resources and the hierarchical structure of the game object tree)
  •         Resource structure: Resources can be divided into many types through a certain classification. In game cases, there are generally models, audio, animation, script code, presets, models, etc. These things are set up in different folders, and correspond to the corresponding names due to the type.
  •  
  • Such a game case contains audio, scenes, text, scripts, etc., in which the audio is placed in the Audio folder alone, the game characters are placed in the Sprites folder, the help information is placed in the TutorialInfo folder, and the presets are placed in the Audio folder. into Prefabs. At the same time, put the core of the game into the _Complete-Game folder, the code into Scripts, the scene into Scenes, etc.
  • The structure of the object organization: the structure of the object organization is generally a tree structure, and some classes have inheritance and other relationships, so that there can be multiple subclasses under a class, and at the same time, the subclasses can have their own subclasses, thereby achieving tree-shaped results .
  • 3. Write a code that uses the debug statement to verify  the basic behavior of MonoBehaviour  or the conditions for event triggering
    • Basic behavior includes Awake() Start() Update() FixedUpdate() LateUpdate()
    • Common events include OnGUI() OnDisable() OnEnable()
    • Answer: The implementation code is as follows:
    • using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class NewBehaviourScript : MonoBehaviour
      {
    •     void  awake ()
          {// Basic behavior 
              Debug. Log ( " Awake " );
          }
      
          //   Basic behavior 
          void  Start ()
          {
              Debug.Log("Start");
          }
      
        //   basic behavior
      // Update is called once per frame 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"); }}
    • Awake行为是在脚本实例被加载时即被调用
    • Start行为仅在游戏开始时调用一次,之后不再调用,也就是在update第一次调动之前调用一次
    • Update行为每一帧触发一次,是实现动画游戏的基础。
    • FixedUpdate行为是在自行设定的帧频下,每一帧触发一次
    • LateUpdate在所有的updata结束后调用
    • OnGUI事件是当渲染和绘制GUI事件时被调用
    • OnDisable事件是当行为变得无效或者非激活状态的时候被调用,用于清理代码等
    • OnEnable事件是当对象启用并激活的时候被调用

  • 查找脚本手册,了解 GameObject,Transform,Component 对象
    • 分别翻译官方对三个对象的描述(Description)
    • 描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件
      • 本题目要求是把可视化图形编程界面与 Unity API 对应起来,当你在 Inspector 面板上每一个内容,应该知道对应 API。
      • 例如:table 的对象是 GameObject,第一个选择框是 activeSelf 属性。
    • 用 UML 图描述 三者的关系(请使用 UMLet 14.1.1 stand-alone版本出图)

    答:1.三个对象的官方描述:

  • GameObject是Unity场景里面代表人物,道具,场景的基础对象,所有实体的基类。某种程度上就是一个容器的定义。是他们实现了游戏对象容器的特点。
  • Transform是用于储存并操控物体的位置、旋转和缩放的类。用于操作对象的位置,角度等。
  • Component是游戏物体一切附加类的基类。比如它是Transform的基类。通过组件能够为游戏对象添加各种脚本行为等。

         2.table的介绍

  • table的属性有layer(图层),active(非静态)tag(标签,untagged)的属性等。
  • transform的属性有position,rotation,scale等。其中
    • Position: (0, 0, 0)
    • Rotation: (0, 0, 0)
    • Scale : (1, 1, 1)
  • table 的部件(Components)指 transform,Mesh filter,Box Collider等


        3.三者关系:



workwork

  • 4.整理相关学习资料,编写简单代码验证以下技术的实现:
    • 查找对象
    • 添加子对象
    • 遍历对象树
    • 清除所有子对象
      • 通过名称查找单个对象、
      • public static GameObject Find(string name); 
      • 通过标签查找单个对象
      • public static GameObject FindWithTag(string tag);
      • 通过标签查找多对象
      • public static GameObject[] FindGameObjectsWithTag(string tag);
      • 通过类型查找单个对象
      • GameObject.FindObjectOfType(string typename)
        ;
      • 通过类型查找多个对象
      • GameObject.FindObjectsOfType(string typename)
      • 
        
      • public static GameObect CreatePrimitive(PrimitiveTypetype);遍历对象树
    • 添加子对象
    • public static GameObect CreatePrimitive(PrimitiveTypetype);
    • 遍历对象树
    • foreach (Transform child in transform) {  
          Debug.Log(child.gameObject.name);  
      } 
    • 清除所有子对象
    • foreach(Transform child in transform)
        {
            Destroy(child.gameObject);
        }
  • 5.资源预设(Prefabs)与 对象克隆 (clone)
    • 预设有什么好处
    • 预设与对象克隆 (clone or copy or Instantiate of Unity Object) 关系?
    • 制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象
  • 答:
    • 预设的优点:预设可以看成是一个模板,可以迅速方便创建大量相同属性的对象、操作简单,代码量少,减少出错概率。,将比较容易多次使用的对象预设,这样以后在重复使用该实体集时就不用再一一使用底层的对象去创建和拼接。
    • 预设与克隆的区别:预设的属性一旦改变,其所有实例也会跟着改变,而克隆的个体之间是相互独立的,因此修改对于克隆是没有改变的。

  • 尝试解释组合模式(Composite Pattern / 一种设计模式)。使用 BroadcastMessage() 方法
    • 向子对象发送消息
    答:组合模式(Composite Pattern / 一种设计模式):用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。组合模式是将对象组合成树形结构以表示“部分-整体”的层次结构,它使得用户对单个对象和组合对象的使用具有一致性。

       代码:

public class Father : MonoBehaviour {
    void test()
    {
        Debug.Log("Hello, dear son!");
    }
    void Start () {
         this.BroadcastMessage("test");
    }
}

public class Child : MonoBehaviour {
    void test()
    {
        Debug.Log("Hi, my dear father");
    }
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325473893&siteId=291194637