Unity 官方对象池 随机值 ListPool LineUtility.Simplify

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;

public class Test : MonoBehaviour
{
    private ObjectPool<GameObject> p;
    private GameObject go1;
    private GameObject go2;
    private GameObject go3;
    // Start is called before the first frame update
    void Start()
    {
        p = new ObjectPool<GameObject>(create, onTake, onReturn, onDestroy);
        go1 = p.Get();
        go2 = p.Get();
        go3 = p.Get();
    }

    void onDestroy(GameObject go)
    {
        Debug.Log("onDestroy");
    }

    void onTake(GameObject go)
    {
        go.SetActive(true);
        Debug.Log("onTake");
    }

    void onReturn(GameObject go)
    {
        go.SetActive(false);
        Debug.Log("onReturn");
    }

    GameObject create()
    {
        Debug.Log("create");
        return new GameObject(UnityEngine.Random.value + "");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.A))
        {
            p.Release(go1);
        }

        if (Input.GetKeyUp(KeyCode.D))
        {
            GameObject ggg = new GameObject();
            p.Get(out ggg);
        }
    }
}

非常简单

Unity的随机数是 UnityEngine.Random.value

LineUtility.Simplify 是干啥用的呢?他能把你复杂的线段简化掉 比如下面这个
在这里插入图片描述
如果你让用户手绘图案 可以试试用这个缩减一下

如果你使用 LineUtility.Simplify 必然会设计到 大量的vector3 vector2 等
难道要写一个缓存池吗?

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;

// This example shows how both version of Get could be used to simplify a line of points.
public class Simplify2DLine
{
    public List<Vector2> SimplifyLine(Vector2[] points)
    {
        // This version will only be returned to the pool if we call Release on it.
        var simplifiedPoints = ListPool<Vector2>.Get();

        // Copy the points into a temp list so we can pass them into the Simplify method
        // When the pooled object leaves the scope it will be Disposed and returned to the pool automatically.
        // This version is ideal for working with temporary lists.
        using (var pooledObject = ListPool<Vector2>.Get(out List<Vector2> tempList))
        {
            for (int i = 0; i < points.Length; ++i)
            {
                tempList.Add(points[i]);
            }

            LineUtility.Simplify(tempList, 1.5f, simplifiedPoints);
        }
        return simplifiedPoints;
    }
}

这是一个缓存池
本来需要手动Release的
结果因为用了 using 就自动回收了
还注意到 out List tempList
不需要new

ListPool.Release(l);
释放后 里面的count是0 数据都清了
也可以这样写

using (ListPool<Vector2>.Get(out List<Vector2> l))
{
    l.Add(new Vector2(1, 1));
    l.Add(new Vector2(1, 1));
    l.Add(new Vector2(1, 1));
    l.Add(new Vector2(1, 1));
}

简单的可以用
var instance = UnsafeGenericPool.Get();
UnsafeGenericPool.Release(instance);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中,虽然没有内置的对象池类,但是可以通过自定义脚本来实现对象池的功能。下面是一个简单的示例代码,展示了如何创建一个基本的对象池: ```csharp using System.Collections.Generic; using UnityEngine; public class ObjectPool : MonoBehaviour { public GameObject prefab; // 预制体 public int poolSize; // 对象池大小 private List<GameObject> objectPool; void Start() { objectPool = new List<GameObject>(); for (int i = 0; i < poolSize; i++) { GameObject obj = Instantiate(prefab); obj.SetActive(false); objectPool.Add(obj); } } public GameObject GetObjectFromPool() { for (int i = 0; i < objectPool.Count; i++) { if (!objectPool[i].activeInHierarchy) { objectPool[i].SetActive(true); return objectPool[i]; } } // 如果对象池中没有可用对象,则动态创建一个新对象 GameObject newObj = Instantiate(prefab); objectPool.Add(newObj); return newObj; } public void ReturnObjectToPool(GameObject obj) { obj.SetActive(false); } } ``` 在上述代码中,我们创建了一个名为ObjectPool的脚本。在Start方法中,我们初始化了对象池,根据poolSize的来创建一定数量的对象,并将它们存储在objectPool列表中。 GetObjectFromPool方法用于从对象池中获取一个可用的对象。我们遍历objectPool列表,寻找第一个处于非激活状态的对象,并将其设置为激活状态,然后返回该对象。如果对象池中没有可用对象,则动态创建一个新对象,并将其添加到objectPool列表中。 ReturnObjectToPool方法用于将使用完的对象返回到对象池中。我们将传入的对象设置为非激活状态,以便下次复用。 通过这样的自定义脚本,你可以在Unity中实现对象池的功能,以提高游戏的性能和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值