Add C# code to the object in Unity and get the object's properties
ready
(基本为废话,有些基础的可以直接跳过)
1、Unity
Software-Unity (version 5.3.4) or (2018)
Just choose one of these two, you don’t need to have both.
These are the two versions I used. I have tested them and I haven’t used the others. There should be little difference
2、VisualStudio
Referred to as VS, version 2017 or 2019
So far the preparations are complete
One, the first step
1. Open the software
Create a new scene (click this new), then name it, select the storage location, and click Create project
2. Various settings
- It looks like this after opening Unity
- I am accustomed to changing his layout format to 2 By 3. After the change, it looks like this. The
five sections are:
The name of each area in the software | Role and my understanding |
---|---|
Scene | Scenes |
Game | Game screen (player's perspective) |
Hierarchy | Hierarchy (object level) |
Project | Various components |
Inspector | Surveillance (object details) |
- Open Preferences in the Edit menu...
- Choose to open the second one, and change the External Script Editor inside according to the picture (here I use the vs2019 version, modify the opening method according to the software used by personal use)
Second, the second step
1. Various creations
-
Right-click and select an object in the Sample Scene (eg cube)
-
After creating it, right-click Assets→Create→Folder in the Project (component) to create a new folder dedicated to the code (you can change the name, I changed it to code)
2. Create an empty code
-
Right-click in the code to create a new C# file, rename it and open
it here and change it to GetTrsndFrom. After opening it, you must pay attention to whether the name of the namespace in C# is consistent with the C# file name (the two pictures below), these two places must be Same, otherwise the error will be fatal.
-
In Unity, use the mouse to drag the code to the Inspector of the cube (in the object details).
-
Open the C# file just now, the code inside should look like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetTrsndFrom : MonoBehaviour {
// Use this for initialization
void Start () {
//此函数里的代码只会在游戏开始第一帧运行一次;
}
// Update is called once per frame
void Update () {
//此函数里的代码会在游戏开始后每帧都运行;
}
}
So far, I have completed the creation of a C# empty code in Unity. The following is an advanced, how to get and print the Transform property of the cube
3. Advanced (get the transform attribute of the cube and print it)
1. Write code, get object attributes and print
//比如我们需要获取正方体的Transfor属性需要如下代码
var tmpSize = GameObject.Find("Cube").GetComponent<Collider>().transform;
Debug.Log(tmpSize);
//写在Start函数和Update函数中有什么区别说过了,亲们可以分别试一试
Don’t worry if you don’t understand, you can copy these first, and learn later, save and switch to the Unity interface after you finish, click the start button in the upper middle
2. Effect preview
The printing effect is as follows:
- Just click Start and wait for a while, wait for the game to run, you will see a line of words in the bottom left corner
- Clicking this will open a dialog box with more information
- Double-click this will open the line of code to run, and select a specific line to run
- If there is no error in the code in the future, errors will often be reported here. Double-click to open it and you can see which line is wrong. The most frequently encountered error is " Null Pointer Exception " as shown below:
3. The difference between Start function and Update function
The direct difference is that one only runs one frame at the beginning of the game, and one runs every frame. The specific effects are left for everyone to try.
Special thanks
Thank you for your support. For a novice, I am very surprised and excited to have so many page views. It is also your surprise. It gave me the reason to write it down. I also deeply understand that some people pay attention to it. People are so powerful, so I would like to thank my fans here, thank you for your attention, I will continue to cheer, if there is anything in the articleproblemwithSuggestHave direct private letter I leave a message or comment , I will carefully read and reply to each one, thank you for your support! ! !