Skip to content

Commit

Permalink
better default value for first list item when using the little plus b…
Browse files Browse the repository at this point in the history
…utton
  • Loading branch information
glabute committed Jun 3, 2024
1 parent fa6c6ad commit aa2b2c3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,22 @@ public override VisualElement CreateInspectorGUI()

ux.AddHeader("Data Points");
var listField = ux.AddChild(SplineDataInspectorUtility.CreateDataListField(
splineData.Targets, targetsProp, () => splineData.GetGetSplineAndDolly(out var spline, out _) ? spline : null));
splineData.Targets, targetsProp,
() => splineData.GetGetSplineAndDolly(out var spline, out _) ? spline : null,
() =>
{
// Create a default item for index 0
var item = new CinemachineSplineDollyLookAtTargets.Item();
item.LookAt = splineData.VirtualCamera.LookAt;
if (item.LookAt == null)
{
// No LookAt? Find a point to look at near the spline
dolly.SplineSettings.GetCachedSpline().EvaluateSplineWithRoll(
spline.transform, 0, Quaternion.identity, null, out var pos, out var rot);
item.WorldLookAt = pos + rot * Vector3.right * 3;
}
return item;
}));

var arrayProp = targetsProp.FindPropertyRelative("m_DataPoints");
listField.OnInitialGeometry(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Unity.Cinemachine.Editor
static class SplineDataInspectorUtility
{
public delegate ISplineContainer GetSplineDelegate();
public delegate T GetDefaultValue<T>();

public static VisualElement CreatePathUnitField(SerializedProperty splineDataProp, GetSplineDelegate getSpline)
{
Expand Down Expand Up @@ -61,7 +62,8 @@ static void ConvertPathUnit(
public static PropertyField CreateDataListField<T>(
SplineData<T> splineData,
SerializedProperty splineDataProp,
GetSplineDelegate getSpline)
GetSplineDelegate getSpline,
GetDefaultValue<T> getDefaultValue = null)
{
var sortMethod = splineData.GetType().GetMethod("ForceSort", BindingFlags.Instance | BindingFlags.NonPublic);
var setDataPointMethod = splineData.GetType().GetMethod("SetDataPoint", BindingFlags.Instance | BindingFlags.Public);
Expand All @@ -85,7 +87,8 @@ public static PropertyField CreateDataListField<T>(
{
if (arrayProp.arraySize == 1)
{
setDataPointMethod.Invoke(splineData, new object[] { 0, new DataPoint<T> () { Value = splineData.DefaultValue } });
T value = getDefaultValue != null ? getDefaultValue() : splineData.DefaultValue;
setDataPointMethod.Invoke(splineData, new object[] { 0, new DataPoint<T> () { Value = value } });
arrayProp.serializedObject.Update();
}
};
Expand Down

0 comments on commit aa2b2c3

Please sign in to comment.