Today, I watched some Unity applications in virtual simulation and Unity3D game development tutorials. Finally, I focused on the content of “script” in Unity Chinese document, and found that it was almost the same as cocos Creator I used before, except that the programming language used was different, including built-in UI, events, The property panels of the game objects were pretty much the same, which saved me a lot of time.

Cocos synthesis of large watermelon game

Four months ago, I spent a week or two working on a game called “Synthesizing giant watermelons” :

The scripting language is Typescript, and I found that Cocos script structures are pretty much the same as Unity’s, such as the lifecycle of game objects:

In the Cocos script

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

In the Unity script

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

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

Of course, there are a lot of similarities, game engines should be designed this way.

What is a game script?

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

The first thing to understand is that scripting is an essential part of any game engine.

Its main uses are to respond to player input, schedule events that occur during play, instantiate graphical effects, control the physical behavior of game objects, customize AI systems for characters, and more.

Scripting concepts in Unity

Unity Creation Script

  1. The Create menu in the upper left of the Project panel creates a new script
  2. Choose Assets > Create > C# Script to Create a new Script

Unity script file analysis

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(){}}Copy the code
  • The MonoBehaviour built-in class derives classes for creating new component types that can be attached to game objects.
  • Update(), which handles frame updates of the gameobject.
  • Start(), where the script is initialized.

The Unity of the Prefabs

Prefabs is commonly used when you want to instantiate complex game objects or collections of game objects at runtime. It is very convenient and has the following advantages over creating game objects from scratch using code:

  • Instantiate a line of code.
  • Built-in window modification prefab.
  • You can change the instantiated PreFab without changing the code.

Here is the code to use Prefab, which has a public variable “myPrefab”, which is a reference to the Prefab, an instance of which is created in the Start() method.

using UnityEngine;
public class InstantiationExample : MonoBehaviour 
{
    // reference prefab. In Inspector, drag the prefab into this field.
    public GameObject myPrefab;

    // This script will simply instantiate the prefab at the start of the game.
    void Start()
    {
        // instantiate to position (0, 0, 0) and zero rotation.
        Instantiate(myPrefab, new Vector3(0.0.0), Quaternion.identity); }}Copy the code

Some Unity script concepts, such as coroutines, namespace, script properties, events, exception handling, please check: docs.unity3d.com/cn/current/…

Important classes in Unity scripts

  • GameObject: Type of object that can exist in the scene
  • MonoBehaviour: base class, derived class
  • Object: The base class for all objects referenced in the editor
  • Transform: Handles game object animation
  • Vectors: classes that manipulate 2D, 3D, and 4D points, lines, and directions
  • Quaternion: classes that rotate in absolute or relative terms
  • ScriptableObject: A data container that holds a large amount of data
  • Time: Measure and control time
  • Mathf: a set of common mathematical functions
  • Random: random values
  • Debug: Visual editor debugging
  • GizmosHandles: Draws lines and shapes and interactive handles and controls

conclusion

Today is mainly to understand the script in Unity, here is a brief summary of the main points:

  • Cocos engine and Unity engine are very similar, support domestic to develop wechat games with Cocos.
  • Scripting is an essential part of a game engine.
  • usePrefabComplex game objects can be instantiated.
  • The Unity infrastructure is inheritedMonoBehaviourClass,StartMethod is used to initialize,UpdateThe method is to handle frame updates of the gameobject.

Finished!

Your likes, comments and concerns are the biggest encouragement for xiaobian O(∩_∩)O👍👍👍

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