Alexander_Fedoseev

View

Aug 28th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public interface IOpenClose
  6. {
  7.     void open();
  8.     void close();
  9.  
  10.     event Action OnOpen;
  11.     event Action OnClosed;
  12.     event Action OnOpening;
  13.     event Action OnClosing;
  14. }
  15. public class View : MonoBehaviour, IOpenClose
  16. {
  17.  
  18.  
  19. #if UNITY_EDITOR
  20.     [ReadOnlyWhenNotPlaying]
  21.     [Header("VIEW")]
  22.  
  23.     public bool doOpen = false;
  24.     private bool _doOpen = false;
  25.     public State curStat;
  26.  
  27.  
  28.     void OnValidate()
  29.     {
  30.         if (doOpen != _doOpen)
  31.         {
  32.             _doOpen = doOpen;
  33.  
  34.             if (doOpen)
  35.             {
  36.                 open();
  37.             }
  38.             else
  39.             {
  40.                 close();
  41.             }
  42.         }
  43.     }
  44. #endif
  45.  
  46.     public enum State
  47.     {
  48.         CLOSED = 0,
  49.         OPENING = 1,
  50.         OPEN = 2,
  51.         CLOSING = 3
  52.     }
  53.  
  54.     public virtual void complete()
  55.     {
  56.         switch (state)
  57.         {
  58.             case State.CLOSING:
  59.                 endClosing();
  60.                 break;
  61.             case State.OPENING:
  62.                 endOpening();
  63.                 break;
  64.         }
  65.     }
  66.  
  67.     public event Action OnOpen = delegate { };
  68.     public event Action OnClosed = delegate { };
  69.     public event Action OnOpening = delegate { };
  70.     public event Action OnClosing = delegate { };
  71.  
  72.     public Action openStartOnceAction;
  73.     public Action openEndOnceAction;
  74.     public Action closeStartOnceAction;
  75.     public Action closeEndOnceAction;
  76.  
  77.     private State _state;
  78.     //public State state { get; protected set; }
  79.  
  80.     public State state
  81.     {
  82.         get
  83.         {
  84.             return _state;
  85.         }
  86.         protected set
  87.         {
  88.             _state = value;
  89. #if UNITY_EDITOR
  90.             curStat = _state;
  91. #endif
  92.         }
  93.     }
  94.  
  95.     public virtual void open()
  96.     {
  97.         open(null);
  98.     }
  99.     public virtual void open(GameObject closeView = null)
  100.     {
  101.         beginOpening();
  102.         Debug.Log("View open(" + closeView + ")");
  103.         endOpening(closeView);
  104.     }
  105.  
  106.     public virtual void close()
  107.     {
  108.         close(null);
  109.     }
  110.     public virtual void close(GameObject openView = null)
  111.     {
  112.         beginClosing();
  113.         Debug.Log("View close(" + openView + ")");
  114.         endClosing(openView);
  115.     }
  116.  
  117.     protected virtual void beginOpening()
  118.     {
  119.         state = State.OPENING;
  120.  
  121.  
  122.         try
  123.         {
  124.  
  125.             if (openStartOnceAction != null)
  126.             {
  127.                 openStartOnceAction();
  128.  
  129.                 openStartOnceAction = null;
  130.             }
  131.  
  132.         }
  133.         catch (Exception)
  134.         {
  135.  
  136.         }
  137.  
  138.  
  139.         OnOpening();
  140.     }
  141.  
  142.     protected virtual void endOpening(GameObject closeView = null)
  143.     {
  144.  
  145.  
  146.         if (closeView != null)
  147.         {
  148.             View closeViewComponent = closeView.GetComponent<View>();
  149.  
  150.             if (closeViewComponent != null)
  151.             {
  152.                 closeViewComponent.close();
  153.             }
  154.             else
  155.             {
  156.                 closeView.gameObject.SetActive(true);
  157.             }
  158.         }
  159.  
  160.         state = State.OPEN;
  161.  
  162.  
  163.         try
  164.         {
  165.  
  166.             if (openEndOnceAction != null)
  167.             {
  168.                 openEndOnceAction();
  169.  
  170.                 openEndOnceAction = null;
  171.             }
  172.  
  173.         }
  174.         catch (Exception)
  175.         {
  176.  
  177.         }
  178.  
  179.         OnOpen();
  180.     }
  181.  
  182.     protected virtual void beginClosing()
  183.     {
  184.         state = State.CLOSING;
  185.  
  186.         try
  187.         {
  188.  
  189.             if (closeStartOnceAction != null)
  190.             {
  191.                 closeStartOnceAction();
  192.  
  193.                 closeStartOnceAction = null;
  194.             }
  195.  
  196.         }
  197.         catch (Exception)
  198.         {
  199.  
  200.         }
  201.  
  202.         OnClosing();
  203.     }
  204.  
  205.     protected virtual void endClosing(GameObject openView = null)
  206.     {
  207.  
  208.         if (openView != null)
  209.         {
  210.             View openViewComponent = openView.GetComponent<View>();
  211.  
  212.             if (openViewComponent != null)
  213.             {
  214.                 openViewComponent.open();
  215.             }
  216.             else
  217.             {
  218.                 openView.gameObject.SetActive(true);
  219.             }
  220.         }
  221.  
  222.         state = State.CLOSED;
  223.  
  224.         try
  225.         {
  226.  
  227.             if (closeEndOnceAction != null)
  228.             {
  229.                 closeEndOnceAction();
  230.  
  231.                 closeEndOnceAction = null;
  232.             }
  233.  
  234.         }
  235.         catch (Exception)
  236.         {
  237.  
  238.         }
  239.  
  240.         OnClosed();
  241.     }
  242.  
  243.  
  244. }
Add Comment
Please, Sign In to add comment