Files
test/Assets/PerfectWorld/Scripts/World/AddressableObject.cs
T
2026-02-15 23:59:34 +07:00

61 lines
1.9 KiB
C#

using BrewMonster.Scripts;
using Cysharp.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
public class AddressableObject : MonoBehaviour
{
public string assetPath;
public async UniTask LoadAsset()
{
var model = await AddressableManager.Instance.LoadPrefabAsync(assetPath);
if (model != null)
{
model = Instantiate(model);
var modelTransform = model.transform;
modelTransform.SetParent(transform);
modelTransform.localPosition = Vector3.zero;
modelTransform.localRotation = Quaternion.identity;
modelTransform.localScale = Vector3.one;
model.SetActive(true);
}
}
public void UnloadAsset()
{
AddressableManager.Instance.ReleaseAsset(assetPath);
}
#if UNITY_EDITOR
public static string _modelPathPrefixToRemove = "Assets/ModelRenderer/Art/Models";
[ContextMenu("Get Asset Path")]
public void GetAssetPath()
{
var prefabObject = PrefabUtility.GetCorrespondingObjectFromSource(this.gameObject);
if (prefabObject != null)
{
var path = AssetDatabase.GetAssetPath(prefabObject);
//remove the prefix from the path
path = path.Substring(_modelPathPrefixToRemove.Length + 1);
//remove the .prefab suffix
path = path.Substring(0, path.Length - ".prefab".Length);
assetPath = path;
// now delete all the children of this object
while (transform.childCount > 0)
{
DestroyImmediate(transform.GetChild(0).gameObject);
}
//unpack completely the prefab
PrefabUtility.UnpackPrefabInstance(this.gameObject, PrefabUnpackMode.Completely, InteractionMode.UserAction);
}
else
{
Debug.LogError("No prefab object found for this object");
}
}
#endif
}