Search Unity

Asset Bundles and export modified serialized prefabs (YAML)

Discussion in 'Asset Bundles' started by Przemyslaw_Zaworski, Apr 19, 2021.

  1. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Hello. I have two Unity projects: one with base game (and a lot of scripts) and one new empty (external) project.
    This empty project will be used by modders, with single editor script loaded from DLL file ("prefab creator") - to prevent access to our source code. This destination prefab in game should have many components, so to set public fields, in external project (where this script doesn't exist) I can prepare and export values to txt config file, share with bundle, then in game, when prefab is loaded, make AddComponent and set field values directly (parse strings from TXT) or using methods from System.Reflection. But recently I though about another approach, to modify directly prefab file. For the simplest example, this prefab in game has following YAML structure:

    upload_2021-4-19_13-23-11.png

    Code (CSharp):
    1. %YAML 1.1
    2. %TAG !u! tag:unity3d.com,2011:
    3. --- !u!1 &4609693597738395880
    4. GameObject:
    5.   m_ObjectHideFlags: 0
    6.   m_CorrespondingSourceObject: {fileID: 0}
    7.   m_PrefabInstance: {fileID: 0}
    8.   m_PrefabAsset: {fileID: 0}
    9.   serializedVersion: 6
    10.   m_Component:
    11.   - component: {fileID: 3639249925125836788}
    12.   - component: {fileID: 261618618606755211}
    13.   m_Layer: 0
    14.   m_Name: GameObject
    15.   m_TagString: Untagged
    16.   m_Icon: {fileID: 0}
    17.   m_NavMeshLayer: 0
    18.   m_StaticEditorFlags: 0
    19.   m_IsActive: 1
    20. --- !u!4 &3639249925125836788
    21. Transform:
    22.   m_ObjectHideFlags: 0
    23.   m_CorrespondingSourceObject: {fileID: 0}
    24.   m_PrefabInstance: {fileID: 0}
    25.   m_PrefabAsset: {fileID: 0}
    26.   m_GameObject: {fileID: 4609693597738395880}
    27.   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
    28.   m_LocalPosition: {x: 0, y: 0, z: 0}
    29.   m_LocalScale: {x: 1, y: 1, z: 1}
    30.   m_Children: []
    31.   m_Father: {fileID: 0}
    32.   m_RootOrder: 0
    33.   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
    34. --- !u!114 &261618618606755211
    35. MonoBehaviour:
    36.   m_ObjectHideFlags: 0
    37.   m_CorrespondingSourceObject: {fileID: 0}
    38.   m_PrefabInstance: {fileID: 0}
    39.   m_PrefabAsset: {fileID: 0}
    40.   m_GameObject: {fileID: 4609693597738395880}
    41.   m_Enabled: 1
    42.   m_EditorHideFlags: 0
    43.   m_Script: {fileID: 11500000, guid: 887124e9eecbe304f96e450e838ae57f, type: 3}
    44.   m_Name:
    45.   m_EditorClassIdentifier:
    46.   Mass: 5
    47.   Size: 20
    In external project , as I said, this script doesn't exist, so there are no MonoBehaviour entries, but I can add them at runtime, with proper fileIDs and it is important, with destination GUI (887124e9eecbe304f96e450e838ae57f).
    After modification, as expected, in external project, component is not valid:
    upload_2021-4-19_13-34-32.png

    but when I copy prefab into game project, component is detected properly.

    Unfortunately, when I export prefab as bundle, component is not detected:
    upload_2021-4-19_13-41-4.png

    Probably when in external project I build bundle, BuildPipeline.BuildAssetBundles doesn't export direct copy of prefab file, but "fix" missing script, so bundle file doesn't contains additional data provided by Prefab Editor. Now I am thinking how to solve this issue, maybe someone has experience with similar problems ?