Animator parameters save and restore

This commit is contained in:
SDraw 2023-03-18 01:29:44 +03:00
parent c90119e20a
commit 3a6304e566
No known key found for this signature in database
GPG key ID: BB95B4DAB2BB8BB5
2 changed files with 78 additions and 6 deletions

View file

@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace ml_amt
{
class AnimatorAnalyzer
{
bool m_enabled = true;
List<AnimatorControllerParameter> m_parameters = null;
public void AnalyzeFrom(Animator p_animator)
{
m_enabled = p_animator.enabled;
m_parameters = p_animator.parameters?.ToList();
if(m_parameters != null)
{
foreach(var l_param in m_parameters)
{
switch(l_param.type)
{
case AnimatorControllerParameterType.Bool:
case AnimatorControllerParameterType.Trigger:
l_param.defaultBool = p_animator.GetBool(l_param.nameHash);
break;
case AnimatorControllerParameterType.Float:
l_param.defaultFloat = p_animator.GetFloat(l_param.nameHash);
break;
case AnimatorControllerParameterType.Int:
l_param.defaultInt = p_animator.GetInteger(l_param.nameHash);
break;
}
}
}
}
public void ApplyTo(Animator p_animator)
{
p_animator.enabled = m_enabled;
if(m_parameters != null)
{
foreach(var l_param in m_parameters)
{
switch(l_param.type)
{
case AnimatorControllerParameterType.Bool:
p_animator.SetBool(l_param.nameHash, l_param.defaultBool);
break;
case AnimatorControllerParameterType.Float:
p_animator.SetFloat(l_param.nameHash, l_param.defaultFloat);
break;
case AnimatorControllerParameterType.Int:
p_animator.SetInteger(l_param.nameHash, l_param.defaultInt);
break;
case AnimatorControllerParameterType.Trigger:
{
if(l_param.defaultBool)
p_animator.SetTrigger(l_param.nameHash);
}
break;
}
}
}
}
public bool IsEnabled() => m_enabled;
}
}

View file

@ -256,14 +256,15 @@ namespace ml_amt
return false;
}
static void OnOverride_Prefix(ref CVRAnimatorManager __instance, ref bool __state)
static void OnOverride_Prefix(ref CVRAnimatorManager __instance, out AnimatorAnalyzer __state)
{
__state = new AnimatorAnalyzer();
try
{
if(Settings.OverrideFix && (__instance.animator != null))
{
__state = __instance.animator.enabled;
if(__state)
__state.AnalyzeFrom(__instance.animator);
if(__state.IsEnabled())
__instance.animator.enabled = false;
__instance.animator.WriteDefaultValues();
}
@ -273,14 +274,14 @@ namespace ml_amt
MelonLoader.MelonLogger.Error(l_exception);
}
}
static void OnOverride_Postfix(ref CVRAnimatorManager __instance, bool __state)
static void OnOverride_Postfix(ref CVRAnimatorManager __instance, AnimatorAnalyzer __state)
{
try
{
if(Settings.OverrideFix && (__instance.animator != null))
{
__instance.animator.enabled = __state;
if(__state)
__state.ApplyTo(__instance.animator);
if(__state.IsEnabled())
__instance.animator.Update(0f);
}
}