TryGetComponent 将尝试获取给定类型的组件。与 GameObject.GetComponent 相比的显著差异在于,如果请求的组件不存在,则此方法不在编辑器中进行分配。 using UnityEngine;public class TryGetComponentExample : MonoBehaviour { void Start() { if (gameObject.TryGetComponent(typeof(HingeJoint), out Component component))...
trygetComponent “当不存在请求的组件时不分配编辑器”。我真的不知道这意味着什么,因为我仍然是Unity的新手,为什么有人会请求首先不存在的组件,所以有人可以解释我是否应该停止使用GetComponent,如果是这样,为什么?预先感谢 可能有一些使用GETCOMPONENT的原因,而不必确定此组件是否存在。在这种情况下,有必要检查组件是否...
TryGetComponent 将尝试获取给定类型的组件。与 GameObject.GetComponent 相比的显著差异在于,如果请求的组件不存在,则此方法不在编辑器中进行分配。 using UnityEngine;public class TryGetComponentExample : MonoBehaviour { void Start() { if (TryGetComponent(out HingeJoint hinge)) { hinge.useSpring = false; }...
TryGetComponent 将尝试获取给定类型的组件。与 GameObject.GetComponent 相比的显著差异在于,如果请求的组件不存在,则此方法不在编辑器中进行分配。 using UnityEngine;public class TryGetComponentExample : MonoBehaviour { void Start() { if (TryGetComponent(out HingeJoint hinge)) { hinge.useSpring = false; }...
using UnityEngine;public class TryGetComponentExample : MonoBehaviour { void Start() { if (TryGetComponent<HingeJoint>(out HingeJoint hinge)) { hinge.useSpring = false; } } } public bool TryGetComponent (Type type, out Component component); ...