Version: 2021.2

GameObjectRecorder.BindComponentsOfType

切换到手册
public void BindComponentsOfType (GameObject target, bool recursive);
public void BindComponentsOfType (GameObject target, Type componentType, bool recursive);

参数

target root 或其任何子项。
recursive 当设置为 true 时,也要绑定 target 子项的变换组件属性。
componentType 组件类型。

描述

target 中的首个 T 类型组件的所有属性添加绑定;如果 recursivetrue ,也要为 target 子项的所有属性添加绑定。

using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;

public class BindComponentScript : MonoBehaviour { void Start() { var recorder = new GameObjectRecorder(gameObject);

// Add bindings for all the properties of the Transform and BoxCollider components. recorder.BindComponentsOfType<Transform>(gameObject, false); recorder.BindComponentsOfType<BoxCollider>(gameObject, false); } }

您也可以使用非通用方法,在这种情况下,typeof() 将获得组件的类型属性。

此示例获得的结果与上面的示例完全相同:

using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;

public class BindComponentNonGenericScript : MonoBehaviour { void Start() { var recorder = new GameObjectRecorder(gameObject);

recorder.BindComponentsOfType(gameObject, typeof(Transform), false); recorder.BindComponentsOfType(gameObject, typeof(BoxCollider), false); } }