Search Unity

Reference asset in UXML

Discussion in 'UI Toolkit' started by BinaryCats, Jan 8, 2020.

  1. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Hi,

    I have a custom visual element, which has a custom attribute. This works fine.

    I wish to reference an asset (in this case a scriptable object) to pull data from, for example set the label to some text stored in the scriptableObject.
    Is it possible to reference an asset from UXML? If not I think it would be neat :)

    I could put the asset path/asset name as the value, and load the asset, but this isn't fool proof as paths and names change

    Thanks in advance
     
    GuillaumeLeplang likes this.
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    You can't directly refer to and instance of the Scriptable object directly from uxml for now. You can however, set the binding-path attribute of your label to the name of the serialized property you want to display, then in c# bind the scriptable object to the cloned uxml hierarchy:

    myUxmlElements.Bind(new SerializedObject(myAsset));

    Hope that helps
     
  3. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Thanks for your reply, I hope that referencing assets in UXML can be considered as a feature request then :)

    Unfortunately binding does not suit my needs, if I had access to myAsset I could (as per my example) set the label text directly, I do not need it to update or any of that jazz.