Skip to content

Commit

Permalink
Merge pull request #820 from anatawa12/workaround-unity-bug
Browse files Browse the repository at this point in the history
chore: avoid `Array index (n) is out of bounds (size=m)` error
  • Loading branch information
anatawa12 authored Jan 7, 2024
2 parents a941fec + 74212f8 commit 6d3c811
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The format is based on [Keep a Changelog].
- In addition, location of the unknown components are shown on the error report.
- AvatarOptimizer didn't register modification to ObjectRegistry `#815`
- Empty Armature trick broken `#819`
- Added workaround for `Array index (n) is out of bounds (size=m)` error

### Security

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog].
- Unknown component warning were shown multiple time for one type `#818`
- In addition, location of the unknown components are shown on the error report.
- Empty Armature trick broken `#819`
- Added workaround for `Array index (n) is out of bounds (size=m)` error

### Security

Expand Down
30 changes: 29 additions & 1 deletion Editor/Processors/MeshInfo2Holder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.Processors.SkinnedMeshes;
Expand All @@ -20,11 +21,38 @@ public void OnActivate(BuildContext context)
public void OnDeactivate(BuildContext context)
{
Debug.Assert(Holder != null, nameof(Holder) + " != null");
Holder.SaveToMesh();
// avoid Array index (n) is out of bounds (size=m) error
// by assigning null to AnimatorController before changing blendShapes count
// and assigning back after changing blendShapes count.
// see https://github.com/anatawa12/AvatarOptimizer/issues/804
using (new EvacuateAnimatorController(context))
{
Holder.SaveToMesh();
}
Holder = null;
}
}

internal struct EvacuateAnimatorController : IDisposable
{
private Dictionary<Animator, RuntimeAnimatorController> _controllers;

public EvacuateAnimatorController(BuildContext context)
{
_controllers = context.GetComponents<Animator>()
.ToDictionary(a => a, a => a.runtimeAnimatorController);

foreach (var animator in _controllers.Keys)
animator.runtimeAnimatorController = null;
}

public void Dispose()
{
foreach (var (animator, runtimeAnimatorController) in _controllers)
animator.runtimeAnimatorController = runtimeAnimatorController;
}
}

internal class MeshInfo2Holder
{
private readonly Dictionary<SkinnedMeshRenderer, MeshInfo2> _skinnedCache =
Expand Down

0 comments on commit 6d3c811

Please sign in to comment.