VR模拟枪支打靶,消灭鬼怪,换弹以及上弦等等硬核枪支操作。 使用HTCVive设备,开启SteamVR进行游玩。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

62 lines
1.8 KiB

//========= Copyright 2016-2018, HTC Corporation. All rights reserved. ===========
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace HTC.UnityPlugin.Utility
{
public class ObjectPool<T>
{
private readonly Stack<T> stack = new Stack<T>();
private readonly Func<T> actionOnNew;
private readonly UnityAction<T> actionOnGet;
private readonly UnityAction<T> actionOnRelease;
public int CountAll { get; private set; }
public int CountInactive { get { return stack.Count; } }
public int CountActive { get { return CountAll - CountInactive; } }
public ObjectPool(Func<T> onNew, UnityAction<T> onGet = null, UnityAction<T> onRelease = null)
{
actionOnNew = onNew;
actionOnGet = onGet;
actionOnRelease = onRelease;
}
public T Get()
{
T element;
if (stack.Count == 0)
{
element = actionOnNew == null ? default(T) : actionOnNew.Invoke();
CountAll++;
}
else
{
element = stack.Pop();
}
if (actionOnGet != null) { actionOnGet.Invoke(element); }
return element;
}
public void Release(T element)
{
if (stack.Count > 0 && ReferenceEquals(stack.Peek(), element))
{
Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
}
if (actionOnRelease != null) { actionOnRelease.Invoke(element); }
stack.Push(element);
if (stack.Count > CountAll)
{
CountAll = stack.Count;
}
}
}
}