最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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)的評論 (共 條)

分享到微博請遵守國家法律
沙河市| 洱源县| 台山市| 义马市| 太湖县| 阿勒泰市| 横山县| 洪江市| 吉首市| 丹凤县| 衡阳市| 小金县| 罗定市| 甘孜县| 灵寿县| 建瓯市| 阜阳市| 万州区| 赣榆县| 福鼎市| 新建县| 弋阳县| 湟源县| 克山县| 祁东县| 京山县| 泰和县| 鱼台县| 林州市| 安义县| 芜湖市| 曲阳县| 桐柏县| 平江县| 桑植县| 高清| 施甸县| 廊坊市| 新竹市| 高邮市| 孝感市|