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);

猜你喜欢

转载自blog.csdn.net/qq_38913715/article/details/130721227