docs.unity.cn
    Show / Hide Table of Contents

    Property InstanceProvider

    InstanceProvider

    The Instance Provider used by the Addressables System.

    Declaration
    public static IInstanceProvider InstanceProvider { get; }
    Property Value
    Type Description
    IInstanceProvider

    The IInstanceProvider object used to create instances of GameObjects.

    Remarks

    Retrieves the interface used by the Addressables Asset System to create instances of Addressable GameObjects.

    Examples

    The example below instantiates a GameObject using ProvideInstance.

            public AssetReferenceGameObject asset; // Identify the asset
        AsyncOperationHandle<GameObject> instHandle;
        AsyncOperationHandle<IList<IResourceLocation>> locHandle;
    
        void UsingInstanceProviderSample()
        {
            locHandle = Addressables.LoadResourceLocationsAsync(asset, typeof(GameObject));
            locHandle.Completed += OnLoadComplete;
        }
    
        void OnLoadComplete(AsyncOperationHandle<IList<IResourceLocation>> handle)
        {
            if (handle.Status == AsyncOperationStatus.Succeeded)
            {
                Debug.Log($"Successfully loaded resource locations");
                foreach (IResourceLocation location in handle.Result)
                {
                    ResourceManager rm = Addressables.ResourceManager;
                    IInstanceProvider provider = Addressables.InstanceProvider;
                    instHandle = rm.ProvideInstance(provider, location, default(InstantiationParameters));
                    instHandle.Completed += OnProvideInstanceComplete;
                }
            }
        }
    
        void OnProvideInstanceComplete(AsyncOperationHandle<GameObject> handle)
        {
            if (handle.Status == AsyncOperationStatus.Succeeded)
            {
                Debug.Log($"Successfully instantiated GameObject named '{handle.Result.name}'");
            }
        }
    
        void ReleaseResources()
        {
            Addressables.Release(locHandle);
            Addressables.Release(instHandle);
        }
    
        // When ready to release the asset, call ReleaseResources().
        // For example during OnDestroy().
        // void OnDestroy()
        // {
        //     ReleaseResources();
        // }
    Back to top Copyright © 2023 Unity Technologies — Terms of use
    Generated by DocFX
    on Monday, June 26, 2023