// Fixed fields are intentional: Unity 2022.3 cannot animate custom nested classes or arrays. // Keep slot names stable; saved AnimationClips bind directly to these fields. using UnityEngine; namespace Yum.Fold { [ExecuteAlways, DisallowMultipleComponent] [AddComponentMenu("")] public sealed class FoldMaterialAnimator : MonoBehaviour { [HideInInspector] public FoldStack owner; [HideInInspector] public Renderer targetRenderer; [HideInInspector] public Material material; [HideInInspector] public int materialIndex; public bool slot0_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot0_opcode; public float slot0_float0; public float slot0_float1; public float slot0_float2; public float slot0_float3; public Vector4 slot0_vector0; public Vector4 slot0_vector1; public Vector4 slot0_vector2; public Vector4 slot0_vector3; public bool slot1_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot1_opcode; public float slot1_float0; public float slot1_float1; public float slot1_float2; public float slot1_float3; public Vector4 slot1_vector0; public Vector4 slot1_vector1; public Vector4 slot1_vector2; public Vector4 slot1_vector3; public bool slot2_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot2_opcode; public float slot2_float0; public float slot2_float1; public float slot2_float2; public float slot2_float3; public Vector4 slot2_vector0; public Vector4 slot2_vector1; public Vector4 slot2_vector2; public Vector4 slot2_vector3; public bool slot3_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot3_opcode; public float slot3_float0; public float slot3_float1; public float slot3_float2; public float slot3_float3; public Vector4 slot3_vector0; public Vector4 slot3_vector1; public Vector4 slot3_vector2; public Vector4 slot3_vector3; public bool slot4_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot4_opcode; public float slot4_float0; public float slot4_float1; public float slot4_float2; public float slot4_float3; public Vector4 slot4_vector0; public Vector4 slot4_vector1; public Vector4 slot4_vector2; public Vector4 slot4_vector3; public bool slot5_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot5_opcode; public float slot5_float0; public float slot5_float1; public float slot5_float2; public float slot5_float3; public Vector4 slot5_vector0; public Vector4 slot5_vector1; public Vector4 slot5_vector2; public Vector4 slot5_vector3; public bool slot6_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot6_opcode; public float slot6_float0; public float slot6_float1; public float slot6_float2; public float slot6_float3; public Vector4 slot6_vector0; public Vector4 slot6_vector1; public Vector4 slot6_vector2; public Vector4 slot6_vector3; public bool slot7_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot7_opcode; public float slot7_float0; public float slot7_float1; public float slot7_float2; public float slot7_float3; public Vector4 slot7_vector0; public Vector4 slot7_vector1; public Vector4 slot7_vector2; public Vector4 slot7_vector3; public bool slot8_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot8_opcode; public float slot8_float0; public float slot8_float1; public float slot8_float2; public float slot8_float3; public Vector4 slot8_vector0; public Vector4 slot8_vector1; public Vector4 slot8_vector2; public Vector4 slot8_vector3; public bool slot9_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot9_opcode; public float slot9_float0; public float slot9_float1; public float slot9_float2; public float slot9_float3; public Vector4 slot9_vector0; public Vector4 slot9_vector1; public Vector4 slot9_vector2; public Vector4 slot9_vector3; public bool slot10_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot10_opcode; public float slot10_float0; public float slot10_float1; public float slot10_float2; public float slot10_float3; public Vector4 slot10_vector0; public Vector4 slot10_vector1; public Vector4 slot10_vector2; public Vector4 slot10_vector3; public bool slot11_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot11_opcode; public float slot11_float0; public float slot11_float1; public float slot11_float2; public float slot11_float3; public Vector4 slot11_vector0; public Vector4 slot11_vector1; public Vector4 slot11_vector2; public Vector4 slot11_vector3; public bool slot12_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot12_opcode; public float slot12_float0; public float slot12_float1; public float slot12_float2; public float slot12_float3; public Vector4 slot12_vector0; public Vector4 slot12_vector1; public Vector4 slot12_vector2; public Vector4 slot12_vector3; public bool slot13_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot13_opcode; public float slot13_float0; public float slot13_float1; public float slot13_float2; public float slot13_float3; public Vector4 slot13_vector0; public Vector4 slot13_vector1; public Vector4 slot13_vector2; public Vector4 slot13_vector3; public bool slot14_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot14_opcode; public float slot14_float0; public float slot14_float1; public float slot14_float2; public float slot14_float3; public Vector4 slot14_vector0; public Vector4 slot14_vector1; public Vector4 slot14_vector2; public Vector4 slot14_vector3; public bool slot15_enabled; [UnityEngine.Animations.DiscreteEvaluation] public int slot15_opcode; public float slot15_float0; public float slot15_float1; public float slot15_float2; public float slot15_float3; public Vector4 slot15_vector0; public Vector4 slot15_vector1; public Vector4 slot15_vector2; public Vector4 slot15_vector3; public SlotAccess slots => new SlotAccess(this); public readonly struct SlotAccess { readonly FoldMaterialAnimator driver; public SlotAccess(FoldMaterialAnimator driver) { this.driver = driver; } public int Length => 16; public FoldOperator this[int index] { get => driver.Read(index, new FoldOperator()); set => driver.Write(index, value); } } public FoldOperator Read(int index, FoldOperator result) { switch (index) { case 0: result.enabled = slot0_enabled; result.opcode = slot0_opcode; result.float0 = slot0_float0; result.float1 = slot0_float1; result.float2 = slot0_float2; result.float3 = slot0_float3; result.vector0 = slot0_vector0; result.vector1 = slot0_vector1; result.vector2 = slot0_vector2; result.vector3 = slot0_vector3; break; case 1: result.enabled = slot1_enabled; result.opcode = slot1_opcode; result.float0 = slot1_float0; result.float1 = slot1_float1; result.float2 = slot1_float2; result.float3 = slot1_float3; result.vector0 = slot1_vector0; result.vector1 = slot1_vector1; result.vector2 = slot1_vector2; result.vector3 = slot1_vector3; break; case 2: result.enabled = slot2_enabled; result.opcode = slot2_opcode; result.float0 = slot2_float0; result.float1 = slot2_float1; result.float2 = slot2_float2; result.float3 = slot2_float3; result.vector0 = slot2_vector0; result.vector1 = slot2_vector1; result.vector2 = slot2_vector2; result.vector3 = slot2_vector3; break; case 3: result.enabled = slot3_enabled; result.opcode = slot3_opcode; result.float0 = slot3_float0; result.float1 = slot3_float1; result.float2 = slot3_float2; result.float3 = slot3_float3; result.vector0 = slot3_vector0; result.vector1 = slot3_vector1; result.vector2 = slot3_vector2; result.vector3 = slot3_vector3; break; case 4: result.enabled = slot4_enabled; result.opcode = slot4_opcode; result.float0 = slot4_float0; result.float1 = slot4_float1; result.float2 = slot4_float2; result.float3 = slot4_float3; result.vector0 = slot4_vector0; result.vector1 = slot4_vector1; result.vector2 = slot4_vector2; result.vector3 = slot4_vector3; break; case 5: result.enabled = slot5_enabled; result.opcode = slot5_opcode; result.float0 = slot5_float0; result.float1 = slot5_float1; result.float2 = slot5_float2; result.float3 = slot5_float3; result.vector0 = slot5_vector0; result.vector1 = slot5_vector1; result.vector2 = slot5_vector2; result.vector3 = slot5_vector3; break; case 6: result.enabled = slot6_enabled; result.opcode = slot6_opcode; result.float0 = slot6_float0; result.float1 = slot6_float1; result.float2 = slot6_float2; result.float3 = slot6_float3; result.vector0 = slot6_vector0; result.vector1 = slot6_vector1; result.vector2 = slot6_vector2; result.vector3 = slot6_vector3; break; case 7: result.enabled = slot7_enabled; result.opcode = slot7_opcode; result.float0 = slot7_float0; result.float1 = slot7_float1; result.float2 = slot7_float2; result.float3 = slot7_float3; result.vector0 = slot7_vector0; result.vector1 = slot7_vector1; result.vector2 = slot7_vector2; result.vector3 = slot7_vector3; break; case 8: result.enabled = slot8_enabled; result.opcode = slot8_opcode; result.float0 = slot8_float0; result.float1 = slot8_float1; result.float2 = slot8_float2; result.float3 = slot8_float3; result.vector0 = slot8_vector0; result.vector1 = slot8_vector1; result.vector2 = slot8_vector2; result.vector3 = slot8_vector3; break; case 9: result.enabled = slot9_enabled; result.opcode = slot9_opcode; result.float0 = slot9_float0; result.float1 = slot9_float1; result.float2 = slot9_float2; result.float3 = slot9_float3; result.vector0 = slot9_vector0; result.vector1 = slot9_vector1; result.vector2 = slot9_vector2; result.vector3 = slot9_vector3; break; case 10: result.enabled = slot10_enabled; result.opcode = slot10_opcode; result.float0 = slot10_float0; result.float1 = slot10_float1; result.float2 = slot10_float2; result.float3 = slot10_float3; result.vector0 = slot10_vector0; result.vector1 = slot10_vector1; result.vector2 = slot10_vector2; result.vector3 = slot10_vector3; break; case 11: result.enabled = slot11_enabled; result.opcode = slot11_opcode; result.float0 = slot11_float0; result.float1 = slot11_float1; result.float2 = slot11_float2; result.float3 = slot11_float3; result.vector0 = slot11_vector0; result.vector1 = slot11_vector1; result.vector2 = slot11_vector2; result.vector3 = slot11_vector3; break; case 12: result.enabled = slot12_enabled; result.opcode = slot12_opcode; result.float0 = slot12_float0; result.float1 = slot12_float1; result.float2 = slot12_float2; result.float3 = slot12_float3; result.vector0 = slot12_vector0; result.vector1 = slot12_vector1; result.vector2 = slot12_vector2; result.vector3 = slot12_vector3; break; case 13: result.enabled = slot13_enabled; result.opcode = slot13_opcode; result.float0 = slot13_float0; result.float1 = slot13_float1; result.float2 = slot13_float2; result.float3 = slot13_float3; result.vector0 = slot13_vector0; result.vector1 = slot13_vector1; result.vector2 = slot13_vector2; result.vector3 = slot13_vector3; break; case 14: result.enabled = slot14_enabled; result.opcode = slot14_opcode; result.float0 = slot14_float0; result.float1 = slot14_float1; result.float2 = slot14_float2; result.float3 = slot14_float3; result.vector0 = slot14_vector0; result.vector1 = slot14_vector1; result.vector2 = slot14_vector2; result.vector3 = slot14_vector3; break; case 15: result.enabled = slot15_enabled; result.opcode = slot15_opcode; result.float0 = slot15_float0; result.float1 = slot15_float1; result.float2 = slot15_float2; result.float3 = slot15_float3; result.vector0 = slot15_vector0; result.vector1 = slot15_vector1; result.vector2 = slot15_vector2; result.vector3 = slot15_vector3; break; default: throw new System.IndexOutOfRangeException(); } return result; } public void Write(int index, FoldOperator value) { switch (index) { case 0: slot0_enabled = value.enabled; slot0_opcode = value.opcode; slot0_float0 = value.float0; slot0_float1 = value.float1; slot0_float2 = value.float2; slot0_float3 = value.float3; slot0_vector0 = value.vector0; slot0_vector1 = value.vector1; slot0_vector2 = value.vector2; slot0_vector3 = value.vector3; break; case 1: slot1_enabled = value.enabled; slot1_opcode = value.opcode; slot1_float0 = value.float0; slot1_float1 = value.float1; slot1_float2 = value.float2; slot1_float3 = value.float3; slot1_vector0 = value.vector0; slot1_vector1 = value.vector1; slot1_vector2 = value.vector2; slot1_vector3 = value.vector3; break; case 2: slot2_enabled = value.enabled; slot2_opcode = value.opcode; slot2_float0 = value.float0; slot2_float1 = value.float1; slot2_float2 = value.float2; slot2_float3 = value.float3; slot2_vector0 = value.vector0; slot2_vector1 = value.vector1; slot2_vector2 = value.vector2; slot2_vector3 = value.vector3; break; case 3: slot3_enabled = value.enabled; slot3_opcode = value.opcode; slot3_float0 = value.float0; slot3_float1 = value.float1; slot3_float2 = value.float2; slot3_float3 = value.float3; slot3_vector0 = value.vector0; slot3_vector1 = value.vector1; slot3_vector2 = value.vector2; slot3_vector3 = value.vector3; break; case 4: slot4_enabled = value.enabled; slot4_opcode = value.opcode; slot4_float0 = value.float0; slot4_float1 = value.float1; slot4_float2 = value.float2; slot4_float3 = value.float3; slot4_vector0 = value.vector0; slot4_vector1 = value.vector1; slot4_vector2 = value.vector2; slot4_vector3 = value.vector3; break; case 5: slot5_enabled = value.enabled; slot5_opcode = value.opcode; slot5_float0 = value.float0; slot5_float1 = value.float1; slot5_float2 = value.float2; slot5_float3 = value.float3; slot5_vector0 = value.vector0; slot5_vector1 = value.vector1; slot5_vector2 = value.vector2; slot5_vector3 = value.vector3; break; case 6: slot6_enabled = value.enabled; slot6_opcode = value.opcode; slot6_float0 = value.float0; slot6_float1 = value.float1; slot6_float2 = value.float2; slot6_float3 = value.float3; slot6_vector0 = value.vector0; slot6_vector1 = value.vector1; slot6_vector2 = value.vector2; slot6_vector3 = value.vector3; break; case 7: slot7_enabled = value.enabled; slot7_opcode = value.opcode; slot7_float0 = value.float0; slot7_float1 = value.float1; slot7_float2 = value.float2; slot7_float3 = value.float3; slot7_vector0 = value.vector0; slot7_vector1 = value.vector1; slot7_vector2 = value.vector2; slot7_vector3 = value.vector3; break; case 8: slot8_enabled = value.enabled; slot8_opcode = value.opcode; slot8_float0 = value.float0; slot8_float1 = value.float1; slot8_float2 = value.float2; slot8_float3 = value.float3; slot8_vector0 = value.vector0; slot8_vector1 = value.vector1; slot8_vector2 = value.vector2; slot8_vector3 = value.vector3; break; case 9: slot9_enabled = value.enabled; slot9_opcode = value.opcode; slot9_float0 = value.float0; slot9_float1 = value.float1; slot9_float2 = value.float2; slot9_float3 = value.float3; slot9_vector0 = value.vector0; slot9_vector1 = value.vector1; slot9_vector2 = value.vector2; slot9_vector3 = value.vector3; break; case 10: slot10_enabled = value.enabled; slot10_opcode = value.opcode; slot10_float0 = value.float0; slot10_float1 = value.float1; slot10_float2 = value.float2; slot10_float3 = value.float3; slot10_vector0 = value.vector0; slot10_vector1 = value.vector1; slot10_vector2 = value.vector2; slot10_vector3 = value.vector3; break; case 11: slot11_enabled = value.enabled; slot11_opcode = value.opcode; slot11_float0 = value.float0; slot11_float1 = value.float1; slot11_float2 = value.float2; slot11_float3 = value.float3; slot11_vector0 = value.vector0; slot11_vector1 = value.vector1; slot11_vector2 = value.vector2; slot11_vector3 = value.vector3; break; case 12: slot12_enabled = value.enabled; slot12_opcode = value.opcode; slot12_float0 = value.float0; slot12_float1 = value.float1; slot12_float2 = value.float2; slot12_float3 = value.float3; slot12_vector0 = value.vector0; slot12_vector1 = value.vector1; slot12_vector2 = value.vector2; slot12_vector3 = value.vector3; break; case 13: slot13_enabled = value.enabled; slot13_opcode = value.opcode; slot13_float0 = value.float0; slot13_float1 = value.float1; slot13_float2 = value.float2; slot13_float3 = value.float3; slot13_vector0 = value.vector0; slot13_vector1 = value.vector1; slot13_vector2 = value.vector2; slot13_vector3 = value.vector3; break; case 14: slot14_enabled = value.enabled; slot14_opcode = value.opcode; slot14_float0 = value.float0; slot14_float1 = value.float1; slot14_float2 = value.float2; slot14_float3 = value.float3; slot14_vector0 = value.vector0; slot14_vector1 = value.vector1; slot14_vector2 = value.vector2; slot14_vector3 = value.vector3; break; case 15: slot15_enabled = value.enabled; slot15_opcode = value.opcode; slot15_float0 = value.float0; slot15_float1 = value.float1; slot15_float2 = value.float2; slot15_float3 = value.float3; slot15_vector0 = value.vector0; slot15_vector1 = value.vector1; slot15_vector2 = value.vector2; slot15_vector3 = value.vector3; break; default: throw new System.IndexOutOfRangeException(); } } MaterialPropertyBlock original, block; readonly FoldOperator scratch = new FoldOperator(); readonly System.Collections.Generic.List shared = new System.Collections.Generic.List(); void OnEnable() => Apply(); void LateUpdate() => Apply(); void OnDidApplyAnimationProperties() => Apply(); void OnDisable() => Restore(); void OnDestroy() => Restore(); public void Apply() { if (!isActiveAndEnabled || !owner || !owner.isActiveAndEnabled || !targetRenderer || !material) { Restore(); return; } targetRenderer.GetSharedMaterials(shared); if (materialIndex < 0 || materialIndex >= shared.Count || shared[materialIndex] != material) { Restore(); return; } if (block == null) block = new MaterialPropertyBlock(); if (original == null) { original = new MaterialPropertyBlock(); targetRenderer.GetPropertyBlock(original, materialIndex); } targetRenderer.GetPropertyBlock(block, materialIndex); if (block.isEmpty) targetRenderer.GetPropertyBlock(block); for (int i = 0; i < 16; i++) Read(i, scratch).Write(block, i); targetRenderer.SetPropertyBlock(block, materialIndex); } public void Restore() { if (original == null) return; if (targetRenderer) { targetRenderer.GetSharedMaterials(shared); if (materialIndex >= 0 && materialIndex < shared.Count) targetRenderer.SetPropertyBlock(original.isEmpty ? null : original, materialIndex); } original = null; } } }