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

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

分享到微博請遵守國家法律
泽库县| 滨海县| 临泉县| 台南市| 临沭县| 淮阳县| 民权县| 罗城| 公安县| 葵青区| 松阳县| 柘荣县| 洪雅县| 临江市| 东乌珠穆沁旗| 电白县| 汕头市| 白朗县| 逊克县| 凯里市| 临湘市| 舞阳县| 德保县| 随州市| 怀集县| 泰和县| 苗栗县| 凤阳县| 西盟| 彭山县| 林芝县| 连城县| 邵武市| 自治县| 措美县| 从江县| 新源县| 吉林省| 边坝县| 台中县| 武平县|