用法:
在新建的类前面加 [RequireComponent(typeof(XXX))]。

例如在新建的类AddOtherComponent前面加 [RequireComponent(typeof(Rigidbody))],挂载Acter到物体上时同时会为该物体
挂载rigibody组件。
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class AddOtherComponent : MonoBehaviour
{
}

这篇博客介绍了Unity中的[RequireComponent]属性用法,通过在自定义脚本前添加该属性,可以确保在挂载脚本时同时添加特定类型的组件,如Rigidbody。示例代码展示了如何在AddOtherComponent类中强制添加Rigidbody组件。
2234

被折叠的 条评论
为什么被折叠?



