Tri-Inspector/Editor/TriPropertyTreeForSerializedObject.cs

81 lines
2.7 KiB
C#
Raw Normal View History

using System;
2022-08-17 13:46:26 -04:00
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEditor;
namespace TriInspector
{
public sealed class TriPropertyTreeForSerializedObject : TriPropertyTree
{
private readonly SerializedObject _serializedObject;
public TriPropertyTreeForSerializedObject([NotNull] SerializedObject serializedObject)
{
_serializedObject = serializedObject ?? throw new ArgumentNullException(nameof(serializedObject));
TargetObjectType = _serializedObject.targetObject.GetType();
TargetsCount = _serializedObject.targetObjects.Length;
TargetIsPersistent = _serializedObject.targetObject is var targetObject &&
2022-12-07 02:38:25 -05:00
targetObject != null && EditorUtility.IsPersistent(targetObject);
2022-06-01 03:35:33 -04:00
RootPropertyDefinition = new TriPropertyDefinition(
memberInfo: null,
2022-08-17 13:46:26 -04:00
ownerType: null,
2022-06-01 03:35:33 -04:00
order: -1,
fieldName: "ROOT",
fieldType: TargetObjectType,
valueGetter: (self, targetIndex) => _serializedObject.targetObjects[targetIndex],
valueSetter: (self, targetIndex, value) => _serializedObject.targetObjects[targetIndex],
2022-08-17 13:46:26 -04:00
attributes: new List<Attribute>(),
2022-06-01 03:35:33 -04:00
isArrayElement: false);
RootProperty = new TriProperty(this, null, RootPropertyDefinition, serializedObject);
2022-06-02 03:18:13 -04:00
RootProperty.ValueChanged += OnPropertyChanged;
RootProperty.ChildValueChanged += OnPropertyChanged;
}
2022-06-01 03:35:33 -04:00
public override void Dispose()
{
2022-06-02 03:18:13 -04:00
RootProperty.ChildValueChanged -= OnPropertyChanged;
RootProperty.ValueChanged -= OnPropertyChanged;
2022-06-01 03:35:33 -04:00
base.Dispose();
}
2022-06-02 03:18:13 -04:00
public override void Update(bool forceUpdate = false)
{
2022-06-02 03:18:13 -04:00
if (forceUpdate)
{
_serializedObject.SetIsDifferentCacheDirty();
_serializedObject.Update();
}
base.Update(forceUpdate);
}
2022-06-02 03:18:13 -04:00
public override bool ApplyChanges()
{
2022-06-02 03:18:13 -04:00
var changed = base.ApplyChanges();
changed |= _serializedObject.ApplyModifiedProperties();
return changed;
}
2022-06-02 03:18:13 -04:00
public override void ForceCreateUndoGroup()
{
2022-06-02 03:18:13 -04:00
Undo.RegisterCompleteObjectUndo(_serializedObject.targetObjects, "Inspector");
Undo.FlushUndoRecordObjects();
}
2022-06-02 03:18:13 -04:00
private void OnPropertyChanged(TriProperty changedProperty)
{
foreach (var targetObject in _serializedObject.targetObjects)
{
EditorUtility.SetDirty(targetObject);
}
RequestValidation();
2022-06-02 03:18:13 -04:00
RequestRepaint();
}
}
}