Search Unity

ObjectField masking serializable field

Discussion in 'Scripting' started by Hender, Nov 20, 2019.

  1. Hender

    Hender

    Joined:
    Dec 15, 2015
    Posts:
    9
    Hi guys,

    Is there a way to create a ObjectField and when that field changes, write a field's value?

    For example, I have a List<HitBuff> which is a [System.Serializable] class. HitBuff's have some fields such as chance and Buff. Buff is a [System.Serializable], but because we have List<HitBuff> I do not want to re-create Buff instances every time I want to add one, rather I'd instantiate from a ScriptableObject.

    So my question is, is it possible to create a masking for this Buff variable which would be a BuffBlueprint, and when that changes, I create a new Buff instance?

    In the image below, I would like to have a ScriptableObject input rather than the Buff itself, however, I'd still like to serialize ONLY the Buff.

    1.PNG

    I have fiddled around with property drawers, but it does not seem to be the way to go. As far as I know, there is no way to write Inspector for Serializable objects, am I correct? That would however solve my problem.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    I'm not sure I understand what it is you are after. Am I getting this right:

    You want to have an Object field into which a ScriptableObject asset can be dragged, and when this happens, some data should get copied from that asset into another field (inside the list).

    If further changes are made to that field, it should not affect the ScriptableObject asset, but should get serialized along with the fields.

    Could you use OnValidate to detect when something was dragged onto one of the Object fields, and if so, then copy over the Buff data? Something like this:

    Code (CSharp):
    1. public class HitBuffListContainer : MonoBehaviour
    2. {
    3.     public List<HitBuff> hitBuffs;
    4.  
    5.     private void OnValidate()
    6.     {
    7.         foreach(var hitBuff in hitBuffs)
    8.         {
    9.             hitBuff.OnValidate();
    10.         }
    11.     }
    12. }
    13.  
    14. public class HitBuff
    15. {
    16.     public float chance;
    17.     public BuffBlueprint applyBuffBlueprint;
    18.     public Buff buff;
    19.  
    20.     public void OnValidate()
    21.     {
    22.         if(applyBuffBlueprint != null)
    23.         {
    24.             buff = applyBuffBlueprint.buff.Copy();
    25.         }
    26.     }
    27. }