AsyncToggler.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using Live2D.Cubism.Framework.Tasking;
  8. using UnityEngine;
  9. namespace Live2D.Cubism.Samples.AsyncBenchmark
  10. {
  11. /// <summary>
  12. /// Shows how to enable the <see cref="CubismBuiltinAsyncTaskHandler"/> from script.
  13. /// </summary>
  14. public sealed class AsyncToggler : MonoBehaviour
  15. {
  16. #if !UNITY_WEBGL
  17. /// <summary>
  18. /// Controls async task handling.
  19. /// </summary>
  20. public bool EnableAsync = true;
  21. /// <summary>
  22. /// Last <see cref="EnableAsync"/> state.
  23. /// </summary>
  24. private bool LastEnableSync { get; set; }
  25. #endif
  26. #region Unity Event Handling
  27. #if UNITY_WEBGL
  28. /// <summary>
  29. /// Called by Unity.
  30. /// </summary>
  31. private void Start()
  32. {
  33. // Deactivate Async.
  34. CubismBuiltinAsyncTaskHandler.Deactivate();
  35. }
  36. #else
  37. /// <summary>
  38. /// Called by Unity. Enables/Disables async task handler.
  39. /// </summary>
  40. private void Update()
  41. {
  42. if (EnableAsync == LastEnableSync)
  43. {
  44. return;
  45. }
  46. if (EnableAsync)
  47. {
  48. CubismBuiltinAsyncTaskHandler.Activate();
  49. }
  50. else
  51. {
  52. CubismBuiltinAsyncTaskHandler.Deactivate();
  53. }
  54. LastEnableSync = EnableAsync;
  55. }
  56. /// <summary>
  57. /// Called by Unity. Disables async task handler.
  58. /// </summary>
  59. private void OnDestroy()
  60. {
  61. EnableAsync = false;
  62. Update();
  63. }
  64. #endif
  65. #endregion
  66. }
  67. }