无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

代碼優(yōu)化(3)

2020-05-26 11:21 作者:unity_某某師_高錦錦  | 我要投稿

對象池

using UnityEngine;

using System.Collections;

using System.Collections.Generic;


public interface IPoolableObject{

void New();

void Respawn();

}


public class ObjectPool<T> where T : IPoolableObject, new() {

private List<T> _pool;

private int _currentIndex = 0;

public ObjectPool(int initialCapacity) {

_pool = new List<T>(initialCapacity);

for(int i = 0; i < initialCapacity; ++i) {

Spawn (); // instantiate a pool of N objects

}

Reset ();

}

public int Count {

get { return _pool.Count; }

}

public void Reset() {

_currentIndex = 0;

}

public T Spawn() {

if (_currentIndex < Count) {

T obj = _pool[_currentIndex];

_currentIndex++;

IPoolableObject ip = obj as IPoolableObject;

ip.Respawn();

return obj;

} else {

T obj = new T();

_pool.Add(obj);

_currentIndex++;

IPoolableObject ip = obj as IPoolableObject;

ip.New();

return obj;

}

}

}

use

using UnityEngine;

using System.Collections;


public class TestObject : IPoolableObject {

public void New() {

// very first initialization here

}

public void Respawn() {

// reset data which allows the object to be recycled here

}


public int Test(int num) {

return num * 2;

}

}



public class ObjectPoolTester : MonoBehaviour {


private ObjectPool<TestObject> _objectPool = new ObjectPool<TestObject>(100);

void Update () {

if (Input.GetKeyDown (KeyCode.Space)) {

? ? ? ? ? ? print("is print.");

_objectPool.Reset ();

int sum = 0;

for(int i = 0; i < 100; ++i) {

TestObject obj = _objectPool.Spawn ();

sum += obj.Test(i);

}

//Debug.Log (string.Format ("(Sum 1-to-100) *2 = {0}", sum));

}

}

}


代碼優(yōu)化(3)的評論 (共 條)

分享到微博請遵守國家法律
祥云县| 邻水| 新绛县| 卢龙县| 延寿县| 奈曼旗| 阜宁县| 普兰店市| 大连市| 中阳县| 东乌珠穆沁旗| 玛纳斯县| 巫山县| 台山市| 运城市| 湘潭县| 浦城县| 张家口市| 塔城市| 会同县| 丰县| 类乌齐县| 南靖县| 石家庄市| 永德县| 上饶县| 苗栗县| 封丘县| 家居| 大冶市| 无极县| 竹山县| 四会市| 塘沽区| 宜丰县| 新闻| 建湖县| 新邵县| 崇文区| 定陶县| 定边县|