ForceReserializeAssets
Loads, upgrades, and writes back to disk the specified assets, their.meta files, or both.
Read time 2 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEditor
- Assembly: UnityEditor.CoreModule
ForceReserializeAssets(IEnumerable<string>, ForceReserializeAssetsOptions)
Loads, upgrades, and writes back to disk the specified assets, their .meta files, or both.
public static void ForceReserializeAssets(IEnumerable<string> assetPaths, ForceReserializeAssetsOptions options = ForceReserializeAssetsOptions.ReserializeAssetsAndMetadata)
Parameters
The project-relative paths of the assets to reserialize.
Whether to reserialize the assets, their .meta files, or both. The default value reserializes both.
Remarks
When Unity loads old data from an asset or scene file, it upgrades the data in memory, but doesn't write it back to disk until something changes the object. Use this method to load, upgrade, and write back assets in a controlled way, without changing them by hand. This avoids upgrades getting mixed in with deliberate changes, which helps in projects that use version control.
Only call this method from a direct user action, such as a menu item. Don't call it from a Unity callback such as , because a scene might be changing at that time. If you do, Unity throws an exception.
OnEnableForceReserializeAssets()
Loads, upgrades, and writes back to disk every asset in the project.
public static void ForceReserializeAssets()
Remarks
When Unity loads old data from an asset or scene file, it upgrades the data in memory, but doesn't write it back to disk until something changes the object. Use this method to load, upgrade, and write back assets in a controlled way, without changing them by hand. This avoids upgrades getting mixed in with deliberate changes, which helps in projects that use version control.
Only call this method from a direct user action, such as a menu item. Don't call it from a Unity callback such as , because a scene might be changing at that time. If you do, Unity throws an exception. This overload reserializes every asset in the project. To choose which assets to reserialize, use the overload that takes .
OnEnableassetPathsExamples
using UnityEditor;using UnityEngine;public class AssetDatabaseExamples : MonoBehaviour{ [MenuItem("AssetDatabase/Force Reserialize Assets Example")] static void UpdateGroundMaterials() { for (var i = 0; i < 10; i++) { var matPath = $"Assets/Materials/GroundMaterial{i}.mat"; var mat = (Material)AssetDatabase.LoadMainAssetAtPath(matPath); AssetImporter.GetAtPath(matPath).SetAssetBundleNameAndVariant("GroundBundle", ""); mat.shader = Shader.Find("Standard"); mat.color = Color.white; mat.mainTexture = (Texture)AssetDatabase.LoadMainAssetAtPath($"Assets/Textures/GroundTexture{i}.jpg"); } AssetDatabase.ForceReserializeAssets(); }}