Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question AssetReference and SerializedProperty

Discussion in 'Addressables' started by RubberBandGames, Aug 25, 2023.

  1. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    Hello,

    Is there a way to get an AssetReference from a SerializedProperty so that we can use it for a PropertyDrawer?

    Thanks,
    Tom
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    The asset reference itself is just a wrapper around a few different serialised fields, all strings I think. The only way to easily get it from the serialised property is with the
    .boxedValue
    property which is only available in 2022+ I believe.

    Otherwise you care more about the properties within the class' properties.
     
  3. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    We are using Unity 2020.3.44f1, is there any alternatives?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    What's your actual end goal here? If you want to draw it, just provide the serialised property to a PropertyField.

    Or do you want to modify the internal data for it? In that case you can just work through it's child properties.
     
  5. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    We just want to be able to get and change the assetreference.

    For example we have a script called ClothingPiece which has access to a Sprite. We want to get this Sprite so we can use it to draw an icon in the CustomPropertyDrawer for the Editor.

    This is currently how we get it using CustomPropertyDrawer.

    Code (CSharp):
    1.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    2.     {
    3.         EditorGUI.BeginProperty(position, label, property);
    4.  
    5.         const float ICONHEIGHT = 50;
    6.         const float OFFSET = 20;
    7.  
    8.         Sprite sprite = null;
    9.  
    10.         overrideHeight = 0;
    11.  
    12.         Rect overrideRect = position;
    13.  
    14.         ClothingPiece clothingPiece = property.objectReferenceValue as ClothingPiece;
    15.         if (clothingPiece)
    16.         {
    17.             sprite = clothingPiece.GetIconSprite();
    18.         }
    19.  
    20.         // Rest of code
    21.  
    22.        // Evenutally we will want to set it to another ClothingPiece which we do like so
    23.  
    24.        property.objectReferenceValue = someNewClothingPiece;
    25. }
    But we cannot use property.objectReferenceValue to get or set an AssetReference as far as we can see.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    Well like I said, you can still just access the internal strings by getting the child properties of the asset reference serialised property.

    Or, for example, if this is for a drawer for an object the asset reference is contained inside of, just expose a C# property and acces is via said object, like you are with
    clothingPiece
    here.
     
  7. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    I'm not sure I completely understand, do you by chance have an example?

    I know I could use a scriptableobject to get around this but that wouldn't work in our project
     
  8. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    I don't get your confusion. If you have an editor for a scriptable object, and that scriptable object has an asset reference, just exposed the reference via a C# property, then you can get access to it via the
    Editor.target
    .
     
  10. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    Because Editor.target is not avaliable for our version of the Editor which is 2020.3.44f1.
    All we have access to is SerializedProperty and anything that the PropertyDrawer inherits.

    Aswell, we are not using a scriptableobject. I mentioned that being a solution but that would not be ideal for us as we would need to create a scriptableobject for every piece of clothing in our game.

    Instead we just want to be able to access a assetreference from a SerializedProperty/PropertyDrawer, this is what our SerializeField looks like.
    Code (CSharp):
    1. [SerializeField] [ClothingPieceMenu(ClothingSelectionType.Hat)] private ClothingHat[] clothingHats
    It doesn't look like it is possible. Which is a shame.
     
  11. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165