Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Default Custom Effect Sorting?

Discussion in 'Image Effects' started by Toxijuice, Jul 26, 2021.

  1. Toxijuice

    Toxijuice

    Joined:
    May 20, 2015
    Posts:
    5
    Hey there!

    I'm trying to figure out if there's a way to set a default sorting for custom effects with the Post-Processing stack.

    Obviously, you can sort the effects manually under "Custom Effect Sorting" on your Post-Process Layer component; however, I can't seem to find any information on if it's possible to set defaults for this order.

    As it is right now, I have a few effects that need to be separate, but work best when placed in a certain order, which Unity does not do by default. This means every time I make a new Post-Process Layer, I have to reorder the effects.

    Generally, this isn't that big of a deal, but I want it to be as easy as possible for users to use these effects in their project.
     
  2. Toxijuice

    Toxijuice

    Joined:
    May 20, 2015
    Posts:
    5
    It's not the perfect solution, but my current stopgap is to add a button to a custom editor that, when clicked, will sort the effects. Running this when the component is created would be more ideal, but I'm unsure of how to do that.

    Here's the code for anyone who wants to do something similar:

    Code (CSharp):
    1. if (GUILayout.Button("Fix Post-processing order")) {
    2.     var layers = FindObjectsOfType<PostProcessLayer>();
    3.     foreach (var layer in layers) {
    4.         List<PostProcessLayer.SerializedBundleRef> aStack;
    5.         bool success = layer.sortedBundles.TryGetValue(
    6.                 PostProcessEvent.AfterStack,
    7.                 out aStack
    8.             );
    9.  
    10.         if (success) {
    11.             PutFXAtEndOfStack(aStack, "Toxijuice/Retro/Derezzer");
    12.             PutFXAtEndOfStack(aStack, "Toxijuice/Retro/CRT Base");
    13.             PutFXAtEndOfStack(aStack, "Toxijuice/Retro/CRT Bloom");
    14.  
    15.             EditorUtility.SetDirty(layer);
    16.         }
    17.     }
    18. }
    Code (CSharp):
    1. void PutFXAtEndOfStack(List<PostProcessLayer.SerializedBundleRef> bundle, string name) {
    2.     for (int i = 0; i < bundle.Count; i++) {
    3.         string item = bundle[i].bundle.attribute.menuItem;
    4.         if (item == name) {
    5.             var fx = bundle[i];
    6.             bundle.RemoveAt(i);
    7.             bundle.Add(fx);
    8.             return;
    9.         }
    10.     }
    11. }
     
    Lobolopez likes this.