可用于屏幕渲染的元素。
画布上的元素在场景渲染之后渲染(从附加的摄像机或使用覆盖模式)。
using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.UI;
// Create a Canvas that holds a Text GameObject.
public class ExampleClass : MonoBehaviour { void Start() { GameObject myGO; GameObject myText; Canvas myCanvas; Text text; RectTransform rectTransform;
// Canvas myGO = new GameObject(); myGO.name = "TestCanvas"; myGO.AddComponent<Canvas>();
myCanvas = myGO.GetComponent<Canvas>(); myCanvas.renderMode = RenderMode.ScreenSpaceOverlay; myGO.AddComponent<CanvasScaler>(); myGO.AddComponent<GraphicRaycaster>();
// Text myText = new GameObject(); myText.transform.parent = myGO.transform; myText.name = "wibble";
text = myText.AddComponent<Text>(); text.font = (Font)Resources.Load("MyFont"); text.text = "wobble"; text.fontSize = 100;
// Text position rectTransform = text.GetComponent<RectTransform>(); rectTransform.localPosition = new Vector3(0, 0, 0); rectTransform.sizeDelta = new Vector2(400, 200); } }
| additionalShaderChannels | 获取或设置要在创建 Canvas 网格时使用的附加着色器通道的遮罩。 |
| cachedSortingLayerValue | 基于 SortingLayerID 缓存的计算值。 |
| isRootCanvas | 这是根 Canvas 吗? |
| normalizedSortingGridSize | 画布将可渲染区域拆分成的标准化网格大小。 |
| overridePixelPerfect | 允许嵌套画布重写从父画布继承的 pixelPerfect 设置。 |
| overrideSorting | 覆盖画布的排序。 |
| pixelPerfect | 强制画布中的元素按像素对齐。仅在 renderMode 为屏幕空间时适用。 |
| pixelRect | 获取 Canvas 的渲染矩形。 |
| planeDistance | 生成画布的位置到摄像机的距离。 |
| referencePixelsPerUnit | 被视为默认值的 Pixels Per Unit 数量。 |
| renderingDisplaySize | Returns the canvas display size based on the selected render mode and target display. |
| renderMode | 该 Canvas 处于世界模式还是覆盖模式? |
| renderOrder | 将画布发射到场景的渲染顺序。(只读) |
| rootCanvas | 通过检查每个父项并返回找到的最后一个画布来返回最接近根的 Canvas。如果未找到其他画布,则画布返回自身。 |
| scaleFactor | 用于缩放整个画布,同时仍使其适合屏幕。仅在 renderMode 为屏幕空间时适用。 |
| sortingLayerID | 画布排序图层的唯一 ID。 |
| sortingLayerName | 画布排序图层的名称。 |
| sortingOrder | 排序图层中的画布顺序。 |
| targetDisplay | 用于覆盖模式,将显示 UI 画布的显示索引。 |
| worldCamera | 在“Screen Space - Camera”中用于调整 Canvas 大小的 Camera。也在 World Space Canvas 中用作通过其发送事件的 Camera。 |
| ForceUpdateCanvases | 强制所有画布更新其内容。 |
| GetDefaultCanvasMaterial | 返回可用于在画布上渲染普通元素的默认材质。 |
| GetETC1SupportedCanvasMaterial | 获取或生成 ETC1 材质。 |
| preWillRenderCanvases | 在即将开始 Canvas 渲染前调用的事件。 |
| willRenderCanvases | 在即将开始 Canvas 渲染前调用的事件。 |
| enabled | 启用的 Behaviour 可更新,禁用的 Behaviour 不可更新。 |
| isActiveAndEnabled | Reports whether a GameObject and its associated Behaviour is active and enabled. |
| gameObject | 此组件附加到的游戏对象。始终将组件附加到游戏对象。 |
| tag | 此游戏对象的标签。 |
| transform | 附加到此 GameObject 的 Transform。 |
| hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
| name | 对象的名称。 |
| BroadcastMessage | 调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。 |
| CompareTag | Checks the GameObject's tag against the defined tag. |
| GetComponent | Returns the component of type if the GameObject has one attached. |
| GetComponentInChildren | Returns the Component of type in the GameObject or any of its children using depth first search. |
| GetComponentInParent | Returns the Component of type in the GameObject or any of its parents. |
| GetComponents | 返回 GameObject 中类型为 type 的所有组件。 |
| GetComponentsInChildren | Returns all components of Type type in the GameObject or any of its children using depth first search. Works recursively. |
| GetComponentsInParent | 返回 GameObject 或其任何父项中类型为 type 的所有组件。 |
| SendMessage | 调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。 |
| SendMessageUpwards | 调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。 |
| TryGetComponent | 获取指定类型的组件(如果存在)。 |
| GetInstanceID | Gets the instance ID of the object. |
| ToString | 返回对象的名称。 |
| Destroy | 移除 GameObject、组件或资源。 |
| DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
| DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
| FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
| FindObjectsOfType | Gets a list of all loaded objects of Type type. |
| Instantiate | 克隆 original 对象并返回克隆对象。 |
| bool | 该对象是否存在? |
| operator != | 比较两个对象是否引用不同的对象。 |
| operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |