Skip to content

Commit

Permalink
Feature/v0.3.3 (#19)
Browse files Browse the repository at this point in the history
* Int parameter editor

* Int transitions: Runtime

* Very basic int transition test

* Generalize Parameter setting

* Create StateMachineParameterRef

* Write a bunch of tests for int transitions

* Write bool transition tests

* Delete redundant code on AnimationStateMachineSmartBlobberSysetm

* Minor change to AnimationParameterPropertyDrawer

* Enum Parameter

* Fix enum selector position

* Fix bug with state Index in UpdateStateMachineJob.CreateState

* Fix StateMachineSystem not running during AnimationState transitions

* Fix broken tests

* Update samples

* Bump to version v0.3.3

* AnimationParameterAsset Hash Fixes #18

* Update changelog
  • Loading branch information
gabrieldechichi authored Oct 17, 2022
1 parent c316d1e commit a3a4264
Show file tree
Hide file tree
Showing 132 changed files with 7,152 additions and 686 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic
Versioning](http://semver.org/spec/v2.0.0.html).

## [0.3.3] – 2022-10-16

### Added
- Int Transitions
- Enum Transitions

### Fixed
- Fix AnimationParameterAsset hash calculation when using strings
- Fixed bug where StateMachine would not update during AnimationState transition
- Fix bug where LinearBlend and SingleStates would reference wrong AnimationState in StateMachine (fix at UpdateStateMachineJob.CreateState)

## [0.3.2] – 2022-9-18

### Added
Expand Down
6 changes: 5 additions & 1 deletion Editor/EditorPreview/PlayableGraphPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ public void DrawPreview(Rect r, GUIStyle background)
}
}

private void HandleCamera(Boolean force = false)
private void HandleCamera(bool force = false)
{
if (Event.current == null)
{
return;
}
// must set hotControl or MouseUp event will not be detected outside window
int controlId = GUIUtility.GetControlID(FocusType.Passive);
if (Event.current.GetTypeForControl(controlId) == EventType.MouseDown)
Expand Down
2 changes: 2 additions & 0 deletions Editor/EditorWindows/ParametersInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public override void OnInspectorGUI()
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Boolean"), false, CreateParameter<BoolParameterAsset>);
menu.AddItem(new GUIContent("Integer"), false, CreateParameter<IntParameterAsset>);
menu.AddItem(new GUIContent("Float"), false, CreateParameter<FloatParameterAsset>);
menu.AddItem(new GUIContent("Enum"), false, CreateParameter<EnumParameterAsset>);
menu.DropDown(rect);
}
}
Expand Down
79 changes: 49 additions & 30 deletions Editor/PropertyDrawers/AnimationParameterPropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using DMotion.Authoring;
using UnityEditor;
using UnityEngine;
Expand All @@ -8,51 +9,69 @@ namespace DMotion.Editor
[CustomPropertyDrawer(typeof(AnimationParameterAsset))]
internal class AnimationParameterPropertyDrawer : PropertyDrawer
{
private EnumTypePopupSelector enumTypePopupSelector;

public AnimationParameterPropertyDrawer()
{
enumTypePopupSelector = new EnumTypePopupSelector();
}

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var parameterAsset = property.objectReferenceValue as AnimationParameterAsset;
if (parameterAsset != null)
{
using (var c = new EditorGUI.ChangeCheckScope())
{
var labelRect = position;
labelRect.width = EditorGUIUtility.labelWidth;
parameterAsset.name = EditorGUI.TextField(labelRect, parameterAsset.name);
var labelWidth = EditorGUIUtility.labelWidth;
var deleteButtonWidth = EditorGUIUtility.singleLineHeight;
var typeWidth = position.width - labelWidth - deleteButtonWidth;
var rects = position.HorizontalLayout(labelWidth, typeWidth, deleteButtonWidth).ToArray();

if (c.changed)
//label
{
EditorUtility.SetDirty(parameterAsset);
var newName = EditorGUI.DelayedTextField(rects[0], parameterAsset.name);

if (newName != parameterAsset.name)
{
parameterAsset.name = newName;
EditorUtility.SetDirty(parameterAsset);
AssetDatabase.SaveAssetIfDirty(parameterAsset);
AssetDatabase.Refresh();
}
}

var deleteButtonRect = position;
deleteButtonRect.xMin = deleteButtonRect.xMax - EditorGUIUtility.singleLineHeight;
if (GUI.Button(deleteButtonRect, "-"))

//type
{
var stateMachine = property.serializedObject.targetObject as StateMachineAsset;
stateMachine.DeleteParameter(parameterAsset);
property.serializedObject.ApplyModifiedProperties();
property.serializedObject.Update();
if (parameterAsset is EnumParameterAsset enumParameterAsset)
{
enumTypePopupSelector.DrawSelectionPopup(rects[1],
GUIContent.none,
enumParameterAsset.EnumType.Type,
newType =>
{
enumParameterAsset.EnumType.Type = newType;
EditorUtility.SetDirty(enumParameterAsset);
});
}
else
{
EditorGUI.LabelField(rects[1], $"({parameterAsset.ParameterTypeName})");
}
}

var typeRect = position;
typeRect.xMax -= deleteButtonRect.width - EditorGUIUtility.standardVerticalSpacing;
typeRect.xMin += labelRect.width + EditorGUIUtility.standardVerticalSpacing*3;
EditorGUI.LabelField(typeRect, $"({GetParameterTypeName(parameterAsset)})");
//delete
{
if (GUI.Button(rects[2], "-"))
{
var stateMachine = property.serializedObject.targetObject as StateMachineAsset;
stateMachine.DeleteParameter(parameterAsset);
property.serializedObject.ApplyModifiedProperties();
property.serializedObject.Update();
}
}
}
}
}

private string GetParameterTypeName(AnimationParameterAsset parameterAsset)
{
switch (parameterAsset)
{
case BoolParameterAsset _:
return "Boolean";
case FloatParameterAsset _:
return "Float";
default:
throw new ArgumentOutOfRangeException(nameof(parameterAsset));
}
}
}
}
37 changes: 37 additions & 0 deletions Editor/PropertyDrawers/EditorGUIUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using UnityEngine;
using System;
using UnityEditor;

namespace DMotion.Editor
{
public static class EditorGUIUtils
{
public static int GenericEnumPopup(Rect r, Type enumType, int current)
{
if (enumType is { IsEnum: true })
{
var enumValue = (Enum) Enum.GetValues(enumType).GetValue(current);
return (int) (object)EditorGUI.EnumPopup(r, enumValue);
}
else
{
EditorGUI.LabelField(r, "Invalid type");
return -1;
}
}

// ScriptAttributeUtility.GetFieldInfoFromProperty(property, out type);
// if (type != (System.Type) null && type.IsEnum)
// {
// EditorGUI.BeginChangeCheck();
// int num = EditorGUI.EnumNamesCache.IsEnumTypeUsingFlagsAttribute(type) ? EditorGUI.EnumFlagsField(position, label, property.intValue, type, false, EditorStyles.popup) : EditorGUI.EnumPopupInternal(position, label, property.intValue, type, (Func<Enum, bool>) null, false, EditorStyles.popup);
// if (!EditorGUI.EndChangeCheck())
// return;
// System.Type enumUnderlyingType = type.GetEnumUnderlyingType();
// if (num < 0 && (enumUnderlyingType == typeof (uint) || enumUnderlyingType == typeof (ushort) || enumUnderlyingType == typeof (byte)))
// property.longValue = (long) (uint) num;
// else
// property.intValue = num;
// }
}
}
3 changes: 3 additions & 0 deletions Editor/PropertyDrawers/EditorGUIUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Editor/PropertyDrawers/EditorLayoutUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace DMotion.Editor
{
public static class EditorLayoutUtils
{
public static IEnumerable<Rect> HorizontalLayout(this Rect r, params float[] widths)
{
if (widths.Length == 0)
{
yield return r;
}

//normalize widths
var sumWidths = widths.Sum();
for (var i = 0; i < widths.Length; i++)
{
widths[i] /= sumWidths;
}

var spacing = EditorGUIUtility.standardVerticalSpacing;
var current = r;
current.width = r.width * widths[0];
yield return current;
for (var i = 1; i < widths.Length; i++)
{
var w = r.width* widths[i];
var prevW = r.width * widths[i - 1];
current.x += prevW + spacing;
current.width = w - spacing;
yield return current;
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/PropertyDrawers/EditorLayoutUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions Editor/PropertyDrawers/EditorSerializationUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;

namespace DMotion.Editor
{
public static class EditorSerializationUtils
{
private const BindingFlags AllBindingFlags = (BindingFlags)(-1);

public static TAttribute GetAttribute<TAttribute>(this SerializedProperty serializedProperty,
bool inherit = true)
where TAttribute : Attribute
{
if (serializedProperty == null)
{
throw new ArgumentNullException(nameof(serializedProperty));
}

var targetObjectType = serializedProperty.serializedObject.targetObject.GetType();

if (targetObjectType == null)
{
throw new ArgumentException($"Could not find the {nameof(targetObjectType)} of {nameof(serializedProperty)}");
}

foreach (var pathSegment in serializedProperty.propertyPath.Split('.'))
{
var fieldInfo = targetObjectType.GetField(pathSegment, AllBindingFlags);
if (fieldInfo != null)
{
return fieldInfo.GetCustomAttribute<TAttribute>(inherit);
}

var propertyInfo = targetObjectType.GetProperty(pathSegment, AllBindingFlags);
if (propertyInfo != null)
{
return propertyInfo.GetCustomAttribute<TAttribute>(inherit);
}
}

throw new ArgumentException($"Could not find the field or property of {nameof(serializedProperty)}");
}
public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this SerializedProperty serializedProperty, bool inherit = true)
where TAttribute : Attribute
{
if (serializedProperty == null)
{
throw new ArgumentNullException(nameof(serializedProperty));
}

var targetObjectType = serializedProperty.serializedObject.targetObject.GetType();

if (targetObjectType == null)
{
throw new ArgumentException($"Could not find the {nameof(targetObjectType)} of {nameof(serializedProperty)}");
}

foreach (var pathSegment in serializedProperty.propertyPath.Split('.'))
{
var fieldInfo = targetObjectType.GetField(pathSegment, AllBindingFlags);
if (fieldInfo != null)
{
return fieldInfo.GetCustomAttributes<TAttribute>(inherit);
}

var propertyInfo = targetObjectType.GetProperty(pathSegment, AllBindingFlags);
if (propertyInfo != null)
{
return propertyInfo.GetCustomAttributes<TAttribute>(inherit);
}
}

throw new ArgumentException($"Could not find the field or property of {nameof(serializedProperty)}");
}
}
}
3 changes: 3 additions & 0 deletions Editor/PropertyDrawers/EditorSerializationUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a3a4264

Please sign in to comment.