Addressable内存释放代码

 Unity Addressable内存管理_凌志·C的博客-CSDN博客_addressables 内存管理
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
 
public class testaa : MonoBehaviour
{
    private static List<GameObject> Cubelist = new List<GameObject>();
    private static List<GameObject> Spherelist = new List<GameObject>();
    private static List<Texture> CubeTexture2Dlist = new List<Texture>();
    private static List<Texture> SphereTexture2Dlist = new List<Texture>();

    public GameObject cube;
    public GameObject sphere;
    
    [MenuItem("testaa/CreateCube")]

    public static void CreateCube()
    {
        Addressables.InstantiateAsync("Cube").Completed += delegate(AsyncOperationHandle<GameObject> handle)
        {  
            Cubelist.Add(handle.Result);
        };
    }
    [MenuItem("testaa/CreateSphere")]

    public static void CreateSphere()
    {
        Addressables.InstantiateAsync("Sphere").Completed += delegate(AsyncOperationHandle<GameObject> handle)
        {  
            Spherelist.Add(handle.Result);
        };
    }
    [MenuItem("testaa/CreateCubeTex")]

    public static void CreateCubeTex()
    {
        GameObject cube = null;
        Addressables.InstantiateAsync("Cube").Completed += delegate(AsyncOperationHandle<GameObject> handle)
        {
            cube = handle.Result;
            Cubelist.Add(handle.Result);
            Addressables.LoadAssetAsync<Texture>("CubeTex").Completed += delegate(AsyncOperationHandle<Texture> tex)
            {
                CubeTexture2Dlist.Add(tex.Result);

                var material= cube.GetComponent<MeshRenderer>();
                material.material.mainTexture = (Texture2D)tex.Result;
            };
        };

    }
    
    [MenuItem("testaa/CreateSphereex")]

    public static void CreateSphereTex()
    {
        GameObject cube = null;
        Addressables.InstantiateAsync("Sphere").Completed += delegate(AsyncOperationHandle<GameObject> handle)
        {
            cube = handle.Result;
            Spherelist.Add(handle.Result);
            Addressables.LoadAssetAsync<Texture>("SphereTex").Completed += delegate(AsyncOperationHandle<Texture> tex)
            {
                SphereTexture2Dlist.Add(tex.Result);
                var material= cube.GetComponent<MeshRenderer>();
                material.material.mainTexture = (Texture2D)tex.Result;
            };
        };

    }
    [MenuItem("testaa/CreateCubeTex2SceneGo")]

    public static void CreateCubeTex1()
    {
        
        Addressables.LoadAssetAsync<Texture>("CubeTex").Completed += delegate(AsyncOperationHandle<Texture> tex)
        {
            CubeTexture2Dlist.Add(tex.Result);

            var material= GameObject.Find("cube").GetComponent<MeshRenderer>();
            material.material.mainTexture = (Texture2D)tex.Result;
        };

    }
    
    [MenuItem("testaa/CreateSphereex2SceneGo")]

    public static void CreateSphereTex1()
    {
        Addressables.LoadAssetAsync<Texture>("SphereTex").Completed += delegate(AsyncOperationHandle<Texture> tex)
        {
            SphereTexture2Dlist.Add(tex.Result);
            var material=  GameObject.Find("sphere").GetComponent<MeshRenderer>();
            material.material.mainTexture = (Texture2D)tex.Result;
        };
    }
    [MenuItem("testaa/ClearCube")]

    public static void ClearCube()
    {
        for (int i = 0; i < Cubelist.Count; i++)
        {
            Addressables.Release(Cubelist[i]);
        }
        Cubelist.Clear();

        if ( GameObject.Find("cube"))
        {
            GameObject.DestroyImmediate( GameObject.Find("cube"));
        }
    }
    [MenuItem("testaa/ClearSphere")]
    public static void ClearSphere()
    {
        for (int i = 0; i < Spherelist.Count; i++)
        {
            Addressables.Release(Spherelist[i]);
        }
        Spherelist.Clear();
        if ( GameObject.Find("sphere"))
        {
            GameObject.DestroyImmediate( GameObject.Find("sphere"));
        }
    }
    [MenuItem("testaa/ClearCubeTexture2D")]
    public static void ClearTexture2D()
    {
        for (int i = 0; i < CubeTexture2Dlist.Count; i++)
        {
            Addressables.Release(CubeTexture2Dlist[i]);
        }
        CubeTexture2Dlist.Clear();
    }
    
    [MenuItem("testaa/ClearSphereTexture2Dlist")]
    public static void ClearSphereTexture2Dlist()
    {
        for (int i = 0; i < SphereTexture2Dlist.Count; i++)
        {
            Addressables.Release(SphereTexture2Dlist[i]);
        }
        SphereTexture2Dlist.Clear();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值