Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- /// <summary>A utility script to pause and unpause active elements in a Unity game
- /// Latest version at: https://pastebin.com/FaT6i5yF </summary>
- /// <description>MIT License - TL;DR - This code is free, don't bother me about it!</description>
- /// <author email="mvaganov@hotmail.com">Michael Vaganov</author>
- public class StopPhysics : MonoBehaviour {
- /// <summary>keep track of an object's physics info</summary>
- private struct Freeze {
- public GameObject go;
- public Vector3 v;
- public Freeze(GameObject go){
- this.go = go;
- Rigidbody rb = go.GetComponent<Rigidbody>();
- if (rb) {
- v=rb.velocity;
- rb.isKinematic=true;
- } else {
- v = Vector3.zero;
- }
- Animation a = go.GetComponent<Animation>();
- if(a) { a[a.clip.name].speed = 0; }
- }
- public void Unfreeze() {
- Rigidbody rb = go.GetComponent<Rigidbody>();
- if (rb) {
- rb.velocity = v;
- rb.isKinematic = false;
- }
- Animation a = go.GetComponent<Animation>();
- if(a) { a[a.clip.name].speed = 1; }
- }
- }
- /// <summary>list of frozen things, saved before the objects are halted.</summary>
- private static Freeze[] snapshot = null;
- /// <returns><c>true</c> if the physics is frozen; otherwise, <c>false</c>.</returns>
- public static bool IsStopped() { return snapshot != null; }
- public void TogglePhysics() { StopPhysics.Toggle (); }
- public void disablePhysics() {
- SetupIfNeeded ();
- whenDeactivates.Invoke ();
- DisablePhysics ();
- }
- public static void DisablePhysics() {
- Rigidbody[] bodies = GameObject.FindObjectsOfType<Rigidbody> ();
- Animation[] anims = GameObject.FindObjectsOfType<Animation> ();
- snapshot = new Freeze[bodies.Length+anims.Length];
- for (int i = 0; i < bodies.Length; ++i) { snapshot [i] = new Freeze(bodies [i].gameObject); }
- for (int i = 0; i < anims.Length; ++i) { snapshot [bodies.Length+i] = new Freeze(anims [i].gameObject); }
- }
- public void enablePhysics() {
- SetupIfNeeded ();
- whenActivates.Invoke ();
- EnablePhysics ();
- }
- public static void EnablePhysics() {
- for (int i = 0; i < snapshot.Length; ++i) {
- if (snapshot [i].go != null) {
- snapshot [i].Unfreeze();
- }
- }
- snapshot = null;
- }
- /// <summary>Toggles the rigibdody physics.</summary>
- public static void Toggle() {
- if (IsStopped ()) { EnablePhysics (); } else { DisablePhysics (); }
- }
- #region find-and-disable classes
- public static System.Type GetType( string typeName ) {
- // Try Type.GetType() first. This will work with types defined by the Mono runtime, in the same assembly as the caller, etc.
- System.Type type = System.Type.GetType( typeName );
- // If it worked, then we're done here
- if( type != null ) return type;
- // If we still haven't found the proper type, we can enumerate all of the loaded assemblies and see if any of them define the type
- try{
- var currentAssembly = System.Reflection.Assembly.GetExecutingAssembly();
- var referencedAssemblies = currentAssembly.GetReferencedAssemblies();
- foreach( var assemblyName in referencedAssemblies ) {
- // Load the referenced assembly
- var assembly = System.Reflection.Assembly.Load( assemblyName );
- if( assembly != null ) {
- // See if that assembly defines the named type
- type = assembly.GetType( typeName );
- if( type != null ) return type;
- }
- }
- } catch (System.Exception){ }
- // if the GetExecutingAssembly library call failed, possibly due to security constraints, try each known assembly individually
- System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies ();
- for (int i = 0; i < assemblies.Length; ++i) {
- type = assemblies [i].GetType (typeName);
- if (type != null) return type;
- }
- // If the TypeName is a full name in an un-loaded assembly, then we can try loading the defining assembly directly
- if( typeName.Contains( "." ) ) {
- try{
- // Get the name of the assembly (Assumption is that we are using fully-qualified type names)
- var assemblyName = typeName.Substring( 0, typeName.IndexOf( '.' ) );
- // Attempt to load the indicated Assembly
- var assembly = System.Reflection.Assembly.Load( assemblyName );
- if( assembly == null ) return null;
- // Ask that assembly to return the proper Type
- type = assembly.GetType( typeName );
- if( type != null ) return type;
- }catch(System.Exception ){}
- }
- // The type just couldn't be found...
- return null;
- }
- [Tooltip("Will search for a MonoBehaviours with the given complete name to disable.")]
- public string[] classTypesToToggle = new string[]{
- "UnityStandardAssets.Characters.FirstPerson.FirstPersonController",
- "UnityStandardAssets.Characters.FirstPerson.RigidbodyFirstPersonController"
- };
- private System.Type[] knownTypes;
- [Tooltip("When physics re-activates.")]
- public UnityEngine.Events.UnityEvent whenActivates;
- [Tooltip("When physics de-activates.")]
- public UnityEngine.Events.UnityEvent whenDeactivates;
- private void SetupIfNeeded() {
- if (knownTypes == null) { Setup (); }
- }
- private void Setup () {
- knownTypes = null;
- for (int i = 0; i < classTypesToToggle.Length; ++i) {
- System.Type t = GetType(classTypesToToggle[i]);
- if (t != null) {
- if (knownTypes == null) {
- knownTypes = new System.Type[classTypesToToggle.Length];
- }
- knownTypes [i] = t;
- }
- }
- if (knownTypes != null) {
- whenActivates.AddListener(()=>{SetEnabled (true);});
- whenDeactivates.AddListener(()=>{SetEnabled (false);});
- }
- }
- private void SetEnabledAllBehavioursOfType(System.Type type, bool enabled) {
- if (type == null) return;
- Object[] objs = GameObject.FindObjectsOfType (type);
- for (int o = 0; o < objs .Length; ++o) {
- Behaviour b = objs [o] as Behaviour;
- if (b != null) {
- b.enabled = enabled;
- }
- }
- }
- public static void SetEnableMouse(bool enabled) {
- Cursor.visible = !enabled;
- Cursor.lockState = enabled ? CursorLockMode.Locked : CursorLockMode.None;
- }
- private void SetEnabled(bool enabled) {
- if (knownTypes != null) {
- for (int i = 0; i < knownTypes.Length; ++i) {
- if (knownTypes [i] != null) {
- SetEnabledAllBehavioursOfType (knownTypes [i], enabled);
- }
- }
- }
- SetEnableMouse (enabled);
- }
- #endregion // find-and-disable classes
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement