Below is a MonoBehaviour script that allows you to select an addressable asset of a specific type, load it, and replace a specified Transform object while preserving its position, rotation, and scale. You can choose when to call LoadResource(), either at startup or manually.
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class AddressableResourceLoader : MonoBehaviour
{
[Header("Addressable Asset Settings")]
[SerializeField] private AssetReference assetReference; // Reference to the addressable asset
[SerializeField] private Transform targetTransform; // Transform to replace with the loaded asset
[Header("Load Settings")]
[SerializeField] private bool loadOnStart = true; // Load asset automatically on Start
private AsyncOperationHandle<GameObject> loadOperationHandle;
private void Start()
{
if (loadOnStart)
{
LoadResource();
}
}
/// <summary>
/// Loads the addressable asset and replaces the target Transform object.
/// </summary>
public void LoadResource()
{
if (assetReference == null || targetTransform == null)
{
Debug.LogError("AssetReference or TargetTransform is not assigned!");
return;
}
// Load the addressable asset
loadOperationHandle = Addressables.LoadAssetAsync<GameObject>(assetReference);
loadOperationHandle.Completed += OnAssetLoaded;
}
private void OnAssetLoaded(AsyncOperationHandle<GameObject> obj)
{
if (obj.Status == AsyncOperationStatus.Succeeded)
{
GameObject loadedAsset = obj.Result;
if (loadedAsset != null)
{
// Instantiate the loaded asset
GameObject newObject = Instantiate(loadedAsset, targetTransform.parent, false);
// Copy position, rotation, and scale from the target Transform
newObject.transform.SetPositionAndRotation(targetTransform.position, targetTransform.rotation);
newObject.transform.localScale = targetTransform.localScale;
// Destroy the original target object
Destroy(targetTransform.gameObject);
}
else
{
Debug.LogError("Loaded asset is null!");
}
}
else
{
Debug.LogError("Failed to load asset: " + obj.Status);
}
Addressables.Release(loadOperationHandle);
}
#if UNITY_EDITOR
[ContextMenu("Assign Asset From Selection")]
private void AssignAssetFromSelection()
{
if (UnityEditor.Selection.activeObject is GameObject selectedGameObject)
{
assetReference = new AssetReference(selectedGameObject);
}
else
{
Debug.LogWarning("Selected object is not a GameObject.");
}
}
#endif
}
Features:
- Addressable Asset Selection: Use an
AssetReference to select the addressable asset in the Inspector.
- Target Transform: Specify the
Transform object to be replaced.
- Load On Start: Option to automatically load the asset when the scene starts.
- Manual Loading: Call
LoadResource() manually when needed.
- Transform Preservation: The loaded asset inherits the position, rotation, and scale of the target
Transform.
Usage:
- Attach this script to a GameObject in your scene.
- Assign the addressable asset using the
AssetReference field in the Inspector.
- Assign the
Target Transform to the object you want to replace.
- Toggle
Load On Start to load the asset automatically at startup, or call LoadResource() manually via code.
Notes:
- Ensure the addressable asset is properly set up in the Addressables system.
- The script destroys the original
targetTransform object after replacing it with the loaded asset.
- Error handling is included for missing references or failed asset loads.
Let me know if you need further customization!