Skip to content

Commit

Permalink
fix: Disappearing changes after reboot or duplication
Browse files Browse the repository at this point in the history
I'm still writing a blog post about this one because, this
takes the cake in terms of broken interactions with the
Unity Editor.

The Unity Editor will show the content of the Animation
Controller additional layers, the new menus items and
parameters without any problem after using the tool.
However before this patch, cloning any of these assets
or restarting the Editor lead to these assets losing
their contents.

Meaning that after a restart, items added to the menus,
parameters and animation controllers layers were lost.

This was due to me not marking the modified elements as
"Dirty".
So even though I explicitly clicked on "Save" in the
editor, even though the editor was clearly showing
"apparently" non-saved content; without a "dirty" flag,
the editor refused to actually save the changes.

Also the way the layers are added to the AnimationController
are somewhat broken.
Currently StateMachine objects are generated in advance, and
then attached to layers added to the FX AnimationController.
This "works" but generated more issues than anything.
I'll try to generate a complete AnimationController and
add code to fuse AnimationControllers together in the
next updates. This should resolve most of the encountered
issues.

Meanwhile, this provoked a similar issue as well, since the
states and transitions added to these StateMachine were attached
to nothing (the StateMachine having no backing storage when
the states are added to it).
Meaning that the StateMachine would lose its States and
Transitions after duplication or reboot.

In order to avoid the issue, additional code make
sure that each state and transition is clearly saved within
the Animation Controller.

Still, what a bunch of stupid bugs. Seriously, why does
this editor doesn't emit any warning when not saving assets
when you explicitly ask for a "Save" to be performed ?

Signed-off-by: Voyage <[email protected]>
  • Loading branch information
vr-voyage committed Jul 31, 2022
1 parent 671295e commit c0d0075
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Constraints/SetupAvatarConstraints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using VRC.SDK3.Avatars.Components;
using UnityEditor.Animations;
using VRC.SDK3.Avatars.ScriptableObjects;
using UnityEditor;

namespace Myy
{
Expand Down Expand Up @@ -198,6 +199,11 @@ private void AttachToAvatar(VRCAvatarDescriptor avatar, MyyAssetsManager runAsse
/* FIXME Let the user define the last parameter */
MyyVRCHelpers.VRCMenuAddSubMenu(menu, subMenu, "World Objects");

EditorUtility.SetDirty(menu);
EditorUtility.SetDirty(subMenu);
EditorUtility.SetDirty(parameters);
AssetDatabase.SaveAssets();

avatarCopy.gameObject.SetActive(true);
}

Expand Down
1 change: 1 addition & 0 deletions Constraints/SetupObjectConstraints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ private bool StateMachineSetupOnOff(AnimatorStateMachine machineOnOff)
{
string paramName = parameters[(int)ParameterIndex.ONOFF].name;


AnimatorState objectOFF = machineOnOff.AddState("OFF", clips[(int)ClipIndex.OFF]);
AnimatorState objectON = machineOnOff.AddState("ON", clips[(int)ClipIndex.ON]);

Expand Down
14 changes: 13 additions & 1 deletion MyyHelpers/MyyAnimHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,19 @@ public static void ControllerAddLayer(
layer.stateMachine = stateMachine;
layer.name = stateMachine.name;
layer.defaultWeight = 1;
AssetDatabase.AddObjectToAsset(layer.stateMachine, AssetDatabase.GetAssetPath(controller));

AssetDatabase.AddObjectToAsset(layer.stateMachine, controller);
foreach (var graphicalState in stateMachine.states)
{
var state = graphicalState.state;

AssetDatabase.AddObjectToAsset(state, controller);
foreach (var transition in state.transitions)
{
AssetDatabase.AddObjectToAsset(transition, controller);
}
}

controller.AddLayer(layer);
}

Expand Down
6 changes: 6 additions & 0 deletions ParticleSystem/SetupAvatarParticles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using VRC.SDK3.Avatars.Components;
using UnityEditor.Animations;
using VRC.SDK3.Avatars.ScriptableObjects;
using UnityEditor;

namespace Myy
{
Expand Down Expand Up @@ -158,6 +159,11 @@ private void AttachToAvatar(VRCAvatarDescriptor avatar, MyyAssetsManager runAsse
toAttach.CopyAnimationParameters(menuParams);
toAttach.VRCMenuAddButtons(menu);

EditorUtility.SetDirty(menu);
EditorUtility.SetDirty(subMenu);
EditorUtility.SetDirty(menuParams);
AssetDatabase.SaveAssets();

avatarCopy.gameObject.SetActive(true);
}

Expand Down

0 comments on commit c0d0075

Please sign in to comment.