 Good day to all. In this article I would like to talk about how you can apply the MVP pattern in the process of developing games on the Unity3D platform. Using this template can help streamline code and improve project structure. It should be immediately noted that the article does not give a detailed description of the template itself, but assumes that the reader has basic knowledge about it.
 Good day to all. In this article I would like to talk about how you can apply the MVP pattern in the process of developing games on the Unity3D platform. Using this template can help streamline code and improve project structure. It should be immediately noted that the article does not give a detailed description of the template itself, but assumes that the reader has basic knowledge about it.
public class FunnyRectView : MonoBehaviour, IFunnyRectView { private IFunnyRectPresenter _presenter; public void Awake() { _presenter = new FunnyRectPresenter(this); _presenter.Initialize(); } }  public class FunnyRectView : MonoBehaviour, IFunnyRectView { private IFunnyRectPresenter _presenter; private bool _isInputEnabled; private Animator _animator; private const string JumpAnimationTriggerName = "JumpTrigger"; private const string RotateAnimationTriggerName = "RotateTrigger"; public void Awake() { _presenter = new FunnyRectPresenter(this); _animator = gameObject.GetComponent<Animator>(); _presenter.Initialize(); } public void OnDestroy() { _presenter.Uninitialize(); } public event EventHandler RotationEnd; public event EventHandler JumpEnd; public event EventHandler Clicked; public void OnMouseDown() { if (!_isInputEnabled) return; Clicked(this, EventArgs.Empty); } public void NotifyRotationEnded() { RotationEnd(this, EventArgs.Empty); } public void NotifyJumpEnded() { JumpEnd(this, EventArgs.Empty); } public void DisableInput() { _isInputEnabled = false; } public void EnableInput() { _isInputEnabled = true; } public void Rotate() { _animator.SetTrigger(RotateAnimationTriggerName); } public void Jump() { _animator.SetTrigger(JumpAnimationTriggerName); } public void ChangeColor(Color color) { var spriteRenderer = gameObject.GetComponent<SpriteRenderer>(); spriteRenderer.color = color; _isInputEnabled = true; } }  public class FunnyRectPresenter : IFunnyRectPresenter { private readonly IFunnyRectView _view; private const int FunnyRectRotateActionCode = 0; private const int FunnyRectJumpActionCode = 1; private const int FunnyRectChangeColorActionCode = 2; public FunnyRectPresenter(IFunnyRectView view) { _view = view; } private void OnRectClicked(object sender, EventArgs e) { _view.DisableInput(); var action = GenerateRandomAction(); switch (action) { case FunnyRectRotateActionCode: _view.Rotate(); break; case FunnyRectJumpActionCode: _view.Jump(); break; case FunnyRectChangeColorActionCode: var color = GenerateRandomColor(); _view.ChangeColor(color); break; } } private void OnRotationEnd(object sender, EventArgs e) { _view.EnableInput(); } private void OnJumpEnd(object sender, EventArgs e) { _view.EnableInput(); } private Color GenerateRandomColor() { var random = new Random(); var c = Color.white; cr = (float)random.NextDouble(); cg = (float)random.NextDouble(); cb = (float)random.NextDouble(); return c; } private int GenerateRandomAction() { var random = new Random(); return random.Next(FunnyRectRotateActionCode, FunnyRectChangeColorActionCode+1); } public void Initialize() { _view.Clicked += OnRectClicked; _view.RotationEnd += OnRotationEnd; _view.JumpEnd += OnJumpEnd; _view.EnableInput(); } public void Uninitialize() { _view.Clicked -= OnRectClicked; _view.RotationEnd -= OnRotationEnd; _view.JumpEnd -= OnJumpEnd; } }  public interface IFunnyRectView { event EventHandler RotationEnd; event EventHandler JumpEnd; event EventHandler Clicked; void EnableInput(); void DisableInput(); void Rotate(); void Jump(); void ChangeColor(Color color); }  public interface IFunnyRectPresenter { void Initialize(); void Uninitialize(); } Source: https://habr.com/ru/post/251543/
All Articles