Skip to content

Commit

Permalink
Fix for toggles using int parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
NinDevs committed Jul 9, 2023
1 parent e554b2a commit 09f5add
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion VRCAvatarHelper/EZAva2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static GameObject avatar
public static bool autoSelectFolderWhenRun = true;
public static bool enableUnityDebugLogs = true;

public static string Version = "v1.1.3";
public static string Version = "v1.1.4";

public enum CreationType
{
Expand Down
9 changes: 2 additions & 7 deletions VRCAvatarHelper/EzAva2Algos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,9 @@ public static void SetupGameObjectToggles(ref List<Category> objCategories)

if (layer.stateMachine.states.Count() >= 2 && ControllerUtil.GetParameterByName(controller, parametername).type == AnimatorControllerParameterType.Bool)
{
layer.stateMachine.RemoveState(layer.stateMachine.states[1].state);

var previousStateName = layer.stateMachine.states[0].state.name;
var previousStateClip = layer.stateMachine.states[0].state.motion.name;

layer.stateMachine.RemoveState(layer.stateMachine.states[0].state);
ControllerUtil.RemoveStates(layer);

states[y] = layer.stateMachine.AddState("Toggles Idle", new Vector3(31, -45));
objCategories[i].states.Add(states[y]);
Expand All @@ -298,9 +295,7 @@ public static void SetupGameObjectToggles(ref List<Category> objCategories)
//Readd the state with the previous clip. This is just so that the default state is always the idle state, we delete the previous state and readd after idle state creation
layer.stateMachine.AddState(previousStateName, new Vector3(360, 55));
objCategories[i].states.Add(layer.stateMachine.states.Where(x => x.state.name == previousStateName).ToList()[0].state);
layer.stateMachine.states[1].state.motion = AnimUtil.LoadAnimClip(previousStateClip, previousStateClip.Substring(0, previousStateClip.LastIndexOf('O'))) != null ?
AnimUtil.LoadAnimClip(previousStateClip, previousStateClip.Substring(0, previousStateClip.LastIndexOf('O'))) :
AnimUtil.LoadAnimClip(previousStateClip, "Multi-Toggles");
layer.stateMachine.states[1].state.motion = AnimUtil.LoadAnimClip(previousStateClip, "Switched");

ControllerUtil.ChangeParameterToInt(controller, layer, expressionParametersMenu, parametername);
objCategories[i].switched = true;
Expand Down
54 changes: 49 additions & 5 deletions VRCAvatarHelper/EzAva2AnimUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ public static void MakeAnimationClips(ref List<Category> matCategories, ref List

//Allows automatic conversion from on off to any state int toggles if we add to an existing layer that had previously two states and toggled via bool
bool wasOnOffLayer = (objCategories[i].layerExists == true && (ControllerUtil.GetParameterByName(EZAvatar.controller, $"Toggle {objCategories[i].name}")?.type == AnimatorControllerParameterType.Bool)) == true ? true : false;
var onOffPath = "";

//If we are using any state transitions for objects toggles instead of on/off
if (objCategories[i].makeIdle)
Expand All @@ -286,15 +287,37 @@ public static void MakeAnimationClips(ref List<Category> matCategories, ref List
if (idleClip != null)
EditorUtility.SetDirty(idleClip);
if (wasOnOffLayer)
{
var onStateClip = ControllerUtil.GetLayerByName(ref EZAvatar.controller, $"Toggle {objCategories[i].name}").stateMachine.states.Where(x => x.state.name.Contains("ON")).ToList()[0].state.motion as AnimationClip;
{
var previousOnStateClip = ControllerUtil.GetLayerByName(ref EZAvatar.controller, $"Toggle {objCategories[i].name}").stateMachine.states.Where(x => x.state.name.Contains("ON")).ToList()[0].state.motion as AnimationClip;
#pragma warning disable CS0618 // Type or member is obsolete
var onStateCurve = AnimationUtility.GetAllCurves(onStateClip)[0];
var onStateCurve = AnimationUtility.GetAllCurves(previousOnStateClip)[0];
#pragma warning restore CS0618 // Type or member is obsolete
onOffPath = onStateCurve.path;

var newOnStateClip = new AnimationClip();
newOnStateClip.name = previousOnStateClip.name;
newOnStateClip.SetCurve(onStateCurve.path, typeof(GameObject), "m_IsActive", onStateCurve.curve);

for (int b = 0; b < gameObj.Count(); b++)
{
foreach (var obj in gameObj)
{
if (obj != gameObj[b])
{
var newOffCurve = new AnimationCurve();
var newPath = obj.transform.GetHierarchyPath().Substring(EZAvatar.avatar.name.Length + 1);
newOffCurve.AddKey(0, 0);
newOffCurve.AddKey(1 / previousOnStateClip.frameRate, 0);
newOnStateClip.SetCurve(newPath, typeof(GameObject), "m_IsActive", newOffCurve);
}
}
}

ExportClip(newOnStateClip, "Switched");

var newCurve = new AnimationCurve();
newCurve.AddKey(0, 0);
newCurve.AddKey(1 / onStateClip.frameRate, 0);
newCurve.AddKey(1 / previousOnStateClip.frameRate, 0);

idleClip = new AnimationClip();
idleClip.name = $"{objCategories[i].name}Idle";
Expand All @@ -304,14 +327,35 @@ public static void MakeAnimationClips(ref List<Category> matCategories, ref List
for (int y = 0; y < gameObj.Count(); y++)
{
var onClip = new AnimationClip();
var path = gameObj[y].transform.GetHierarchyPath().Substring(EZAvatar.avatar.name.Length + 1);

//Creates curves/keys for gameobject active, per object
var onCurve = new AnimationCurve();
var path = gameObj[y].transform.GetHierarchyPath().Substring(EZAvatar.avatar.name.Length + 1);
onCurve.AddKey(0, 1);
onCurve.AddKey(1 / onClip.frameRate, 1);
onClip.SetCurve(path, typeof(GameObject), "m_IsActive", onCurve);

//For each object that is not the current obj, set them to off
foreach (var obj in gameObj)
{
if (obj != gameObj[y])
{
var newOffCurve = new AnimationCurve();
var newPath = obj.transform.GetHierarchyPath().Substring(EZAvatar.avatar.name.Length + 1);
newOffCurve.AddKey(0, 0);
newOffCurve.AddKey(1 / onClip.frameRate, 0);
onClip.SetCurve(newPath, typeof(GameObject), "m_IsActive", newOffCurve);
}
}

if (wasOnOffLayer)
{
var previousStateCurve = new AnimationCurve();
previousStateCurve.AddKey(0, 0);
previousStateCurve.AddKey(1 / onClip.frameRate, 0);
onClip.SetCurve(onOffPath, typeof(GameObject), "m_IsActive", previousStateCurve);
}

onClip.name = $"{gameObj[y].name}ON";
objCategories[i].animClips.Add(onClip);
ExportClip(onClip, objCategories[i].name);
Expand Down

0 comments on commit 09f5add

Please sign in to comment.