image.png
Unity 场景中所有实体的基类。
注意:GameObject 类中的很多变量已被删除。例如, 要访问 csharp 中的 GameObject.renderer,请改用 GetComponent<Renderer>()。
函数 | 含义 |
---|---|
activeInHierarchy | 定义 GameObject 在 Scene 中是否处于活动状态。 |
activeSelf | 此 GameObject 的本地活动状态。(只读) |
isStatic | 仅限 Editor 的 API,指定游戏对象是否为静态。 |
layer | 该游戏对象所在的层。 |
scene | 该 GameObject 所属的场景。 |
tag | 此游戏对象的标签。 |
transform | 附加到此 GameObject 的 Transform。 |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class addLight : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnGUI()
{
if (GUILayout.Button("add light"))
{
// 添加游戏对象
GameObject lightAdd = new GameObject();
// 添加光组件
Light lightRed = lightAdd.AddComponent<Light>();
// 设置光源颜色
lightRed.color = Color.red;
// 点光源
lightRed.type = LightType.Point;
}
}
}
运行效果:
image.png