Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public interface IOpenClose
- {
- void open();
- void close();
- event Action OnOpen;
- event Action OnClosed;
- event Action OnOpening;
- event Action OnClosing;
- }
- public class View : MonoBehaviour, IOpenClose
- {
- #if UNITY_EDITOR
- [ReadOnlyWhenNotPlaying]
- [Header("VIEW")]
- public bool doOpen = false;
- private bool _doOpen = false;
- public State curStat;
- void OnValidate()
- {
- if (doOpen != _doOpen)
- {
- _doOpen = doOpen;
- if (doOpen)
- {
- open();
- }
- else
- {
- close();
- }
- }
- }
- #endif
- public enum State
- {
- CLOSED = 0,
- OPENING = 1,
- OPEN = 2,
- CLOSING = 3
- }
- public virtual void complete()
- {
- switch (state)
- {
- case State.CLOSING:
- endClosing();
- break;
- case State.OPENING:
- endOpening();
- break;
- }
- }
- public event Action OnOpen = delegate { };
- public event Action OnClosed = delegate { };
- public event Action OnOpening = delegate { };
- public event Action OnClosing = delegate { };
- public Action openStartOnceAction;
- public Action openEndOnceAction;
- public Action closeStartOnceAction;
- public Action closeEndOnceAction;
- private State _state;
- //public State state { get; protected set; }
- public State state
- {
- get
- {
- return _state;
- }
- protected set
- {
- _state = value;
- #if UNITY_EDITOR
- curStat = _state;
- #endif
- }
- }
- public virtual void open()
- {
- open(null);
- }
- public virtual void open(GameObject closeView = null)
- {
- beginOpening();
- Debug.Log("View open(" + closeView + ")");
- endOpening(closeView);
- }
- public virtual void close()
- {
- close(null);
- }
- public virtual void close(GameObject openView = null)
- {
- beginClosing();
- Debug.Log("View close(" + openView + ")");
- endClosing(openView);
- }
- protected virtual void beginOpening()
- {
- state = State.OPENING;
- try
- {
- if (openStartOnceAction != null)
- {
- openStartOnceAction();
- openStartOnceAction = null;
- }
- }
- catch (Exception)
- {
- }
- OnOpening();
- }
- protected virtual void endOpening(GameObject closeView = null)
- {
- if (closeView != null)
- {
- View closeViewComponent = closeView.GetComponent<View>();
- if (closeViewComponent != null)
- {
- closeViewComponent.close();
- }
- else
- {
- closeView.gameObject.SetActive(true);
- }
- }
- state = State.OPEN;
- try
- {
- if (openEndOnceAction != null)
- {
- openEndOnceAction();
- openEndOnceAction = null;
- }
- }
- catch (Exception)
- {
- }
- OnOpen();
- }
- protected virtual void beginClosing()
- {
- state = State.CLOSING;
- try
- {
- if (closeStartOnceAction != null)
- {
- closeStartOnceAction();
- closeStartOnceAction = null;
- }
- }
- catch (Exception)
- {
- }
- OnClosing();
- }
- protected virtual void endClosing(GameObject openView = null)
- {
- if (openView != null)
- {
- View openViewComponent = openView.GetComponent<View>();
- if (openViewComponent != null)
- {
- openViewComponent.open();
- }
- else
- {
- openView.gameObject.SetActive(true);
- }
- }
- state = State.CLOSED;
- try
- {
- if (closeEndOnceAction != null)
- {
- closeEndOnceAction();
- closeEndOnceAction = null;
- }
- }
- catch (Exception)
- {
- }
- OnClosed();
- }
- }
Add Comment
Please, Sign In to add comment