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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值