Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRTPostEffectorEditor does not support Undo and Prefab dirtying #2

Open
kadams8670 opened this issue Nov 18, 2023 · 0 comments
Open

Comments

@kadams8670
Copy link

I wanted to experiment with using this for a personal project. However, I noticed some issues with the custom editor implementation and decided to fix them.

The custom editor for CRTPostEffector uses EditorGUILayout functions to directly modify the class properties and doesn't associate them with a SerializedProperty. This means that the editor won't properly register changes for the Undo system or detect Prefab changes.

You can fix this by migrating your EditorGUILayout calls to use SerializedProperties. I'm not going to go to the trouble of making a pull request, but I've posted the revised code.

Note: I implemented the ObjectFieldHelper() function myself to preserve the "allowSceneObjects" boolean. Annoyingly, Unity's EditorGUILayout.PropertyField() function doesn't seem to have an overload for "allowSceneObjects" when using SerializedProperties.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using static CRTPostEffecter;
using static UnityEditor.EditorGUILayout;

[CustomEditor(typeof(CRTPostEffecter))]
public class CRTPostEffecterEditor : Editor
{
    CRTPostEffecter effect;

    void OnEnable()
    {
        effect = (CRTPostEffecter)target;
    }

    public override void OnInspectorGUI()
    {
        using (new EditorGUI.DisabledScope(true))
            EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour((CRTPostEffecter)target), typeof(CRTPostEffecter), false);

        EditorGUI.BeginChangeCheck();

        ObjectFieldHelper(serializedObject.FindProperty(nameof(effect.material)), new GUIContent("Effect Material"), effect.material, typeof(Material), false);

        using (new HorizontalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.whiteNoiseFrequency)), new GUIContent("White Noise Freaquency (x/1000)"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.whiteNoiseLength)), new GUIContent("White Noise Time Left (sec)"));
        }
        using (new VerticalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.screenJumpFrequency)), new GUIContent("Screen Jump Freaquency (x/1000)"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.screenJumpLength)), new GUIContent("Screen Jump Length"));
            using (new HorizontalScope())
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.screenJumpMinLevel)), new GUIContent("min"));
                EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.screenJumpMaxLevel)), new GUIContent("max"));
            }
        }
        using (new HorizontalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isScanline)), new GUIContent("Scanline On / Off"));
        }

        using (new HorizontalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isMonochrome)), new GUIContent("Monochrome On / Off"));
        }

        using (new HorizontalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.flickeringStrength)), new GUIContent("Flickering Strength"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.flickeringCycle)), new GUIContent("Flickering Cycle"));
        }

        using (new VerticalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isSlippage)), new GUIContent("Slippage On / Off"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isSlippageNoise)), new GUIContent("Slippage Noise"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.slippageStrength)), new GUIContent("Slippage Strength"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.slippageInterval)), new GUIContent("Slippage Interval"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.slippageScrollSpeed)), new GUIContent("Slippage Scroll Speed"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.slippageSize)), new GUIContent("Slippage Size"));
        }

        using (new VerticalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isChromaticAberration)), new GUIContent("Chromatic Aberration On / Off"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.chromaticAberrationStrength)), new GUIContent("Chromatic Aberration Strength"));
        }

        using (new VerticalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isMultipleGhost)), new GUIContent("Multiple Ghost On / Off"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.multipleGhostStrength)), new GUIContent("Multiple Ghost Strength"));
        }

        using (new VerticalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isLetterBox)), new GUIContent("Letter Box On / Off"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.letterBoxType)), new GUIContent("Letter Box Type"));
        }

        using (new VerticalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isDecalTex)), new GUIContent("Decal Tex On / Off"));
            ObjectFieldHelper(serializedObject.FindProperty(nameof(effect.decalTex)), new GUIContent("Decal Tex"), effect.decalTex, typeof(Texture2D), false);
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.decalTexPos)), new GUIContent("Decal Tex Position"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.decalTexScale)), new GUIContent("Decal Tex Scale"));
        }

        using (new VerticalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isLowResolution)), new GUIContent("Low Resolution"));
            //effect.resolutions = EditorGUILayout.Vector2IntField("Resolutions", effect.resolutions);
            //EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.resolutions)), new GUIContent("Resolutions"));
        }

        using (new VerticalScope(GUI.skin.box))
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(effect.isFilmDirt)), new GUIContent("Film Dirt"));
            ObjectFieldHelper(serializedObject.FindProperty(nameof(effect.filmDirtTex)), new GUIContent("Film Dirt Tex"), effect.filmDirtTex, typeof(Texture2D), false);
        }

        if (EditorGUI.EndChangeCheck())
        {
            serializedObject.ApplyModifiedProperties();
        }
    }


    static Object ObjectFieldHelper(SerializedProperty serializedProperty, GUIContent label, Object obj, System.Type objType, bool allowSceneObjects, bool applyModifiedProperties = false)
    {
        Rect position = GetControlRect();
        GUIContent finalLabel = EditorGUI.BeginProperty(position, label, serializedProperty);
        EditorGUI.BeginChangeCheck();
        Object tempObject = EditorGUI.ObjectField(position, finalLabel, obj, objType, allowSceneObjects);
        if (EditorGUI.EndChangeCheck())
        {
            serializedProperty.objectReferenceValue = tempObject;
            if (applyModifiedProperties)
            {
                serializedProperty.serializedObject.ApplyModifiedProperties();
            }
        }
        EditorGUI.EndProperty();
        return serializedProperty.objectReferenceValue;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant