五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

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

unity 自己寫的對象池腳本 分享給大家

2023-07-13 16:55 作者:超級馬里跳  | 我要投稿

using System;

using System.Collections;

using System.Collections.Generic;

using System.Runtime.InteropServices.WindowsRuntime;

using UnityEngine;


//設(shè)置自動回收的 字典id

public struct PoolID

{

? ? public int id;

? ? public float time;

? ? public PoolID(int id_, float time_)

? ? {

? ? ? ? id = id_;

? ? ? ? time = time_;

? ? }

}


public class ObjPool : MonoBehaviour

{


? ? public static Dictionary<int, Stack<Component>> pool_go = new Dictionary<int, Stack<Component>>();

? ? public static List<Component> returntf = new List<Component>();

? ? public static List<PoolID> returntime = new List<PoolID>();


? ? // Use this for initialization

? ? void Awake()

? ? {

? ? ? ? DontDestroyOnLoad(this);

? ? }


? ? private void Update()

? ? {//需要自動回收的在這里回收

? ? ? ? if (returntf.Count > 0)

? ? ? ? {

? ? ? ? ? ? int c = returntf.Count - 1;//?倒序刪除list

? ? ? ? ? ? for (int i = c; i > -1; i--)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? var pd = returntime[i];

? ? ? ? ? ? ? ? if (pd.time < Time.time)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? Component C = returntf[i];

? ? ? ? ? ? ? ? ? ? pool_go[pd.id].Push(C);

? ? ? ? ? ? ? ? ? ? C.gameObject.SetActive(false);

? ? ? ? ? ? ? ? ? ? returntf.RemoveAt(i);

? ? ? ? ? ? ? ? ? ? returntime.RemoveAt(i);

? ? ? ? ? ? ? ? }


? ? ? ? ? ? }

? ? ? ? }


? ? }

? ? //根據(jù)需要初始化 數(shù)組的大小

? ? public static void SetPoolStartSize<T>(T key, int count) where T : Component

? ? {

? ? ? ? Stack<Component> temp;

? ? ? ? int name = key.GetHashCode();

? ? ? ? if (!pool_go.TryGetValue(name, out temp))

? ? ? ? {

? ? ? ? ? ? temp = new Stack<Component>(count);

? ? ? ? ? ? pool_go.Add(name, temp);

? ? ? ? }


? ? ? ? for (int i = 0; i < count; i++)

? ? ? ? {

? ? ? ? ? ? T go;

? ? ? ? ? ? // ? go = GameObject.Instantiate<T>(prefab).GetComponent<T>();

? ? ? ? ? ? go = Instantiate<T>(key,Vector3.one*99999, Quaternion.identity);

? ? ? ? ? ? go.hideFlags = HideFlags.HideInHierarchy;

? ? ? ? ? ? temp.Push(go);

? ? ? ? ? ? go.gameObject.SetActive(false);

? ? ? ? }


? ? }

? ? public static T GetComponent<T>(T key, Vector3 pos, Quaternion rot) where T : Component

? ? {


? ? ? ? Stack<Component> qc;

? ? ? ? T go;

? ? ? ? int name = key.GetHashCode();

? ? ? ? if (pool_go.TryGetValue(name, out qc))

? ? ? ? {

? ? ? ? ? ? if (qc.Count > 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? go = (T)qc.Pop();

? ? ? ? ? ? ? ? go.transform.SetPositionAndRotation(pos, rot);

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? ? ? go = Instantiate<T>(key, pos, rot);


? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? qc = new Stack<Component>();

? ? ? ? ? ? pool_go.Add(name, qc);

? ? ? ? ? ? go = Instantiate<T>(key, pos, rot);

? ? ? ? ? ? // go.hideFlags = HideFlags.HideInHierarchy;

? ? ? ? }

? ? ? ? go.gameObject.SetActive(true);

? ? ? ? return go;

? ? }


? ? //可以自動回收 ?設(shè)置自動回收的時間

? ? public static T GetComponent<T>(T key, Vector3 pos, Quaternion rot, float t) where T : Component

? ? {


? ? ? ? Stack<Component> qc;

? ? ? ? T go;

? ? ? ? int name = key.GetHashCode();

? ? ? ? if (pool_go.TryGetValue(name, out qc))

? ? ? ? {

? ? ? ? ? ? if (qc.Count > 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? go = (T)qc.Pop();

? ? ? ? ? ? ? ? go.transform.SetPositionAndRotation(pos, rot);

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? ? ? go = Instantiate<T>(key, pos, rot);

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? qc = new Stack<Component>();

? ? ? ? ? ? pool_go.Add(name, qc);

? ? ? ? ? ? go = Instantiate<T>(key, pos, rot);

? ? ? ? ? ? // go.hideFlags = HideFlags.HideInHierarchy;


? ? ? ? }

? ? ? ? returntf.Add(go);

? ? ? ? returntime.Add(new PoolID(name, Time.time + t));

? ? ? ? go.gameObject.SetActive(true);

? ? ? ? return go;

? ? }

// 使用示例

// Transform tf=ObjPool.GetComponent<Transform>(keyTf);

? ? public static T GetComponent<T>(T key) where T : Component

? ? {

? ? ? ? Stack<Component> qc;

? ? ? ? T go;

? ? ? ? int name = key.GetHashCode();

? ? ? ? if (pool_go.TryGetValue(name, out qc))

? ? ? ? {

? ? ? ? ? ? if (qc.Count > 0)

? ? ? ? ? ? ? ? go = (T)qc.Pop();

? ? ? ? ? ? else

? ? ? ? ? ? ? ? go = Instantiate<T>(key);

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? qc = new Stack<Component>();

? ? ? ? ? ? pool_go.Add(name, qc);

? ? ? ? ? ? go = Instantiate<T>(key);

? ? ? ? ? ? // go.hideFlags = HideFlags.HideInHierarchy;

? ? ? ? }

? ? ? ? go.gameObject.SetActive(true);

? ? ? ? return go;

? ? }


? ? public static void ReturnGO<T>(T go, T key, Action act = null) where T : Component

? ? {

? ? ? ? if (act != null)

? ? ? ? ? ? act();

? ? ? ? int name = key.GetHashCode();

? ? ? ? if (!pool_go.ContainsKey(name))

? ? ? ? {

? ? ? ? ? ? pool_go.Add(name, new Stack<Component>());

? ? ? ? }

? ? ? ? pool_go[name].Push(go);

? ? ? ? go.gameObject.SetActive(false);

? ? }


}


unity 自己寫的對象池腳本 分享給大家的評論 (共 條)

分享到微博請遵守國家法律
增城市| 西丰县| 城步| 道孚县| 靖江市| 泊头市| 廊坊市| 鄂尔多斯市| 堆龙德庆县| 京山县| 资兴市| 平阴县| 万盛区| 河池市| 汉川市| 新和县| 内江市| 湖南省| 崇义县| 大足县| 安远县| 随州市| 凤台县| 义乌市| 平潭县| 新干县| 滨海县| 友谊县| 盱眙县| 临安市| 东海县| 玉山县| 长兴县| 宁津县| 托克逊县| 陕西省| 西吉县| 将乐县| 新绛县| 井冈山市| 周口市|