ModelSpawner.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Core;
  8. using Live2D.Cubism.Rendering;
  9. using System.Collections.Generic;
  10. using Live2D.Cubism.Framework.Tasking;
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. using Random = System.Random;
  14. namespace Live2D.Cubism.Samples.AsyncBenchmark
  15. {
  16. /// <summary>
  17. /// Spawns models for benchmarking.
  18. /// </summary>
  19. public sealed class ModelSpawner : MonoBehaviour
  20. {
  21. /// <summary>
  22. /// <see cref="CubismModel"/> prefab to spawn.
  23. /// </summary>
  24. [SerializeField]
  25. public GameObject ModelPrefab;
  26. /// <summary>
  27. /// UI component representing current model count.
  28. /// </summary>
  29. [SerializeField]
  30. public Text ModelCountUi;
  31. /// <summary>
  32. /// Holds the number of instances of the model.
  33. /// </summary>
  34. public int InstancesCount { get; private set; }
  35. /// <summary>
  36. /// Model instances.
  37. /// </summary>
  38. private List<GameObject> Instances { get; set; }
  39. /// <summary>
  40. /// <see cref="AsyncBenchmark.BenchmarkController"/> Component.
  41. /// </summary>
  42. private BenchmarkController BenchmarkController { get; set; }
  43. #region Interface for UI Elements
  44. /// <summary>
  45. /// Adds a new instance.
  46. /// </summary>
  47. public void IncreaseInstances()
  48. {
  49. if (ModelPrefab == null)
  50. {
  51. return;
  52. }
  53. // Spawn new instance.
  54. var instance = Instantiate(ModelPrefab);
  55. var random = new Random();
  56. var offsetX = (float)random.Next(-1000, 1000) / 1000f;
  57. var offsetY = (float)random.Next(-1000, 1000) / 1000f;
  58. var screenToWorld = Camera.main.ScreenToWorldPoint(
  59. new Vector3(
  60. Screen.width,
  61. Screen.height,
  62. Camera.main.nearClipPlane));
  63. instance.transform.position = new Vector3(
  64. screenToWorld.x * offsetX,
  65. screenToWorld.y * offsetY,
  66. instance.transform.position.z);
  67. // Register instance and update UI.
  68. Instances.Add(instance);
  69. // Make sure to assign a unique sorting order to the instance.
  70. instance.GetComponent<CubismRenderController>().SortingOrder = Instances.Count;
  71. // Update propertie.
  72. InstancesCount = Instances.Count;
  73. // Update UI.
  74. ModelCountUi.text = BenchmarkController == null
  75. ? Instances.Count.ToString()
  76. : string.Concat("Current Model Count:", Instances.Count.ToString());
  77. }
  78. /// <summary>
  79. /// Removes an instance.
  80. /// </summary>
  81. public void DecreaseInstances()
  82. {
  83. // Return early if there's nothing to decrease.
  84. if (Instances.Count == 0)
  85. {
  86. return;
  87. }
  88. // Remove last instance and update UI.
  89. DestroyImmediate(Instances[Instances.Count - 1]);
  90. Instances.RemoveAt(Instances.Count - 1);
  91. ModelCountUi.text = Instances.Count.ToString();
  92. }
  93. #endregion
  94. #region Unity Event Handling
  95. /// <summary>
  96. /// Called by Unity. Initializes fields.
  97. /// </summary>
  98. private void Start()
  99. {
  100. Instances = new List<GameObject>();
  101. BenchmarkController = GetComponent<BenchmarkController>();
  102. }
  103. #endregion
  104. }
  105. }