Random#
Collections / Random
Can Read
Cannot Write
Description#
A Game Object picked at random from a collection
Parameters#
| Name | Description |
|---|---|
| Collection | The collection the Game Object is picked from |
Code#
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
using VisualScript.Icons;
using Random = UnityEngine.Random;
namespace VisualScript.Runtime
{
[Get]
[Title("Random")]
[Category("Collections/Random")]
[Description("A Game Object picked at random from a collection")]
[Image(typeof(IconListRandom), ColorTheme.Type.Teal)]
[Parameter("Collection", "The collection the Game Object is picked from")]
[Keywords("Dice", "Any", "Choose", "Pick", "Shuffle", "List", "Array")]
[Serializable]
public class TypeGameObjectCollectionRandom : TypeGameObject
{
[SerializeField] private GetCollectionGameObjects m_Collection = new GetCollectionGameObjects();
public override GameObject Get(Args args)
{
List<GameObject> buffer = ListPool<GameObject>.Get();
try
{
this.m_Collection.Get(args, buffer);
GameObject result = buffer.Count > 0 ? buffer[Random.Range(0, buffer.Count)] : null;
return result;
}
finally
{
ListPool<GameObject>.Release(buffer);
}
}
public override string ToString()
{
return $"Random of {this.m_Collection}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnUpdate;
}
}