In unity, what is a serialized resource?

Ok, the following is a detailed explanation of Serialized Assets, including introduction, methods and examples:

introduce

In Unity, serializing assets refers to saving objects and their properties in a Unity scene or prefab to a file on disk so that they can be restored to their original state in the future. Serialized resource files can include scene files (.unity) and prefab files (.prefab), which contain all the information used to create and define objects in a scene or prefab, including their position, rotation, scale, and Any mounted components and their properties.

Serializing assets is one of Unity's core features, allowing developers to easily reuse, share, and modify them without having to manually create and configure each object. In addition, Unity's serialization system also supports version control and custom serialization, allowing developers to better manage and control their projects.

method

In Unity, the method of serializing resources mainly includes the following steps:

1. Create a scene or prefab

In Unity, you can use scenes or prefabs to create and organize objects in your game. When creating a scene or prefab, you can define the appearance and behavior of objects by adjusting their properties in the Inspector panel.

2. Save the scene or prefab

Once you have finished creating and defining your scene or prefab, you can save them as serialized asset files. In Unity, you can save a scene or prefab to a file on disk by selecting the "Save" or "Save As" option in the "File" menu.

3. Load the scene or prefab

To use scenes or prefabs from serialized resource files, they can be loaded into Unity. In Unity, you can load a scene or prefab file from disk by selecting the "Open Scene" or "Open Prefab" option in the "File" menu.

4. Edit the scene or prefab

Once a scene or prefab is loaded, they can be edited using the tools and panels in the Unity editor. Objects, components, and properties can be added, removed, or modified, and their position, rotation, and scale adjusted.

5. Save the scene or prefab

Once you are done editing your scene or prefab, you can save them back into the serialized asset file. In Unity, you can save a scene or prefab back to a file on disk by selecting the "Save" option in the "File" menu.

for example

Here is a simple example showing how to use serialized assets to create and edit a prefab:

1. Create a prefab

In Unity, create a new empty game object by selecting the "GameObject" menu and then the "Create Empty" option. Name this Game Object "Cube".

In the Inspector panel, adjust the Transform component of the "Cube" object, setting its position to (0, 0, 0), its rotation to (0, 0, 0), and its scale to (1, 1 ,1).

Create another new empty game object by selecting the "Cube" object, then the "GameObject" menu, and then the "Create Empty" option. Name this Game Object "Sphere".

In the Inspector panel, adjust the Transform component of the "Sphere" object, setting its position to (0, 1, 0), its rotation to (0, 0, 0), and its scale to (1, 1 ,1).

Drag and drop the "Sphere" object onto the "Cube" object, making it a child of the "Cube".

Create another new empty game object by selecting the "Cube" object, then the "GameObject" menu, and then the "Create Empty" option. Name this Game Object "Cylinder".

In the Inspector panel, adjust the Transform component of the "Cylinder" object, setting its position to (0, -1, 0), its rotation to (0, 0, 0), and its scale to (1, 1, 1).

Drag and drop the "Cylinder" object onto the "Cube" object, making it a child of the "Cube".

Select the "Cube" object, then select the "File" menu, then select the "Save Prefab" option to save it as a prefab file on disk.

2. Edit the prefab

Drag and drop the prefab file into the Unity editor so it will load.

Select the "Cube" object, adjust its Transform component in the Inspector panel, and set its position to (1, 0, 0).

Select the "Sphere" object, adjust its Transform component in the Inspector panel, and set its scale to (2, 2, 2).

Select the "Cylinder" object, adjust its Transform component in the Inspector panel, and set its rotation to (0, 45, 0).

Select the "File" menu, then select the "Save" option to save the prefab back to a file on disk.

3. Using prefabs

Drag and drop the prefab into the scene to instantiate it as a game object.

Select the prefab instance in the scene and view its Transform component in the Inspector panel to ensure the changes have been applied.

When running the game, the prefab instance should appear with the changed position, scale, and rotation.

This is a simple example showing how to use serialized assets to create and edit prefabs and use them in Unity.

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/131222590