Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

auto removing CustomBlends added at Runtime?

Discussion in 'Cinemachine' started by Kev00, Aug 6, 2019.

  1. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    Hi,

    Is there a way to automatically remove CustomBlends added at runtime?

    At the moment, I'm tracking all the CustomBlends I'm adding across various objects and then removing them OnDestroy() as needed via this set of functions.


    Code (CSharp):
    1.  
    2. public void RemoveAllBlendsAdded(CinemachineBlenderSettings.CustomBlend[] blendsToRemove)
    3. {
    4.             List<CinemachineBlenderSettings.CustomBlend> newBlendList = new List<CinemachineBlenderSettings.CustomBlend>();
    5.             for (int i=0; i< activeBrain.m_CustomBlends.m_CustomBlends.Length; i++)
    6.             {
    7.                 if(!CameraBrainContainsBlend(blendsToRemove, activeBrain.m_CustomBlends.m_CustomBlends[i]))
    8.                 {
    9.                     newBlendList.Add(activeBrain.m_CustomBlends.m_CustomBlends[i]);
    10.                 }
    11.             }
    12.  
    13.             activeBrain.m_CustomBlends.m_CustomBlends = newBlendList.ToArray();
    14. }
    15.  
    16.  public bool CameraBrainContainsBlend(CinemachineBlenderSettings.CustomBlend[] blends, CinemachineBlenderSettings.CustomBlend hasBlend)
    17.         {
    18.             bool blendFound = false;
    19.             for (int k = 0; k < blends.Length && !blendFound; k++)
    20.                 blendFound = (blends[k].GetHashCode() == hasBlend.GetHashCode());
    21.                    
    22.             return blendFound;
    23.         }
    24.  
     
    Last edited: Aug 6, 2019
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    CustomBlends is a Unity asset, and was not designed to be modified dynamically at runtime. One way to make it dynamic-ish is to use the fact that its entries are name-based, so any vcam pair with the same names as the from/to fields in the custom blend entry will match that custom blend. Maybe you can have a bunch of standardized entries in the asset, and just name your vcams appropriately to pick up the blends.
     
  3. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    Thank you! I suppose the way I'm doing it now is completely wrong then.

    Each character in my game has a rig that contains many vcams with a set of blends that I add at run-time per spawn.

    I can certainly pre-populate the CustomBlends asset with dozens of place holders. Hopefully, having blends in m_CustomBlends that are not used won't degrade performance. Is there a way to disable a blend at runtime?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Performance won't be affected by having lots of blends. We're not talking thousands, right?

    There is no function to disable a blend definition. Can I ask what your use-case is for that?
     
  5. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    A few hundred at most.

    As for the use case for the "disable" feature it's probably not needed.

    I plan to fill the customblend array with apx 50 blends for each style I need. I was only wondering about the performance hit when there are a large number of blends with the From and To = "(none)". I also thought it would easier and faster to search for a blend that has a disabled flag than to search for "(none)"

    Otherwise, I don't think I will be disabling the blend for any specific camera effect.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    A few hundred is a lot. It will do a linear search when it needs to find a blend, so maybe you need to rethink the strategy. Do you really have that many different blend styles? Can some naming convention be used for the vcams to whittle down the number of blends? Remember: vcams with the same names are equivalent as far as the blend system is concerned.
     
  7. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    hmm... well each of the vcams that I create at runtime has a unique name.

    vcam.name += "_" + vcam.GetInstanceID().ToString();

    Off hand, I don't recall why I did that exactly. Hopefully it was just for debugging purposes. I was probably not aware that the blend system can work with vcams that have the same name.


    Thanks