/**
* Copyright(c) Live2D Inc. All rights reserved.
*
* Use of this source code is governed by the Live2D Open Software license
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
*/
using Live2D.Cubism.Core;
using Live2D.Cubism.Rendering;
using System.Collections.Generic;
using Live2D.Cubism.Framework.Tasking;
using UnityEngine;
using UnityEngine.UI;
using Random = System.Random;
namespace Live2D.Cubism.Samples.AsyncBenchmark
{
///
/// Spawns models for benchmarking.
///
public sealed class ModelSpawner : MonoBehaviour
{
///
/// prefab to spawn.
///
[SerializeField]
public GameObject ModelPrefab;
///
/// UI component representing current model count.
///
[SerializeField]
public Text ModelCountUi;
///
/// Holds the number of instances of the model.
///
public int InstancesCount { get; private set; }
///
/// Model instances.
///
private List Instances { get; set; }
///
/// Component.
///
private BenchmarkController BenchmarkController { get; set; }
#region Interface for UI Elements
///
/// Adds a new instance.
///
public void IncreaseInstances()
{
if (ModelPrefab == null)
{
return;
}
// Spawn new instance.
var instance = Instantiate(ModelPrefab);
var random = new Random();
var offsetX = (float)random.Next(-1000, 1000) / 1000f;
var offsetY = (float)random.Next(-1000, 1000) / 1000f;
var screenToWorld = Camera.main.ScreenToWorldPoint(
new Vector3(
Screen.width,
Screen.height,
Camera.main.nearClipPlane));
instance.transform.position = new Vector3(
screenToWorld.x * offsetX,
screenToWorld.y * offsetY,
instance.transform.position.z);
// Register instance and update UI.
Instances.Add(instance);
// Make sure to assign a unique sorting order to the instance.
instance.GetComponent().SortingOrder = Instances.Count;
// Update propertie.
InstancesCount = Instances.Count;
// Update UI.
ModelCountUi.text = BenchmarkController == null
? Instances.Count.ToString()
: string.Concat("Current Model Count:", Instances.Count.ToString());
}
///
/// Removes an instance.
///
public void DecreaseInstances()
{
// Return early if there's nothing to decrease.
if (Instances.Count == 0)
{
return;
}
// Remove last instance and update UI.
DestroyImmediate(Instances[Instances.Count - 1]);
Instances.RemoveAt(Instances.Count - 1);
ModelCountUi.text = Instances.Count.ToString();
}
#endregion
#region Unity Event Handling
///
/// Called by Unity. Initializes fields.
///
private void Start()
{
Instances = new List();
BenchmarkController = GetComponent();
}
#endregion
}
}