using System; namespace LibMMD.Unity3D { /// Captures submesh visibility and can restore it after reloading the same model. public sealed class MmdSubMeshVisibilitySnapshot { private readonly string[] _names; private readonly bool[] _visible; private MmdSubMeshVisibilitySnapshot(string[] names, bool[] visible) { _names = names; _visible = visible; } public int Count => _visible.Length; public static MmdSubMeshVisibilitySnapshot Capture(IMmdModelInstance model) { if (model == null) throw new ArgumentNullException(nameof(model)); var names = model.GetSubMeshNames(); var visible = new bool[names.Length]; for (var i = 0; i < visible.Length; i++) visible[i] = model.IsSubMeshVisible(i); return new MmdSubMeshVisibilitySnapshot(names, visible); } /// /// Applies entries whose index and material name still match, and returns the number applied. /// The name check avoids restoring stale state when a model file was edited in place. /// public int ApplyTo(IMmdModelInstance model) { if (model == null) throw new ArgumentNullException(nameof(model)); var currentNames = model.GetSubMeshNames(); var count = Math.Min(currentNames.Length, Math.Min(_names.Length, _visible.Length)); var applied = 0; for (var i = 0; i < count; i++) { if (!string.Equals(currentNames[i], _names[i], StringComparison.Ordinal)) continue; model.SetSubMeshVisible(i, _visible[i]); applied++; } return applied; } } }