[Unity3D] Unity script① (Create C# script | Open C# script in Visual Studio 2019 | Compile C# script | Mount C# script to game object | Run script )





1. Create a Unity script



Under the Assets directory in the Project window, create a Scripts directory for storing C# scripts;

insert image description here

In the directory on the right side of the Project window, click the right mouse button on the blank space, and select the "Create | C# Script" option in the pop-up menu to create a new C# script;

insert image description here

Rename the C# script, the name of the script must be in English, without special characters; here rename the C# script to "BehaviourScript";

insert image description here





2. Open C# script in Visual Studio 2019



In the Project window, double-click the C# script, and the following dialog box will pop up, allowing you to choose how to open the file. Select the Microsoft Visual Studio 2019 development environment to open the script in Visual Studio 2019. When opening, a project environment will be automatically created for the script;

insert image description here

Install Visual Studio 2019 Reference:


Running Unity C# scripts in Visual Studio 2019 requires the following two components:

  • .NET Desktop Development :
    insert image description here
  • Game development with Unity:

insert image description here
The above two components must be installed in advance in Visual Studio Installer, otherwise Unity’s C# script cannot be run in VS, or there is no code prompt;


After entering Visual Studio 2019, edit the code in the Start method:

insert image description here
The complete code is as follows:

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

public class NewBehaviourScript : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        Debug.Log("Unity 脚本入口 , 启动加载时调用");
    }

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




3. Compile the C# script



After saving the code, go back to the Unity editor window, and Unity will automatically compile the C# script;

insert image description here





4. Mount the C# script to the game object GameObject



The script must be mounted on the game object GameObject to take effect, otherwise the script will not be executed in Unity;

There are two ways to mount the script:

  • How to add components: select the object, add components through "Add Component" in the Inspector window, find the corresponding script component under the Scripts group, and add it;
  • Direct drag method: select the object, drag the script directly from the Project window to the Inspector window;

1. How to add components


Select the object in the Hierarchy window, select "Collapse All Components" in the Inspector window, and collapse all components;

insert image description here

Then click the "Add Component" button at the bottom of the Inspector window, and find the newly added script under Scripts to add it successfully;

insert image description here
Under the Scripts group, there is the C# script BehaviourScript.cs just created;
insert image description here


2. Direct drag method


Select the object, drag the script directly from the Project window to the Inspector window;
insert image description here





5. Run the C# script



Click the run button on the right side of the toolbar in the Unity editor to run the game, and at the same time the components in each game object GameObject will also run automatically, then the C# script component we added to the cube will also run, and output the content insert image description herein the Console console;Unity 脚本入口 , 启动加载时调用

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/127890336