refactor():重构编辑系统,不再需要添加额外的脚本作为可编辑的入口了
This commit is contained in:
@@ -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
|
||||
{
|
@@ -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
|
@@ -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
|
||||
{
|
23
Assets/Script/Gameplay/Edit/EditableManager.cs
Normal file
23
Assets/Script/Gameplay/Edit/EditableManager.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Script/Gameplay/Edit/EditableManager.cs.meta
Normal file
3
Assets/Script/Gameplay/Edit/EditableManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9704373d6ec1409bb4e26e4b16324c40
|
||||
timeCreated: 1761035435
|
@@ -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
|
||||
{
|
@@ -1,7 +1,7 @@
|
||||
using UnityEngine;
|
||||
using Script.Gameplay.Interface;
|
||||
|
||||
namespace Script.Gameplay.Facility
|
||||
namespace Script.Gameplay.Edit
|
||||
{
|
||||
public abstract class InteractableBaseController : MonoBehaviour, IInteractable
|
||||
{
|
@@ -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
|
||||
{
|
@@ -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
|
@@ -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
|
||||
{
|
@@ -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
|
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7986c88b8e1d4944832af56e23188597
|
||||
timeCreated: 1760664530
|
@@ -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();
|
||||
// }
|
||||
}
|
@@ -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)
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user