Files
2025TapTapGameJam/Assets/Script/Gameplay/Edit/RigidbodyEditableController.cs

50 lines
1.3 KiB
C#
Raw Normal View History

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>();
//应用序列化的初始状态
_rigidbody.isKinematic = !isOpenInEditor;
}
#if UNITY_EDITOR
private void OnValidate()
{
//在编辑器中即时生效
_rigidbody = _rigidbody == null ? GetComponent<Rigidbody>() : _rigidbody;
if (_rigidbody != null)
_rigidbody.isKinematic = !isOpenInEditor;
}
#endif
}
}