Ejemplo n.º 1
0
        /// <summary>
        /// Use this for adding Undo support for changes that are not serialized by Unity when Unity can't handle undoing the action
        /// </summary>
        /// <param name="targets"> The UnityEngine.Object targets which contain the members which are being modified. </param>
        /// <param name="memberInfo"> The LinkedMemberInfo for the members which are being modified. This cannot be null. </param>
        /// <param name="valuesTo"> The value to which the members will be set. </param>
        /// <param name="menuItemText"> Message describing the undo. </param>
        /// <param name="registerCompleteObjectUndo"> Set to true if full object hierarchy should be serialized for the Undo. This is needed for undoing changes to array size fields to function properly. </param>
        /// <returns> True if should call EditorUtility.SetDirty after value has changed. </returns>
        public static bool RegisterUndoableAction(Object[] targets, [NotNull] LinkedMemberInfo memberInfo, object[] valuesTo, [NotNull] string menuItemText, bool registerCompleteObjectUndo)
        {
            if (disabled)
            {
                                #if DEV_MODE && DEBUG_UNDO
                Debug.Log("RegisterUndoableAction with undoText=" + StringUtils.ToString(menuItemText) + " " + StringUtils.Red("ABORTING..."));
                                #endif
                return(false);
            }

                        #if UNITY_EDITOR
            if (targets.Length > 0 && memberInfo.IsUnitySerialized)
            {
                                #if DEV_MODE && DEBUG_UNDO
                Debug.Log("RegisterUndoableAction with " + StringUtils.Green("IsUnitySerialized") + " field=" + memberInfo + ", unityObject=" + StringUtils.ToString(targets[0].name) + ", valuesTo=" + StringUtils.ToString(valuesTo) + ", undoText=" + StringUtils.ToString(menuItemText) + ", registerCompleteObjectUndo=" + StringUtils.ToColorizedString(registerCompleteObjectUndo) + ", IsUnitySerialized=" + StringUtils.ToColorizedString(true) + ", registeredForUndoThisFrame.Contains(targets[0])=" + StringUtils.ToColorizedString(registeredForUndoThisFrame.Contains(targets[0])), targets[0]);
                                #endif

                Instance().DoRegisterUndoableActionInternal(targets, menuItemText, registerCompleteObjectUndo);
                return(true);
            }
                        #endif

                        #if DEV_MODE && DEBUG_UNDO
            Debug.Log(StringUtils.ToColorizedString("RegisterUndoableAction with IsUnitySerialized=", memberInfo.IsUnitySerialized, " memberInfo=", memberInfo.ToString(), ", targets=", StringUtils.ToString(targets), ", valuesTo=", StringUtils.ToString(valuesTo), ", menuItemText=" + StringUtils.ToString(menuItemText), ", registerCompleteObjectUndo=", registerCompleteObjectUndo, ", registeredForUndoThisFrame.Contains(targets[0])=", (targets.Length == 0 ? "n/a" : StringUtils.ToColorizedString(registeredForUndoThisFrame.Contains(targets[0])))));
                        #endif

            Instance().DoRegisterUndoableActionInternal(instance, menuItemText);             //register full undo or not?
            UndoableModification modification;
            try
            {
                modification = UndoableModification.Create(memberInfo, valuesTo);
            }
                        #if DEV_MODE
            catch (SerializationException e)
            {
                Debug.LogError(e);
                        #else
            catch (SerializationException)
            {
                        #endif
                return(false);
            }

            instance.AddNewUndoableModification(modification);

                        #if SET_DIRTY_WHEN_NON_SERIALIZED_FIELDS_MODIFIED
            return(true);
                        #else
            return(false);
                        #endif
        }