Search Unity

How to get ExposedName from a Generic Field?

Discussion in 'Timeline' started by zIyaGtVm, Jul 1, 2018.

  1. zIyaGtVm

    zIyaGtVm

    Joined:
    Dec 27, 2017
    Posts:
    131
    I'm trying to resolve fields from ScriptableObject then use them to reset TimelineAsset's bindings.
    The steps are just like the thread below.

    https://forum.unity.com/threads/can-i-use-the-exposedreference-t-type-for-custom-assets.488989/
    Everything works fine,but the problem occurs when I try to make a Generics version.
    I try to resolve any type of my ScriptableObject by using C# Reflection which I'm not good at.
    So here is my question:How can I replace the paremeter[templete.scenePlayerTrans] by casting the field?
    or is there any better way to achieve this?

    Code (CSharp):
    1.  
    2. public void ResovleTemplete()
    3.         {
    4.             // get ScriptableObject's type and get all the fields
    5.             Type templeteType = typeof(KeepWalkingTemplate);
    6.             FieldInfo[] templateFields = templeteType.GetFields(BindingFlags.Public | BindingFlags.Instance);
    7.  
    8.             foreach (FieldInfo field in templateFields)
    9.             {
    10.                 string name = field.Name;
    11.                 object value = field.GetValue(templete);
    12.                 // when field's name match timeline track's
    13.                 foreach (PlayableBinding item in targetAsset.outputs)
    14.                 {
    15.                     if (item.streamName == name)
    16.                     {
    17.                         //                                      I want replace this by casting field's value to ExposedReference<Transform>
    18.                         pd.SetGenericBinding(item.sourceObject, templete.scenePlayerTrans.Resolve(erContext));
    19.                     }
    20.                 }
    21.             }
    22.         }
    23.  
    The exposedName & Fields :
    debug.jpg

    Any advice would be appreciated Thanks!
     
  2. zIyaGtVm

    zIyaGtVm

    Joined:
    Dec 27, 2017
    Posts:
    131
    Still don't know how to cast it.
    Maybe anybody can tell me in which way do you manage hundreds of timelineAsset in different types?

    When you have:
    1. Different types of TimelinesAssets.[e.g. enter room timeline/ open chest timeline]
    2. Each type will be called many times in different situations. [e.g. character A enters room1 / character B enters room2]

    For me, I think :
    1. Build a templete with several slots to store BindingSourceObjects and ExposedReferenceParameters for each timelineAsset. then save to ScriptableObject.
    2. Everytime player trigger a timeline , load timelineAsset with Parameters SO . and Play.

    Do you have a better way to manage timelines? Please tell me.
     
  3. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    129
    What i am doing is using prefabs. Each spawned unit will have their own Playable directors on GameObjects embedded in their prefabs. Any exposed references used on the Timelines are then relative to the prefab. The actual TimelineAsset is shared, but the timeline resolves automatically to game objects within the prefab.

    The downside with that is you're limited to referencing things inside the prefab. To interact with external things you could try and bypass the reference entirely with custom tracks/clips - so on Track or Clip creation (or play/pause) you could search within your scene to grab some resource to use.
     
  4. zIyaGtVm

    zIyaGtVm

    Joined:
    Dec 27, 2017
    Posts:
    131
    Yes I agree. I once thought of All in one Prefabs before, but In my case I can't just simply limit every sourceObject inside prefabs because sometimes my reference object are in multi-scenes.
    That's why I tried this stupid way to save everything's ExposedReference I need into a ScriptableObject Asset.
    Thanks for sharing,tara!