Article Directory
1. Create a Unity script
Under the Assets directory in the Project window, create a Scripts directory for storing C# scripts;
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;
Rename the C# script, the name of the script must be in English, without special characters; here rename the C# script to "BehaviourScript";
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;
Install Visual Studio 2019 Reference:
- 【Unity3D】Unity3D software installation (register an account and download Unity Hub | install Unity Hub | obtain personal version authorization | Chinese environment settings | install Unity3D editor )
- 【Visual Studio】Visual Studio 2019 Community Edition CMake development environment installation (download | install related components | create compile execution project | error handling) (recommended ☆)
Running Unity C# scripts in Visual Studio 2019 requires the following two components:
- .NET Desktop Development :
- Game development with Unity:
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:
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;
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;
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;
Under the Scripts group, there is the C# script BehaviourScript.cs just created;
2. Direct drag method
Select the object, drag the script directly from the Project window to the Inspector window;
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 in the Console console;Unity 脚本入口 , 启动加载时调用