头图

Today I saw some Unity applications in virtual simulation and Unity3D game development tutorials. Finally, I focused on script " and found that it is cocos creator I used before, except for the ones used The programming language is different, the built-in UI, events, and the property panel of the game object are all similar, which also saved me a lot of time.

Cocos synthetic big watermelon game

4 months ago, I had tossed about game development for a week or two. What I did was the game of "synthesizing big watermelon":

file

The scripting language used is Typescript . I found that the script structure of Cocos is almost the same as that of Unity, such as the life cycle of game objects:

In Cocos script

export default class Game extends cc.Component {
     start() {}
     update() {}
}

In Unity script

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

public class Game : MonoBehaviour
{
    void Start(){}
    void Update(){}
}

Of course, there are many similarities. The game engine should be designed like this.

What is a game script?

What is a script? What exactly does the script do for the game engine?

The first thing to understand is that the script is an essential part of any game engine, .

Its main purpose is to respond to player input, arrange events that occur during the game, instantiate graphic effects, control the physical behavior of game objects, and customize the AI system for the character.

Scripting concept in Unity

Unity create script

  1. Create a new script in the Create menu at the top left of the Project panel
  2. Select Assets> Create> C# Script to create a new script

Analysis of Unity script file

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

public class Wall : MonoBehaviour
{
    // Start is called before the first frame update

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
  • MonoBehaviour built-in class derived class is used to create new component types that can be attached to game objects.
  • Update(), which handles the frame update of the game object.
  • Start(), where the script is initialized.

Prefabs in Unity

The Chinese translation of Prefabs is a prefab. It is generally used when you want to instantiate a complex game object or a collection of game objects at runtime. It is very convenient. Compared with using code to create a game object from scratch, it has the following advantages:

  • Instantiate one line of code.
  • The built-in window modifies the prefab.
  • You can change the instantiated prefab without changing the code.

The following is the code for using Prefab. It has a public variable "myPrefab", which is a reference to the prefab. Create an instance of the prefab in the Start() method.

using UnityEngine;
public class InstantiationExample : MonoBehaviour 
{
    // 引用预制件。在 Inspector 中,将预制件拖动到该字段中。
    public GameObject myPrefab;

    // 该脚本将在游戏开始时简单地实例化预制件。
    void Start()
    {
        // 实例化为位置 (0, 0, 0) 和零旋转。
        Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
    }
}

There are also some Unity script concepts, such as coroutines, namespaces, script attributes, events, and exception handling. Please check: https://docs.unity3d.com/cn/current/Manual/ScriptingSection.html

Important classes in Unity script

  • GameObject : The types of objects that can exist in the scene
  • MonoBehaviour : base class, derived class
  • Object : The base class of all objects referenced in the editor
  • Transform : Processing game object animation
  • Vectors manipulating 2D, 3D and 4D points, lines and directions
  • Quaternion : Absolute or relative rotation class
  • ScriptableObject : a data container for storing large amounts of data
  • Time : measurement and control time
  • Mathf : A set of common mathematical functions
  • Random : random value
  • Debug : Visual editor debugging
  • Gizmos and Handles : draw lines and shapes and interactive handles and controls

Summarize

Today, I mainly learned about the scripts in Unity. Here is a brief summary of the main points:

  • The Cocos engine is very similar to the Unity engine. To support domestic development, use Cocos to develop WeChat games.
  • Script is an essential part of a game engine.
  • Use Prefab to instantiate complex game objects.
  • The Unity infrastructure is inherited from the MonoBehaviour class, the Start method is used for initialization, and the Update method is to handle the frame update of the game object.

over!

Your likes, comments, and attention are the biggest encouragement to the editor O(∩_∩)O👍👍👍

I am Geek Ape Xiaobing , the public account [Geek Ape], I record the learning and growth of independent developers (game development/product development/reverse/operation design).

Soldier
80 声望5 粉丝