Last active
September 11, 2015 12:35
-
-
Save nicloay/4cfc395a55b43a2b068d to your computer and use it in GitHub Desktop.
Unity3d pause monobehaviour without timescale
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 UnityEngine; | |
| using System.Collections.Generic; | |
| public class GameManager : MonoBehaviour { | |
| readonly List<LifeTimeComponent> registeredComponents = new List<LifeTimeComponent>(); | |
| public void RegisterLifeComponent(LifeTimeComponent component){ | |
| if (!registeredComponents.Contains(component)) | |
| registeredComponents.Add(component); | |
| } | |
| public static void UnregisterComponent(LifeTimeComponent component){ | |
| if (instance != null) | |
| instance.registeredComponents.Remove(component); | |
| } | |
| public void PauseGame(){ | |
| Screen.lockCursor = false; | |
| LevelCommand.PauseAllCommands = true; | |
| registeredComponents.ForEach(x=> x.Pause()); | |
| } | |
| public void UnpauseGame(){ | |
| Screen.lockCursor = true; | |
| LevelCommand.PauseAllCommands = false; | |
| registeredComponents.ForEach(x=>x.Unpause()); | |
| } | |
| } |
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 UnityEngine; | |
| using System.Collections; | |
| public class LifeTimeComponent : MonoBehaviour { | |
| bool isActive; | |
| public bool IsActive { | |
| get { | |
| return isActive; | |
| } | |
| } | |
| void Start(){ | |
| Activate(); | |
| } | |
| void OnEnable(){ | |
| Activate(); | |
| } | |
| void OnDisable(){ | |
| Deactivate(); | |
| } | |
| public void Activate(){ | |
| isActive = true; | |
| GameManager.Instance.RegisterLifeComponent(this); | |
| SendMessage("OnActivate", SendMessageOptions.DontRequireReceiver); | |
| } | |
| public void Deactivate(){ | |
| isActive = false; | |
| GameManager.UnregisterComponent(this); | |
| SendMessage("OnDeactivate", SendMessageOptions.DontRequireReceiver); | |
| } | |
| bool paused = false; | |
| public void Pause(){ | |
| isActive = false; | |
| PauseRigidbody(); | |
| PauseAnimation(); | |
| SendMessage("OnPause", SendMessageOptions.DontRequireReceiver); | |
| paused = true; | |
| } | |
| public void Unpause(){ | |
| if (!paused) | |
| return; | |
| isActive = true; | |
| UnpauseRigidbody(); | |
| UnpauseAnimation(); | |
| SendMessage("OnUnpause", SendMessageOptions.DontRequireReceiver); | |
| paused = false; | |
| } | |
| Vector2 savedVelocity; | |
| float savedAngularVelocity; | |
| void PauseRigidbody () | |
| { | |
| if (rigidbody2D == null) | |
| return; | |
| savedVelocity = rigidbody2D.velocity; | |
| savedAngularVelocity = rigidbody2D.angularVelocity; | |
| rigidbody2D.isKinematic = true; | |
| } | |
| void UnpauseRigidbody () | |
| { | |
| if (rigidbody2D == null) | |
| return; | |
| rigidbody2D.isKinematic = false; | |
| rigidbody2D.AddForce(savedVelocity * (rigidbody2D.mass == 0.0f ? 1.0f : rigidbody2D.mass), ForceMode2D.Impulse); | |
| rigidbody2D.AddTorque(savedAngularVelocity * (rigidbody2D.mass == 0.0f ? 1.0f : rigidbody2D.mass), ForceMode2D.Impulse); | |
| rigidbody2D.WakeUp(); | |
| } | |
| void PauseAnimation () | |
| { | |
| SetAnimationSpeed(0.0f); | |
| } | |
| void UnpauseAnimation () | |
| { | |
| SetAnimationSpeed(1.0f); | |
| } | |
| public void SetAnimationSpeed(float speed){ | |
| if (animation != null){ | |
| foreach(AnimationState animationState in animation){ | |
| animationState.speed = speed; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment