using UnityEngine; using Script.Gameplay.Interface; namespace Script.Gameplay.Edit { [RequireComponent(typeof(Rigidbody))] public class RigidbodyEditableController : MonoBehaviour, IEditableComponent { [SerializeField] private bool isEnableEdit = true; public bool IsEnableEdit { get => isEnableEdit; set => isEnableEdit = value; } [SerializeField] private bool isOpenInEditor = true; public bool IsOpenInEditor { get => isOpenInEditor; set { isOpenInEditor = value; if (_rigidbody != null) _rigidbody.isKinematic = !isOpenInEditor; } } public string ComponentName { get; set; } = "Rigidbody"; private Rigidbody _rigidbody; private void Awake() { _rigidbody = GetComponent(); //应用序列化的初始状态 _rigidbody.isKinematic = !IsEnableEdit; } #if UNITY_EDITOR private void OnValidate() { //在编辑器中即时生效 _rigidbody = _rigidbody == null ? GetComponent() : _rigidbody; if (_rigidbody != null) _rigidbody.isKinematic = !IsEnableEdit; } #endif } }