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(); } }
Addressable内存释放代码
最新推荐文章于 2024-08-26 15:49:18 发布