refactor():重构编辑系统,不再需要添加额外的脚本作为可编辑的入口了

This commit is contained in:
2025-10-21 16:40:41 +08:00
parent 1ce3d1a0d8
commit 93b83b3af3
37 changed files with 186 additions and 199 deletions

View File

@@ -4,7 +4,7 @@ using UnityEngine;
using Script.Gameplay.Interface;
using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
public class ButtonInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable, ISignalSender
{

View File

@@ -1,7 +1,7 @@
using UnityEngine;
using Script.Gameplay.Interface;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
[RequireComponent(typeof(Collider))]
public class ColliderEditableController : MonoBehaviour, IEditableComponent

View File

@@ -5,7 +5,7 @@ using UnityEngine;
using Script.Gameplay.Interface;
using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
public class DoorInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable
{

View File

@@ -0,0 +1,23 @@
using Core;
using UnityEngine;
using System.Collections.Generic;
using Script.Gameplay.Interface;
namespace Script.Gameplay.Edit
{
public class EditableManager : MonoSingleton<EditableManager>
{
public static List<IEditableComponent> GetEditableComponents(GameObject target)
{
var components = new List<IEditableComponent>();
foreach (var mb in target.GetComponentsInChildren<MonoBehaviour>(true))
{
if (mb is IEditableComponent editableComponent)
{
components.Add(editableComponent);
}
}
return components;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9704373d6ec1409bb4e26e4b16324c40
timeCreated: 1761035435

View File

@@ -4,7 +4,7 @@ using UnityEngine;
using Script.Gameplay.Connect;
using Script.Gameplay.Interface;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
public class EmitterController : MonoBehaviour, IEditableComponent, IConnectable
{

View File

@@ -1,7 +1,7 @@
using UnityEngine;
using Script.Gameplay.Interface;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
public abstract class InteractableBaseController : MonoBehaviour, IInteractable
{

View File

@@ -3,7 +3,7 @@ using UnityEngine;
using Script.Gameplay.Interface;
using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
public class LeverInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable, ISignalSender
{

View File

@@ -2,7 +2,7 @@ using System.Collections.Generic;
using UnityEngine;
using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
// 号码枚举类型
public enum NumberType

View File

@@ -3,7 +3,7 @@ using UnityEngine;
using Script.Gameplay.Interface;
using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
public class PressurePlateController : MonoBehaviour, IEditableComponent, IConnectable, ISignalSender
{

View File

@@ -1,7 +1,7 @@
using UnityEngine;
using Script.Gameplay.Interface;
namespace Script.Gameplay.Facility
namespace Script.Gameplay.Edit
{
[RequireComponent(typeof(Rigidbody))]
public class RigidbodyEditableController : MonoBehaviour, IEditableComponent

View File

@@ -1,44 +0,0 @@
using System;
using System.Collections.Generic;
using Script.Gameplay.Player;
using UnityEngine;
using Script.Gameplay.Interface;
namespace Script.Gameplay.Facility
{
public class FacilityEditableController : MonoBehaviour, IEditable
{
public void OnGazeEnter(PlayerEditController editor)
{
}
public void OnGazeExit(PlayerEditController editor)
{
}
public void BeginEdit()
{
}
public void EndEdit()
{
}
public List<IEditableComponent> GetEditableComponents()
{
var components = new List<IEditableComponent>();
foreach (var mb in GetComponentsInChildren<MonoBehaviour>(true))
{
if (mb is IEditableComponent editableComponent)
{
components.Add(editableComponent);
}
}
return components;
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 7986c88b8e1d4944832af56e23188597
timeCreated: 1760664530

View File

@@ -1,16 +1,16 @@
using UnityEngine;
using Script.Gameplay.Player;
using System.Collections.Generic;
using Script.Gameplay.Facility;
// using Script.Gameplay.Facility;
namespace Script.Gameplay.Interface
{
public interface IEditable
{
void OnGazeEnter(PlayerEditController editor); // 玩家开始注视时触发
void OnGazeExit(PlayerEditController editor); // 玩家停止注视时触发
void BeginEdit();
void EndEdit();
List<IEditableComponent> GetEditableComponents();
}
// public interface IEditable
// {
// void OnGazeEnter(PlayerEditController editor); // 玩家开始注视时触发
// void OnGazeExit(PlayerEditController editor); // 玩家停止注视时触发
// void BeginEdit();
// void EndEdit();
// List<IEditableComponent> GetEditableComponents();
// }
}

View File

@@ -12,11 +12,11 @@ namespace Script.Gameplay.Player
[SerializeField] private FirstPersonRaycaster raycaster; // 新增:第一人称射线检测器
public bool IsEnableEditing = true; // 是否启用编辑功能
private bool isEditing = false; // 当前是否处于编辑状态
public event Action<IEditable> OnBeginEditTarget;
public event Action<IEditable> OnEndEditTarget;
public event Action<GameObject> OnBeginEditTarget;
public event Action<GameObject> OnEndEditTarget;
private IEditable currentTarget; // 射线命中的当前可编辑对象(用于按键交互)
private IEditable previousGazedTarget; // 上一次注视的对象(用于注视进入/离开事件)
private GameObject currentTarget; // 射线命中的当前可编辑对象(用于按键交互)
private GameObject previousGazedTarget; // 上一次注视的对象(用于注视进入/离开事件)
private InputManager inputManager;
@@ -43,20 +43,27 @@ namespace Script.Gameplay.Player
void DetectInteractable()
{
if (raycaster == null) return;
GameObject lookAtObj = raycaster.CurrentLookAtObject;
IEditable hitEditable = lookAtObj != null ? lookAtObj.GetComponent<IEditable>() : null;
if(lookAtObj == null) return;
GameObject hitEditable = null;
if (lookAtObj.GetComponent<IEditableComponent>() != null)
{
hitEditable = lookAtObj;
}
// 如果命中对象与之前注视的不一样,触发进入/离开事件
if (hitEditable != previousGazedTarget)
{
if (previousGazedTarget != null)
{
previousGazedTarget.OnGazeExit(this);
// previousGazedTarget.OnGazeExit(this);
}
if (hitEditable != null)
{
hitEditable.OnGazeEnter(this);
// hitEditable.OnGazeEnter(this);
}
previousGazedTarget = hitEditable;
@@ -65,11 +72,6 @@ namespace Script.Gameplay.Player
currentTarget = hitEditable;
}
public IEditable GetCurrentTarget()
{
return currentTarget;
}
private void EditTarget()
{
if (isEditing)

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Core;
using Script.Gameplay.Edit;
using Script.Gameplay.Interface;
using Script.Gameplay.Player;
using UnityEngine;
@@ -26,13 +27,13 @@ namespace UI
ControllerLocator.Instance.TryGetWait<PlayerEditController>(OnGetPlayerEditController);
}
public void OnBeginEdit(IEditable target)
public void OnBeginEdit(GameObject target)
{
this.gameObject.SetActive(true);
if (target != null)
{
ClearComponentUI();
GenerateComponentUI(GetEditableComponentsFromEditable(target));
GenerateComponentUI(EditableManager.GetEditableComponents(target));
}
else
{
@@ -43,7 +44,7 @@ namespace UI
}
}
public void OnEndEdit(IEditable target)
public void OnEndEdit(GameObject target)
{
this.gameObject.SetActive(false);
@@ -63,19 +64,6 @@ namespace UI
//Debug.Log("PlayerEditViewer obtained PlayerEditController.");
}
private List<IEditableComponent> GetEditableComponentsFromEditable(IEditable target)
{
if (target == null)
{
Debug.Log($"editable is null in {nameof(PlayerEditViewer)}");
return null;
}
return target.GetEditableComponents()
.Where(c => c != null)
.ToList();
}
private void ClearComponentUI()
{
foreach (var viewer in componentViewers)