auto removing CustomBlends added at Runtime?

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.

public void RemoveAllBlendsAdded(CinemachineBlenderSettings.CustomBlend[] blendsToRemove)
{
            List<CinemachineBlenderSettings.CustomBlend> newBlendList = new List<CinemachineBlenderSettings.CustomBlend>();
            for (int i=0; i< activeBrain.m_CustomBlends.m_CustomBlends.Length; i++)
            {
                if(!CameraBrainContainsBlend(blendsToRemove, activeBrain.m_CustomBlends.m_CustomBlends[i]))
                {
                    newBlendList.Add(activeBrain.m_CustomBlends.m_CustomBlends[i]);
                }
            }

            activeBrain.m_CustomBlends.m_CustomBlends = newBlendList.ToArray();
}

 public bool CameraBrainContainsBlend(CinemachineBlenderSettings.CustomBlend[] blends, CinemachineBlenderSettings.CustomBlend hasBlend)
        {
            bool blendFound = false;
            for (int k = 0; k < blends.Length && !blendFound; k++)
                blendFound = (blends[k].GetHashCode() == hasBlend.GetHashCode());
                   
            return blendFound;
        }

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.

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?

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?

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.

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.

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