Search Unity

Bug A Bug about save to json

Discussion in 'Open Projects' started by DannyYeung, Feb 17, 2023.

  1. DannyYeung

    DannyYeung

    Joined:
    Jan 3, 2019
    Posts:
    3
    In this project, inventory items will be saved to json file in guid.
    Like that:

    Code (CSharp):
    1. public class SerializableScriptableObject : ScriptableObject
    2. {
    3.     [SerializeField, HideInInspector] private string _guid;
    4.     public string Guid => _guid;
    5.  
    6. #if UNITY_EDITOR
    7.     void OnValidate()
    8.     {
    9.         var path = AssetDatabase.GetAssetPath(this);
    10.         _guid = AssetDatabase.AssetPathToGUID(path);
    11.     }
    12. #endif
    13. }
    But when I use "Use existing build" mode in Addressables, all saved items guid would be null.
    Like that:

    Code (CSharp):
    1. {
    2.     "_locationId": "",
    3.     "_itemStacks": [
    4.         {
    5.             "itemGuid": "",
    6.             "amount": 1
    7.         },
    8.         {
    9.             "itemGuid": "",
    10.             "amount": 20
    11.         },
    12.         {
    13.             "itemGuid": "",
    14.             "amount": 20
    15.         },
    16.         {
    17.             "itemGuid": "",
    18.             "amount": 20
    19.         },
    20.         {
    21.             "itemGuid": "",
    22.             "amount": 20
    23.         },
    24.         {
    25.             "itemGuid": "",
    26.             "amount": 1
    27.         }
    28.     ],
    29.     "_finishedQuestlineItemsGUIds": [],
    30.     "_masterVolume": 1.0,
    31.     "_musicVolume": 1.0,
    32.     "_sfxVolume": 1.0,
    33.     "_resolutionsIndex": 11,
    34.     "_antiAliasingIndex": 2,
    35.     "_shadowDistance": 50.0,
    36.     "_isFullscreen": false,
    37.     "_currentLocale": {
    38.         "instanceID": 0
    39.     }
    40. }
    That's interesting, so i remove the [HideInInspect] attribute, and the items guid in in editor runtime would be null.
    Code (CSharp):
    1. [SerializeField] private string _guid;
    nullguid.png

    So, the problem is when building addressables, this code will get a null guid?

    Code (CSharp):
    1. void OnValidate()
    2.     {
    3.         var path = AssetDatabase.GetAssetPath(this);
    4.         _guid = AssetDatabase.AssetPathToGUID(path);
    5.     }

    Can I trust this workflow which save ScriptableObject's guid to json to locate all unique assets?