PhotonTransformViewEditor can lose inspector changes

Options
I was looking at Photon code and occasionally noticed that PhotonTransformViewEditor works with PhotonTransformView's fields directly. With this approach Unity can skip user changes because it will not know about them. It is better to access the fields through serializedObject and call ApplyModifiedProperties(). Or Undo.RecordObject() can be used if required to change fields directly. I suggest to change PhotonTransformViewEditor like this:

// ----------------------------------------------------------------------------
// <copyright file="PhotonTransformViewEditor.cs" company="Exit Games GmbH">
//   PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
// </copyright>
// <summary>
//   This is a custom editor for the TransformView component.
// </summary>
// <author>developer@exitgames.com</author>
// ----------------------------------------------------------------------------


namespace Photon.Pun
{
    using UnityEditor;
    using UnityEngine;


    [CustomEditor(typeof(PhotonTransformView))]
    public class PhotonTransformViewEditor : Editor
    {
        private SerializedProperty m_SynchronizePosition;
        private SerializedProperty m_SynchronizeRotation;
        private SerializedProperty m_SynchronizeScale;

        private bool helpToggle = false;


        private void OnEnable ()
        {
            m_SynchronizePosition = serializedObject.FindProperty("m_SynchronizePosition");
            m_SynchronizeRotation = serializedObject.FindProperty("m_SynchronizeRotation");
            m_SynchronizeScale = serializedObject.FindProperty("m_SynchronizeScale");
        }

        public override void OnInspectorGUI()
        {
            if (Application.isPlaying)
            {
                EditorGUILayout.HelpBox("Editing is disabled in play mode.", MessageType.Info);
                return;
            }

            serializedObject.Update();

            EditorGUILayout.LabelField("Synchronize Options");

            EditorGUI.indentLevel += 2;
            m_SynchronizePosition.boolValue = EditorGUILayout.ToggleLeft(" Position", m_SynchronizePosition.boolValue);
            m_SynchronizeRotation.boolValue = EditorGUILayout.ToggleLeft(" Rotation", m_SynchronizeRotation.boolValue);
            m_SynchronizeScale.boolValue = EditorGUILayout.ToggleLeft(" Scale", m_SynchronizeScale.boolValue);
            EditorGUI.indentLevel -= 2;

            serializedObject.ApplyModifiedProperties();

            this.helpToggle = EditorGUILayout.Foldout(this.helpToggle, "Info");
            if (this.helpToggle)
            {
                EditorGUI.indentLevel += 1;
                EditorGUILayout.HelpBox("The Photon Transform View of PUN 2 is simple by design.\nReplace it with the Photon Transform View Classic if you want the old options.\nThe best solution is a custom IPunObservable implementation.", MessageType.Info, true);
                EditorGUI.indentLevel -= 1;
            }
        }
    }
}

Comments

  • Tobias
    Options
    Thanks for the hint.
    Can you point us to any bug report or such, that describes how / when Unity might lose updates, when we use the properties directly?

    We will likely implement/apply the changes but we always like the background to understand the issues. Overall, using the serialized properties can be quite unwieldy quickly.