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 Cubelist = new List();
private static List Spherelist = new List();
private static List CubeTexture2Dlist = new List();
private static List SphereTexture2Dlist = new List();
public GameObject cube;
public GameObject sphere;
[MenuItem("testaa/CreateCube")]
public static void CreateCube()
{
Addressables.InstantiateAsync("Cube").Completed += delegate(AsyncOperationHandle handle)
{
Cubelist.Add(handle.Result);
};
}
[MenuItem("testaa/CreateSphere")]
public static void CreateSphere()
{
Addressables.InstantiateAsync("Sphere").Completed += delegate(AsyncOperationHandle handle)
{
Spherelist.Add(handle.Result);
};
}
[MenuItem("testaa/CreateCubeTex")]
public static void CreateCubeTex()
{
GameObject cube = null;
Addressables.InstantiateAsync("Cube").Completed += delegate(AsyncOperationHandle handle)
{
cube = handle.Result;
Cubelist.Add(handle.Result);
Addressables.LoadAssetAsync("CubeTex").Completed += delegate(AsyncOperationHandle tex)
{
CubeTexture2Dlist.Add(tex.Result);
var material= cube.GetComponent();
material.material.mainTexture = (Texture2D)tex.Result;
};
};
}
[MenuItem("testaa/CreateSphereex")]
public static void CreateSphereTex()
{
GameObject cube = null;
Addressables.InstantiateAsync("Sphere").Completed += delegate(AsyncOperationHandle handle)
{
cube = handle.Result;
Spherelist.Add(handle.Result);
Addressables.LoadAssetAsync("SphereTex").Completed += delegate(AsyncOperationHandle tex)
{
SphereTexture2Dlist.Add(tex.Result);
var material= cube.GetComponent();
material.material.mainTexture = (Texture2D)tex.Result;
};
};
}
[MenuItem("testaa/CreateCubeTex2SceneGo")]
public static void CreateCubeTex1()
{
Addressables.LoadAssetAsync("CubeTex").Completed += delegate(AsyncOperationHandle tex)
{
CubeTexture2Dlist.Add(tex.Result);
var material= GameObject.Find("cube").GetComponent();
material.material.mainTexture = (Texture2D)tex.Result;
};
}
[MenuItem("testaa/CreateSphereex2SceneGo")]
public static void CreateSphereTex1()
{
Addressables.LoadAssetAsync("SphereTex").Completed += delegate(AsyncOperationHandle tex)
{
SphereTexture2Dlist.Add(tex.Result);
var material= GameObject.Find("sphere").GetComponent();
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();
}
}