Unity/유니티 개발
[유니티 개발] Game Programming Pattern(SOLID 패턴)
도이(doi)
2022. 5. 9. 15:47
728x90
Manager 스크립트
using UnityEngine;
public class SelectionManager : MonoBehaviour
{
private IRayProvider _rayProvider;
private ISelector _selector;
private ISelectionResponse _selectionResponse;
private Transform _currentSelection;
private void Awake()
{
_rayProvider = GetComponent<IRayProvider>();
_selector = GetComponent<ISelector>();
_selectionResponse = GetComponent<ISelectionResponse>();
}
private void Update()
{
if (_currentSelection != null)
_selectionResponse.OnDeselect(_currentSelection);
_selector.Check(_rayProvider.CreateRay());
_currentSelection = _selector.GetSelection();
if (_currentSelection != null)
_selectionResponse.OnSelect(_currentSelection);
}
}
Iselector 인터페이스와
Iselector 인터페이스를 상속한 구현(Implement)
RayCastBasedTagSelector 스크립트
using UnityEngine;
public interface ISelector
{
void Check(Ray ray);
Transform GetSelection();
}
using UnityEngine;
public class RayCastBasedTagSelector : MonoBehaviour, ISelector
{
[SerializeField] private string selectableTag = "Selectable";
private Transform _selection;
public void Check(Ray ray)
{
_selection = null;
if (!Physics.Raycast(ray, out var hit)) return;
var selection = hit.transform;
if (selection.CompareTag(selectableTag))
{
_selection = selection;
}
}
public Transform GetSelection()
{
return _selection;
}
}
IselectionResponse 인터페이스와
IselectionResponse 인터페이스를 상속한 구현(Implement)
OutlineSelectionResponse 스크립트
using UnityEngine;
internal interface ISelectionResponse
{
void OnSelect(Transform selection);
void OnDeselect(Transform selection);
}
using UnityEngine;
public class OutlineSelectionResponse : MonoBehaviour, ISelectionResponse
{
public void OnSelect(Transform selection)
{
var outline = selection.GetComponent<Outline>();
if (outline != null)
{
outline.OutlineWidth = 10;
}
}
public void OnDeselect(Transform selection)
{
var outline = selection.GetComponent<Outline>();
if (outline != null)
{
outline.OutlineWidth = 0;
}
}
}
IRayProvider 인터페이스와
IRayProvider 인터페이스를 상속한 구현(Implement)
MouseScreenRayProvider 스크립트
using UnityEngine;
public interface IRayProvider
{
Ray CreateRay();
}
using UnityEngine;
public class MouseScreenRayProvider : MonoBehaviour, IRayProvider
{
public Ray CreateRay()
{
return Camera.main.ScreenPointToRay(Input.mousePosition);
}
}
https://github.com/LucasDiogo96/S.O.L.I.D
GitHub - LucasDiogo96/S.O.L.I.D: This repository explains a summary of the solid pattern with examples in C #
This repository explains a summary of the solid pattern with examples in C # - GitHub - LucasDiogo96/S.O.L.I.D: This repository explains a summary of the solid pattern with examples in C #
github.com
728x90
반응형