private void NormalTest() { Log.L("Create a auto create pool"); MyPool pool = new MyPool(); Log.L("Get an instance from pool"); var _obj = pool.Get(); Log.L($"the type of instance is {_obj.GetType()}"); Log.L("Let's put the instance to pool"); pool.Set(_obj); }
private ObjectPool _spawnPool;//内存池对象 // Use this for initialization void Start() { _instance = this; _spawnPool = PoolManager.Pools["MyPool"]; InitializePool(/*内存池对象*/ _spawnPool, /*文件夹名称*/ StaticParameter.s_Folder_GameEffect, /*预制体名称*/ StaticParameter.s_Prefab_SwordLight, /*默认初始化Prefab数量*/ 4, /*实例化限制*/ true, /*无限取Prefab*/ true, /*限制池子里最大的Prefab数量*/ 4, /*自动清理池子*/ false, /*最终保留*/ 3, /*多久清理一次*/ 1, /*每次清理几个*/ 1); InitializePool(/*内存池对象*/ _spawnPool, /*文件夹名称*/ StaticParameter.s_Folder_GameEffect, /*预制体名称*/ StaticParameter.s_Prefab_Juice, /*默认初始化Prefab数量*/ 6, /*实例化限制*/ true, /*无限取Prefab*/ true, /*限制池子里最大的Prefab数量*/ 6, /*自动清理池子*/ false, /*最终保留*/ 4, /*多久清理一次*/ 1, /*每次清理几个*/ 2); InitializePool(/*内存池对象*/ _spawnPool, /*文件夹名称*/ StaticParameter.s_Folder_GameEffect, /*预制体名称*/ StaticParameter.s_Prefab_Line, /*默认初始化Prefab数量*/ 3, /*实例化限制*/ false, /*无限取Prefab*/ true, /*限制池子里最大的Prefab数量*/ 6, /*自动清理池子*/ true, /*最终保留*/ 2, /*多久清理一次*/ 5, /*每次清理几个*/ 2); InitializePool(/*内存池对象*/ _spawnPool, /*文件夹名称*/ StaticParameter.s_Folder_GameEffect, /*预制体名称*/ StaticParameter.s_Prefab_Fire, /*默认初始化Prefab数量*/ 3, /*实例化限制*/ false, /*无限取Prefab*/ true, /*限制池子里最大的Prefab数量*/ 6, /*自动清理池子*/ true, /*最终保留*/ 2, /*多久清理一次*/ 5, /*每次清理几个*/ 2); }
/// <summary> /// 自己管理生命周期的对象池例子 /// </summary> private void CustomPoolExample() { Log.L(""); Log.L("————这个例子讲述如何使用自己管理生命周期的对象池————"); Log.L("创建一个自己管理生命周期的对象池"); MyPool pool = new MyPool(); Log.L("从对象池中取一个对象"); var _obj = pool.Get(); Log.L($"取出的对象类型为 {_obj.GetType()}\n"); Log.L("将对象归还到对象池"); pool.Set(_obj); Log.L("再从对象池中取一个对象"); _obj = pool.Get(); }
void OnBulletDestroyed(GameObject bullet) { // Recycle your GameObject // 1/ If you need the label, you can retrieve it this way BulletScript bulletScript = bullet.GetComponent <BulletScript>(); if (bulletScript != null) { BulletPool pool = null; string bulletName = bulletScript.Bullet.Label.ToLower(); // TODO: Your pool here MyPool.Recycle(bulletScript); } // 2/ Otherwise you have a direct reference to the bullet's GameObject // TODO: Your pool here MyPool.Recycle(bullet); }
BulletScript OnBulletSpawned(BulletObject bullet, string bulletName) { // Get a GameObject from the pool // bulletName and bullet can help you identify the bullet you want // TODO: Your pool here GameObject go = MyPool.Get(); // Make sure this GameObject has a BulletScript and return it // BulletScript can be added on the fly, there is no special parameter to pass BulletScript bulletScript = go.GetComponent <BulletScript>(); if (bulletScript == null) { bulletScript = go.AddComponent <BulletScript>(); } return(bulletScript); }
void InitializePool(WWW www) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(www.text); XmlNodeList nodeList = xmlDoc.SelectSingleNode("root").ChildNodes; foreach (XmlElement xmlelement in nodeList) { foreach (GameObject prefab in _prefabs) { if (prefab.name == xmlelement.Name) { MyPool.InitializePool(/*内存池对象*/ _spawnPool, /*预制体文件*/ prefab, /*默认初始化Prefab数量*/ 4, /*实例化限制*/ false, /*无限取Prefab*/ true, /*限制池子里最大的Prefab数量*/ 4, /*自动清理池子*/ true, /*最终保留*/ 4, /*多久清理一次*/ 1, /*每次清理几个*/ 1); } } } }
//创建实例 Transform CreatInstance(int mark, string prefabName, Vector3 pos, Quaternion rot) { bool isContains = false; //检测内存池内是否含有指定预制体 for (int i = 0; i < _spawnPool._perPrefabPoolOptions.Count; i++) { if (_spawnPool._perPrefabPoolOptions[i].prefab.name.Contains(prefabName)) { isContains = true; break; } } if (!isContains) { MyPool.InitializePool(/*内存池对象*/ _spawnPool, /*文件夹名称*/ "Prefab", /*预制体名称*/ prefabName, /*默认初始化Prefab数量*/ 2, /*实例化限制*/ false, /*无限取Prefab*/ true, /*限制池子里最大的Prefab数量*/ 4, /*自动清理池子*/ true, /*最终保留*/ 3, /*多久清理一次*/ 1, /*每次清理几个*/ 1); } Transform temp = _spawnPool.Active(prefabName, pos, rot); if (mark == 0) { _now_List.Add(temp); } else if (mark == 1) { _middle_List.Add(temp); } else { _last_List.Add(temp); } return(temp); }
static void Main(string[] args) { pool = DBFactory.CreateMySqlPool("Database=jcpt_auth;Data Source=10.1.55.44;Port=3306;User Id=auggie;Password=123456;Charset=utf8;Pooling=false", TimeOut: 20); for (int i = 0; i < 20; i++) { int curTask = i; Task.Factory.StartNew(() => { RunAct(curTask); }); } bool first = false; while (true) { Console.WriteLine("当前连接数:" + pool.CurrentCount); if (pool.CurrentCount == 20) { first = true; } if (first && pool.CurrentCount < 5) { first = false; for (int i = 0; i < 10; i++) { int curTask = i; Task.Factory.StartNew(() => { RunAct(curTask); }); } } System.Threading.Thread.Sleep(1000); } }
/// <summary>打开客户端</summary> public virtual Boolean Open() { if (Active) { return(true); } lock (Root) { if (Active) { return(true); } var ss = Servers; if (ss == null || ss.Length == 0) { throw new ArgumentNullException(nameof(Servers), "未指定服务端地址"); } if (Pool == null) { Pool = new MyPool { Host = this } } ; if (Encoder == null) { Encoder = new JsonEncoder(); } //if (Encoder == null) Encoder = new BinaryEncoder(); if (Handler == null) { Handler = new ApiHandler { Host = this } } ; if (StatInvoke == null) { StatInvoke = new PerfCounter(); } if (StatProcess == null) { StatProcess = new PerfCounter(); } if (StatSend == null) { StatSend = new PerfCounter(); } if (StatReceive == null) { StatReceive = new PerfCounter(); } Encoder.Log = EncoderLog; // 不要阻塞打开,各个线程从池里借出连接来使用 //var ct = Pool.Get(); //try //{ // // 打开网络连接 // if (!ct.Open()) return false; //} //finally //{ // Pool.Put(ct); //} ShowService(); var ms = StatPeriod * 1000; if (ms > 0) { _Timer = new TimerX(DoWork, null, ms, ms) { Async = true } } ; return(Active = true); } }
//private TcpClient _Client; //private Stream _Stream; #endregion #region 构造 /// <summary>实例化</summary> public ClusterClient() { _Pool = new MyPool { Client = this }; }