在创建脚本时,实际上是在自定义新的组件类型,可以像任何其他组件一样将这种组件附加到游戏对象。
正如其他组件通常具有可在 Inspector 中编辑的属性一样,您也可以让自己脚本中的值可在 Inspector 中编辑。
using UnityEngine;
using System.Collections;
public class MainPlayer : MonoBehaviour
{
public string myName;
// 此函数用于初始化
void Start ()
{
Debug.Log("I am alive and my name is " + myName);
}
}
此代码在 Inspector 中创建一个标记为“My Name”的可编辑字段。
The Inspector might display variable names differently to how you define them in a script if the name conforms to one of a set of rules:
There are some special cases, such as “iPad” or “x64”, where these rules are not applied.
Unity 通过在变量名称中出现大写字母的位置引入空格来创建 Inspector 标签。但是,这纯粹是出于显示目的,在代码中应始终使用变量名称。如果编辑该名称,然后按 Play,您将看到消息中包含您输入的文本。
In C#, the simplest way to see a variable in the Inspector is to declare it as public. An alternative method is to use SerializeField. Conversely, you can use HideInInspector to prevent a public variable from being displayed in the Inspector.
Unity 实际上允许您在游戏运行时更改脚本变量的值。此功能很有用,无需停止和重新启动即可直接查看更改的效果。当游戏运行过程结束时,变量的值将重置为按下 Play 之前所处的任何值。这样可确保自由调整对象的设置,而不必担心会造成任何永久性损坏。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.