最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Unity_Addressable_Loading Addressable assets(加載資源)

2023-04-07 08:44 作者:unity_某某師_高錦錦  | 我要投稿

The?Addressables?class provides several methods for loading Addressable assets. You can load assets one at a time or in batches. To identify the assets to load, you pass either a single key or a list of keys to the loading function. A key can be one of the following objects:

譯:Addressables類提供了幾種用于加載Addressable資產(chǎn)的方法。您可以逐個加載資產(chǎn)或批量加載。要識別要加載的資產(chǎn),可以將單個鍵或鍵列表傳遞給加載函數(shù)。鍵可以是以下對象之一:

  • Address: a string containing the address you assigned to the asset譯:Address:包含您為資產(chǎn)分配的地址的字符串

  • Label: a string containing a label assigned to one or more assets譯:Label:包含分配給一個或多個資產(chǎn)的標簽的字符串

  • AssetReference object: an instance of?AssetReference譯:AssetReference對象:AssetReference的實例

  • IResourceLocation?instance: an intermediate object that contains information to load an asset and its dependencies.譯:IResourceLocation實例:包含加載資產(chǎn)及其依賴項信息的中間對象。

When you call one of the asset loading functions, the Addressables system begins an asynchronous operation that carries out the following tasks:

譯:調(diào)用其中一個資產(chǎn)加載函數(shù)時,Addressables系統(tǒng)開始異步操作,執(zhí)行以下任務(wù):

  1. Looks up the resource locations for the specified keys (except IResourceLocation keys)譯:查找指定鍵的資源位置(除IResourceLocation鍵之外)

  2. Gathers the list of dependencies譯:收集依賴關(guān)系列表

  3. Downloads any remote AssetBundles that are required譯:下載所需的任何遠程AssetBundle

  4. Loads the AssetBundles into memory譯:?將AssetBundle加載到內(nèi)存中

  5. Sets the?Result?object of the operation to the loaded objects譯:將操作的Result對象設(shè)置為已加載的對象

  6. Updates the?Status?of the operation and calls any?Completed?event listeners譯:更新操作的狀態(tài)并調(diào)用任何已完成的事件偵聽器

If the load operation succeeds, the Status is set to Succeeded and the loaded object or objects can be accessed from the?Result?object.

譯:如果加載操作成功,則狀態(tài)設(shè)置為Succeeded,并且可以從Result對象訪問已加載的對象或?qū)ο蠹?/p>

If an error occurs, the exception is copied to the?OperationException?member of the operation object and the Status is set to Failed. By default, the exception is not thrown as part of the operation. However, you can assign a handler function to the?ResourceManager.ExceptionHandler?property to handle any exceptions. Additionally, you can enable the?Log Runtime Exceptions?option in your Addressable system settings to record errors to the Unity?Console.

譯:如果發(fā)生錯誤,則異常會復(fù)制到操作對象的OperationException成員中,并將狀態(tài)設(shè)置為Failed。默認情況下,異常不作為操作的一部分拋出。但是,可以為ResourceManager.ExceptionHandler屬性分配處理程序函數(shù)以處理任何異常。此外,您可以在Addressable系統(tǒng)設(shè)置中啟用Log Runtime Exceptions選項,以將錯誤記錄到Unity控制臺中。

When you call loading functions that can load multiple Addressable assets, you can specify whether the entire operation should abort if any single load operation fails or whether the operation should load any assets it can. In both cases, the operation status is set to failed. (Set the?releaseDependenciesOnFailure?parameter to true in the call to the loading function to abort the entire operation on any failure.)

譯:當調(diào)用可以加載多個Addressable資產(chǎn)的加載函數(shù)時,可以指定整個操作是否應(yīng)在任何單個加載操作失敗時中止,或者操作是否應(yīng)加載它可以加載的任何資產(chǎn)。在這兩種情況下,操作狀態(tài)都設(shè)置為失敗。(在調(diào)用加載函數(shù)時將releaseDependenciesOnFailure參數(shù)設(shè)置為true可在任何失敗時中止整個操作。)

See?Operations?for more information about asynchronous operations and writing asynchronous code in Unity scripts.

譯:有關(guān)異步操作和在Unity腳本中編寫異步代碼的更多信息,請參見“操作”章節(jié)

Loading a single asset

Use the?LoadAssetAsync?method to load a single Addressable asset, typically with an address as the key:

譯:使用LoadAssetAsync方法加載單個Addressable資產(chǎn),通常使用地址作為鍵:

NOTE

You can use a label or other type of key when you call?LoadAssetAsync, not just an address. However, if the key resolves to more than one asset, only the first asset found is loaded. For example, if you call this method with a label applied to several assets, Addressables returns whichever one of those assets that happens to be located first.

譯:在調(diào)用LoadAssetAsync時,您可以使用標簽或其他類型的鍵,而不僅僅是地址。但是,如果該鍵解析為多個資產(chǎn),則只會加載找到的第一個資產(chǎn)。例如,如果您使用應(yīng)用于多個資產(chǎn)的標簽調(diào)用此方法,則Addressables會返回最先找到的那個資產(chǎn)

Loading multiple assets

Use the?LoadAssetsAsync?method to load more than one Addressable asset in a single operation. When using this function, you can specify a single key, such as a label, or a list of keys.

譯:使用LoadAssetsAsync方法在單個操作中加載多個Addressable資產(chǎn)。使用此函數(shù)時,可以指定單個鍵,例如標簽,或鍵列表

When you specify multiple keys, you can specify a?merge mode?to determine how the sets of assets matching each key are combined:

譯:當您指定多個鍵時,可以指定合并模式以確定如何組合與每個鍵匹配的資產(chǎn)集

  • Union?: include assets that match any key譯:Union:包括匹配任何鍵的資產(chǎn)

  • Intersection?: include assets that match every key譯:Intersection:包括匹配每個鍵的資產(chǎn)

  • UseFirst: include assets only from the first key that resolves to a valid location譯:UseFirst:僅包括從解析為有效位置的第一個鍵中的資產(chǎn)

You can specify how to handle loading errors with the?releaseDependenciesOnFailure?parameter. If true, then the operation fails if it encounters an error loading any single asset. The operation and any assets that did successfully load are released.

譯:您可以使用releaseDependenciesOnFailure參數(shù)指定如何處理加載錯誤。如果為true,則如果遇到任何單個資產(chǎn)加載錯誤,則操作失敗。操作和任何成功加載的資產(chǎn)都將被釋放

If false, then the operation loads any objects that it can and does not release the operation. In the case of failures, the operation still completes with a status of Failed. In addition, the list of assets returned has null values where the failed assets would otherwise appear.

譯:如果為false,則操作會加載任何可以加載的對象,并且不會釋放操作。在失敗的情況下,操作仍將以失敗狀態(tài)完成。此外,返回的資產(chǎn)列表中的空值是失敗的資產(chǎn)將出現(xiàn)的位置

Set?releaseDependenciesOnFailure?to true when loading a group of assets that must be loaded as a set in order to be used. For example, if you are loading the assets for a game level, it might make sense to fail the operation as a whole rather than load only some of the required assets.

譯:在加載必須作為一組加載的一組資產(chǎn)時,請將releaseDependenciesOnFailure設(shè)置為true。例如,如果您正在加載游戲級別的資產(chǎn),則整個操作失敗可能比僅加載某些所需資產(chǎn)更合理

Correlating loaded assets to their keys

When you load multiple assets in one operation, the order in which individual assets are loaded is not necessarily the same as the order of the keys in the list you pass to the loading function.

譯:當您在一個操作中加載多個資產(chǎn)時,單個資產(chǎn)加載的順序不一定與您傳遞給加載函數(shù)的密鑰列表中的順序相同

If you need to associate an asset in a combined operation with the key used to load it, you can perform the operation in two steps:

譯:如果您需要將組合操作中的資產(chǎn)與用于加載它的鍵相關(guān)聯(lián),則可以分兩步執(zhí)行操作

  1. Load the?IResourceLocation?instances with the list of asset keys.譯:使用資產(chǎn)鍵列表加載IResourceLocation實例

  2. Load the individual assets using their IResourceLocation instances as keys.譯:使用它們的IResourceLocation實例作為鍵加載單個資產(chǎn)

The IResourceLocation object contains the key information so you can, for example, keep a dictionary to correlate the key to an asset. Note that when you call a loading function, such as?LoadAssetsAsync, the operation first looks up the?IResourceLocation?instances that correspond to a key and then uses that to load the asset. When you load an asset using an IResourceLocation, the operation skips the first step. Thus, performing the operation in two steps does not add significant additional work.

譯:IResourceLocation對象包含密鑰信息,因此您可以例如保留字典以將密鑰與資產(chǎn)相關(guān)聯(lián)。請注意,當您調(diào)用加載函數(shù)(例如LoadAssetsAsync)時,操作首先查找與密鑰對應(yīng)的IResourceLocation實例,然后使用它來加載資產(chǎn)。當您使用IResourceLocation加載資產(chǎn)時,操作跳過第一步。因此,分兩步執(zhí)行操作不會增加顯著的額外工作量

The following example loads the assets for a list of keys and inserts them into a dictionary by their address (PrimaryKey). The example first loads the resource locations for the specified keys. When that operation is complete, it loads the asset for each location, using the Completed event to insert the individual operation handles into the dictionary. The operation handles can be used to instantiate the assets, and, when the assets are no longer needed, to release them.

譯:以下示例加載指定鍵的資產(chǎn)并將其按地址(PrimaryKey)插入字典中。示例首先加載指定鍵的資源位置。當該操作完成時,它會為每個位置加載資產(chǎn),并使用Completed事件將單個操作句柄插入字典中。操作句柄可用于實例化資產(chǎn),并在不再需要資產(chǎn)時釋放它們

Note that the loading function creates a group operation with?ResourceManager.CreateGenericGroupOperation. This allows the function to continue after all the loading operations have finished. In this case, the function dispatches a "Ready" event to notify other scripts that the loaded data can be used.

譯:請注意,載入函數(shù)使用ResourceManager.CreateGenericGroupOperation創(chuàng)建了一個組操作。這使得函數(shù)在所有加載操作完成后繼續(xù)執(zhí)行。在這種情況下,函數(shù)會發(fā)送“Ready”事件以通知其他腳本可使用已加載的數(shù)據(jù)

Loading an AssetReference

The?AssetReference?class has its own load method,?LoadAssetAsync.

譯:AssetReference類有自己的load方法,LoadAssetAsync

You can also use the AssetReference object as a key to the?Addressables.LoadAssetAsync?methods. If you need to spawn multiple instances of the asset assigned to an AssetReference, use?Addressables.LoadAssetAsync, which gives you an operation handle that you can use to release each instance.

譯:您還可以將AssetReference對象用作Addressables.LoadAssetAsync方法的關(guān)鍵字。如果您需要生成分配給AssetReference的資產(chǎn)的多個實例,請使用Addressables.LoadAssetAsync,它會為您提供操作句柄,您可以使用該操作句柄釋放每個實例

See?AssetReference?for more information about using AssetReferences.

譯:有關(guān)使用AssetReferences的更多信息,請參見AssetReference

Loading Scenes

Use the?Addressables.LoadSceneAsync?method to load an Addressable Scene asset by address or other Addressable key object.

譯:使用Addressables.LoadSceneAsync方法按地址或其他Addressable鍵對象加載Addressable場景資產(chǎn)

NOTE

Addressables.LoadSceneAsync?uses the Unity Engine?SceneManager.LoadSceneAsync?method internally. API that affects the behaviour of?SceneManager.LoadSceneAsync?will most likely affect?Addressables.LoadSceneAsync?in the same way, for example?Application.backgroundLoadingPriority.

譯:Addressables.LoadSceneAsync在內(nèi)部使用Unity Engine SceneManager.LoadSceneAsync方法。影響SceneManager.LoadSceneAsync行為的API很可能也會以同樣的方式影響Addressables.LoadSceneAsync,例如Application.backgroundLoadingPriority

The remaining parameters of the method correspond to those used with the?SceneManager.LoadSceneAsync?method:

譯:方法的其余參數(shù)對應(yīng)于使用SceneManager.LoadSceneAsync方法使用的參數(shù)

  • loadMode: whether to add the loaded Scene into the current Scene or to unload and replace the current Scene.譯:loadMode:是否將加載的場景添加到當前場景中,還是卸載并替換當前場景

  • loadSceneParameters: includes loadMode in addition to localPhysicsMode, used when loading the Scene to specify whether a 2D and/or 3D physics Scene should be created譯:loadSceneParameters:包括loadMode以及l(fā)ocalPhysicsMode,用于在加載場景時指定是否創(chuàng)建2D和/或3D物理場景

  • activateOnLoad: whether to activate the scene as soon as it finishes loading or to wait until you call the SceneInstance object's?ActivateAsync?method. Corresponds to the?AsyncOperation.allowSceneActivation?option. Defaults to true.譯:activateOnLoad:是否在完成加載后立即激活場景,還是等待您調(diào)用SceneInstance對象的ActivateAsync方法。對應(yīng)于AsyncOperation.allowSceneActivation選項。默認值為true

  • priority: the priority of the AsyncOperation used to load the Scene. Corresponds to the?AsyncOperation.priority?option. Defaults to 100.譯:priority:用于加載場景的AsyncOperation的優(yōu)先級。對應(yīng)于AsyncOperation.priority選項。默認值為100

WARNING

Setting the?activateOnLoad?parameter to false blocks the AsyncOperation queue, including the loading of any other Addressable assets, until you activate the scene. To activate the scene, call the?ActivateAsync?method of the?SceneInstance?returned by?LoadSceneAsync. See?AsyncOperation.allowSceneActivation?for additional information.

譯:注意:將activateOnLoad參數(shù)設(shè)置為false會阻塞AsyncOperation隊列,包括加載其他Addressable資產(chǎn),直到您激活場景。要激活場景,請調(diào)用LoadSceneAsync返回的SceneInstance的ActivateAsync方法。有關(guān)詳細信息,請參見AsyncOperation.allowSceneActivation

The following example loads a scene additively. The Component that loads the Scene, stores the operation handle and uses it to unload and release the Scene when the parent GameObject is destroyed.

譯:下面的示例會逐步加載場景。加載場景的組件會存儲操作句柄,并在父GameObject銷毀時使用它來卸載和釋放場景。

See the?Scene loading project?in the?Addressables-Sample?repository for additional examples.

譯:有關(guān)其他示例,請參見Addressables-Sample存儲庫中的場景加載項目

If you load a Scene with?LoadSceneMode.Single, the Unity runtime unloads the current Scene and calls?Resources.UnloadUnusedAssets. See?Releasing Addressable assets?for more information.

譯:如果您使用LoadSceneMode.Single加載場景,則Unity運行時會卸載當前場景并調(diào)用Resources.UnloadUnusedAssets。有關(guān)詳細信息,請參見發(fā)布Addressable資產(chǎn)

NOTE

In the Editor, you can always load scenes in the current project, even when they are packaged in a remote bundle that is not available and you set the Play Mode Script to?Use Existing Build. The Editor loads the Scene using the asset database.

譯:注意:在編輯器中,即使場景打包在不可用的遠程包中,并將Play Mode Script設(shè)置為Use Existing Build,您仍然可以加載當前項目中的場景。編輯器使用資產(chǎn)數(shù)據(jù)庫加載場景

Loading assets by location

When you load an Addressable asset by address, label, or AssetReference, the Addressables system first looks up the resource locations for the assets and uses these?IResourceLocation?instances to download the required AssetBundles and any dependencies. You can perform the asset load operation in two steps by first getting the IResourceLocation objects with?LoadResourceLocationsAsync?and then using those objects as keys to load or instantiate the assets.

譯:當通過地址、標簽或AssetReference加載Addressable資產(chǎn)時,Addressables系統(tǒng)首先查找資產(chǎn)的資源位置,并使用這些IResourceLocation實例下載所需的AssetBundle和任何依賴項。您可以通過首先使用LoadResourceLocationsAsync獲取IResourceLocation對象,然后使用這些對象作為關(guān)鍵字來加載或?qū)嵗Y產(chǎn),執(zhí)行資產(chǎn)加載操作的兩個步驟

IResourceLocation?objects contain the information needed to load one or more assets.

The?LoadResourceLocationsAsync?method never fails. If it cannot resolve the specified keys to the locations of any assets, it returns an empty list. You can restrict the types of asset locations returned by the function by specifying a specific type in the?type?parameter.

The following example loads locations for all assets labeled with "knight" or "villager":

譯:IResourceLocation對象包含加載一個或多個資產(chǎn)所需的信息。 LoadResourceLocationsAsync方法永遠不會失敗。如果無法將指定的鍵解析為任何資產(chǎn)的位置,則返回空列表。您可以通過在類型參數(shù)中指定特定類型來限制函數(shù)返回的資產(chǎn)位置類型。 以下示例加載所有標記為“knight”或“villager”的資產(chǎn)的位置

Loading locations of subobjects

Locations for SubObjects are generated at runtime to reduce the size of the content catalogs and improve runtime performance. When you call?LoadResourceLocationsAsync?with the key of an asset with subobjects and don't specify a type, then the function generates IResourceLocation instances for all of the subobjects as well as the main object (if applicable). Likewise, if you do not specify which subobject to use for an AssetReference that points to an asset with subobjects, then the system generates IResourceLocations for every subobject.

譯:在運行時生成SubObjects的位置可以減小內(nèi)容目錄的大小并提高運行時性能。當您使用LoadResourceLocationsAsync調(diào)用具有SubObjects的資產(chǎn)的關(guān)鍵字并且未指定類型時,函數(shù)會為所有SubObjects以及主對象(如果適用)生成IResourceLocation實例。同樣,如果您未指定AssetReference指向具有SubObjects的資產(chǎn)的子對象,則系統(tǒng)會為每個子對象生成IResourceLocations

For example, if you load the locations for an FBX asset, with the address, "myFBXObject", you might get locations for three assets: a GameObject, a Mesh, and a Material. If, instead, you specified the type in the address, "myFBXObject[Mesh]", you would only get the Mesh object. You can also specify the type using the?type?parameter of the LoadResourceLocationsAsync function.

譯:例如,如果您加載FBX資產(chǎn)的位置,地址為“myFBXObject”,則可以獲得三個資產(chǎn)的位置:GameObject、Mesh和Material。如果您在地址中指定類型“myFBXObject [Mesh]”,則只會得到Mesh對象。您還可以使用LoadResourceLocationsAsync函數(shù)的類型參數(shù)指定類型


Instantiating objects from Addressables

You can load an asset, such as a Prefab, and then create an instance of it with?Instantiate. You can also load and create an instance of an asset with?Addressables.InstantiateAsync. The primary difference between these two ways of instantiating objects is how the asset reference counts are affected.

譯:您可以加載資產(chǎn)(例如Prefab),然后使用Instantiate創(chuàng)建其實例。您也可以使用Addressables.InstantiateAsync加載并創(chuàng)建資產(chǎn)的實例。這兩種實例化對象方式之間的主要區(qū)別在于如何影響資產(chǎn)引用計數(shù)

When you use InstantiateAsync, the reference counts of the loaded assets are incremented each time you call the method. Thus if you instantiate a Prefab five times, the reference count for the Prefab asset and any of its dependencies are incremented by five. You can then release each instance separately as they are destroyed in the game.

譯:當您使用InstantiateAsync時,每次調(diào)用該方法時,加載的資產(chǎn)的引用計數(shù)都會增加。因此,如果您實例化Prefab五次,則Prefab資產(chǎn)及其所有依賴項的引用計數(shù)都會增加五次。然后,當游戲中的每個實例被銷毀時,您可以單獨釋放每個實例

When you use LoadAssetAsync and Object.Instantiate, then the asset reference counts are only incremented once, for the initial load. If you release the loaded asset (or its operation handle) and the reference count drops to zero, then the asset is unloaded and all the additional instantiated copies lose their subassets as well -- they still exist as GameObjects in the scene, but without Meshes, Materials, or other assets that they depend on.

譯:當您使用LoadAssetAsync和Object.Instantiate時,資產(chǎn)引用計數(shù)僅在初始加載時增加一次。如果您釋放了加載的資產(chǎn)(或其操作句柄)并且引用計數(shù)降至零,則資產(chǎn)將被卸載,所有其他實例化的副本也會失去其子資產(chǎn)--它們?nèi)匀淮嬖谟趫鼍爸械腉ameObject,但沒有Meshes,Materials或其他其依賴的資產(chǎn)

Which scenario is better, depends on how you organize your object code. For example, if you have a single manager object that supplies a pool of Prefab enemies to spawn into a game level, it might be most convenient to release them all at the completion of the level with a single operation handle stored in the manager class. In other situations, you might want to instantiate and release assets individually.

譯:哪種場景更好,取決于您如何組織對象代碼。例如,如果您有一個單獨的管理對象,為游戲級別提供一組Prefab敵人池,則存儲在管理類中的單個操作句柄在完成級別時最方便釋放它們。在其他情況下,您可能需要單獨實例化和釋放資產(chǎn)

The following example calls?InstantiateAsync?to instantiate a Prefab. The example adds a component to the instantiated GameObject that releases the asset when the GameObject is destroyed.

譯:以下示例調(diào)用InstantiateAsync以實例化Prefab。示例為實例化的GameObject添加了一個組件,該組件在銷毀GameObject時釋放資產(chǎn)

When you call?InstantiateAsync?you have the same options as the?Object.Instantiate?method, and also the following additional parameters:

譯:當您調(diào)用InstantiateAsync時,您具有與Object.Instantiate方法相同的選項,以及以下附加參數(shù):

  • instantiationParameters: this parameter takes a?InstantiationParameters?struct that you can use to specify the instantiation options instead of specifying them in every call to the InstantiateAsync call. This can be convenient if you use the same values for multiple instantiations.譯:instantiationParameters:此參數(shù)采用InstantiationParameters結(jié)構(gòu),您可以使用該結(jié)構(gòu)在每次調(diào)用InstantiateAsync時指定實例化選項。如果您對多個實例使用相同的值,則可以使用此選項

  • trackHandle: If true, which is the default, then the Addressables system keeps track of the operation handle for the instantiated instance. This allows you to release the asset with the?Addressables.ReleaseInstance?method. If false, then the operation handle is not tracked for you and you must store a reference to the handle returned by InstantiateAsync in order to release the instance when you destroy it.譯:trackHandle:如果為true(默認值),則Addressables系統(tǒng)跟蹤實例化實例的操作句柄。這允許您使用Addressables.ReleaseInstance方法釋放資產(chǎn)。如果為false,則系統(tǒng)不會為您跟蹤操作句柄,您必須存儲由InstantiateAsync返回的句柄引用,以便在銷毀實例時釋放它

Asynchronous Loading

The Addressables system API is asynchronous and returns an?AsyncOperationHandle?for use with managing operation progress and completion. Addressables is designed to content location agnostic. The content may need to be downloaded first or use other methods that can take a long time. To force synchronous execution, See?Synchronous Addressables?for more information.

譯:Addressables系統(tǒng)API是異步的,并返回AsyncOperationHandle以用于管理操作進度和完成。Addressables旨在內(nèi)容位置無關(guān)。內(nèi)容可能需要首先下載或使用其他需要很長時間的方法。有關(guān)強制同步執(zhí)行的詳細信息,請參見同步Addressables

When loading an asset for the first time, the handle is done after a minimum of one frame. You can wait until the load has completed using different methods as shown below. If the content has already been loaded, execution times may differ between the various asynchronous loading options shown below.

譯:第一次加載資產(chǎn)時,句柄完成至少需要一幀的時間。您可以使用以下不同的方法等待加載完成。如果內(nèi)容已經(jīng)加載,則在下面顯示的各種異步加載選項之間的執(zhí)行時間可能會有所不同

  • Coroutine: Always be delayed at minimum of one frame before execution continues.譯:協(xié)程:始終至少延遲一幀,然后才繼續(xù)執(zhí)行

  • Completed callback: Is a minimum of one frame if the content has not already been loaded, otherwise the callback is invoked in the same frame.譯:完成回調(diào):如果內(nèi)容尚未加載,則至少需要一幀,否則在同一幀中調(diào)用回調(diào)

  • Awaiting?AsyncOperationHandle.Task: Is a minimum of one frame if the content has not already been loaded, otherwise the execution continues in the same frame.譯:等待AsyncOperationHandle.Task:如果內(nèi)容尚未加載,則至少需要一幀,否則在同一幀中繼續(xù)執(zhí)行

Releasing Addressable assets

Because the Addressables system uses reference counting to determine whether an asset is in use, you must release every asset that you load or instantiate when you are done with it. See?Memory Management?for more information.

譯:釋放Addressable資產(chǎn) 由于Addressables系統(tǒng)使用引用計數(shù)來確定資產(chǎn)是否正在使用,因此在使用完每個加載或?qū)嵗馁Y產(chǎn)后,必須釋放它。有關(guān)更多信息,請參見內(nèi)存管理

When you unload a Scene, its AssetBundle is unloaded. This unloads assets associated with the Scene, for example any GameObjects moved from the original Scene to a different Scene.

譯:當您卸載場景時,其AssetBundle也會被卸載。這將卸載與場景相關(guān)的資產(chǎn),例如從原始場景移動到不同場景中的任何GameObject

Note that the Unity runtime automatically calls?UnloadUnusedAssets?when you load a Scene using the?LoadSceneMode.Single?mode. To prevent the Scene and its assets from being unloaded, maintain a reference to the scene load operation handle until it the Scene should be unloaded manually. One way to do this is to use?ResourceManager.Acquire?on the load operation handle. Conventional methods of preserving the assets such as?Object.DontDestroyOnLoad?or?HideFlags.DontUnloadUnusedAsset?will not work.

譯:請注意,當您使用LoadSceneMode.Single模式加載場景時,Unity運行時會自動調(diào)用UnloadUnusedAssets。為防止場景及其資產(chǎn)被卸載,請保留對場景加載操作句柄的引用,直到手動卸載場景。一種方法是在加載操作句柄上使用ResourceManager.Acquire。傳統(tǒng)的保存資產(chǎn)的方法,如Object.DontDestroyOnLoad或HideFlags.DontUnloadUnusedAsset將無法工作。

Individual Addressables and their operation handles that you loaded separately from the Scene are not released. You must call?Resources.UnloadUnusedAssets?or?UnloadAsset?to free these assets. (The exception to this is that any Addressable assets that you instantiated using?Addressables.InstantiateAsync?with?trackHandle?set to true, the default, are automatically released.)

譯:單獨從場景加載的Addressables及其操作句柄不會被釋放。您必須調(diào)用Resources.UnloadUnusedAssets或UnloadAsset來釋放這些資產(chǎn)。(例外情況是使用trackHandle設(shè)置為true的Addressables.InstantiateAsync實例化的任何Addressable資產(chǎn),即默認情況下會自動釋放。)

Using Addressables in a Scene

If a Scene is itself Addressable, you can use Addressable assets in the scene just as you would any assets. You can place Prefabs and other assets in the Scene, assign assets to component properties, and so on. If you use an asset that is not Addressable, that asset becomes an implicit dependency of the Scene and the build system packs it in the same AssetBundle as the Scene when you make a content build. (Addressable assets are packed into their own AssetBundles according to the group they are in.)

譯:如果場景本身是Addressable,則可以像使用任何資產(chǎn)一樣在場景中使用Addressable資產(chǎn)。您可以將Prefabs和其他資產(chǎn)放在場景中,將資產(chǎn)分配給組件屬性等等。如果使用的資產(chǎn)不是Addressable,則該資產(chǎn)成為場景的隱式依賴項,并且在制作內(nèi)容構(gòu)建時,構(gòu)建系統(tǒng)將其打包到與場景相同的AssetBundle中。(Addressable資產(chǎn)按照它們所在的組分別打包到自己的AssetBundles中。)

NOTE

Implicit dependencies used in more than one place can be duplicated in multiple AssetBundles and in the built-in scene data. Use the?Check Duplicate Bundle Dependencies?rule in the Analyze tool to find unwanted duplication of assets.

譯:在多個位置使用的隱式依賴項可能會在多個AssetBundle和內(nèi)置場景數(shù)據(jù)中重復(fù)。使用Analyze工具中的檢查重復(fù)Bundle依賴項規(guī)則來查找不需要的資產(chǎn)重復(fù)

If a Scene is NOT Addressable, then any Addressable assets you add directly to the scene hierarchy become implicit dependencies and Unity includes copies of those assets in the built-in scene data even if they also exist in an Addressable group. The same is true for any assets, such as Materials, assigned to a component on a GameObject in the scene.

譯:如果場景不是Addressable,則直接添加到場景層次結(jié)構(gòu)中的任何Addressable資產(chǎn)都將成為隱式依賴項,即使它們也存在于Addressable組中,Unity也會在內(nèi)置場景數(shù)據(jù)中包含這些資產(chǎn)的副本。對于在場景中的GameObject上分配給組件的任何資產(chǎn),例如材質(zhì),也是如此

In custom component classes, you can use?AssetReference?fields to allow the assignment of Addressable assets in non-Addressable scenes. Otherwise, you can use?addresses?and?labels?to load assets at runtime from a script. Note that you must load an AssetReference in code whether or not the Scene is Addressable.

譯:在自定義組件類中,您可以使用AssetReference字段允許在非Addressable場景中分配Addressable資產(chǎn)。否則,您可以使用地址和標簽從腳本中在運行時加載資產(chǎn)。請注意,無論場景是否是Addressable,您都必須在代碼中加載AssetReference





Unity_Addressable_Loading Addressable assets(加載資源)的評論 (共 條)

分享到微博請遵守國家法律
光山县| 恩施市| 海门市| 科技| 开原市| 娄底市| 永平县| 涟水县| 射洪县| 金沙县| 博客| 绥江县| 兰州市| 柞水县| 宁波市| 绥中县| 新闻| 彩票| 塘沽区| 张家港市| 井冈山市| 崇明县| 儋州市| 兴城市| 彩票| 安庆市| 新丰县| 普安县| 长子县| 青岛市| 中山市| 富阳市| 浑源县| 凤翔县| 扶绥县| 贵南县| 威信县| 明溪县| 黄大仙区| 徐水县| 大化|