public AfterEffectsInstance(AfterEffectsResource resource) { SetResource(resource); speed = 1f; // デフォルトセット _cut = _resource.defaultCut; _lastAppliedFrame = -(float.MaxValue); }
public void Dispose() { _resource = null; _cut = null; _onEnd = null; _onLoop = null; _scaleCurves = null; _rotationCurves = null; _graphicPositionCurves = null; _graphicSizeCurves = null; _spriteRendererPositionCurves = null; _graphicOpacityCurves = null; _canvasGroupOpacityCurves = null; _spriteRendererOpacityCurves = null; _graphicVisibilityCurves = null; _spriteRendererVisibilityCurves = null; _gameObjectVisibilityCurves = null; _callbackTimeline = null; }
// 区間の最初に戻す。ループカウントも初期化。カット名が与えられればそのカットに変更 public void Rewind(string cutName = null, Action onEnd = null, Action onLoop = null) { if (cutName != null) { var cut = _resource.FindCut(cutName); if (cut != null) { _cut = cut; } else { Debug.LogError("Rewind failed. " + cutName + " is not found."); } } _frame = _cut.start; _currentLoopCount = 0; _nextIsEnd = false; _onEnd = onEnd; _onLoop = onLoop; }
public virtual void UpdatePerFrame(float deltaTime) { // 再生中でなく、「もう一回」がないなら、抜ける if (!_isPlaying) { return; } if (_lastAppliedFrame != _frame) { _lastAppliedFrame = _frame; Vector2 tmpVector2; // 計算するよ if (_scaleCurves != null) { foreach (var curve in _scaleCurves) { // スケールは1値の場合と2値の場合がある if (curve.type == AfterEffectsCurveSet.ValueType.Vector2) { _resource.GetVector2(out tmpVector2, curve.index, _frame); curve.transform.localScale = new Vector3(tmpVector2.x, tmpVector2.y, 1f); } else { var v = _resource.GetFloat(curve.index, _frame); curve.transform.localScale = new Vector3(v, v, 1f); } } } if (_rotationCurves != null) { foreach (var curve in _rotationCurves) { float v = _resource.GetFloat(curve.index, _frame); curve.transform.localRotation = Quaternion.Euler(0f, 0f, -v); //角度逆 } } if (_graphicPositionCurves != null) { foreach (var curve in _graphicPositionCurves) { _resource.GetVector2(out tmpVector2, curve.index, _frame); tmpVector2.y = -tmpVector2.y; // Yは反転。下プラス。 curve.transform.anchoredPosition = tmpVector2; } } if (_graphicSizeCurves != null) { foreach (var curve in _graphicSizeCurves) { _resource.GetVector2(out tmpVector2, curve.index, _frame); curve.transform.sizeDelta = tmpVector2; } } if (_spriteRendererPositionCurves != null) { foreach (var curve in _spriteRendererPositionCurves) { _resource.GetVector2(out tmpVector2, curve.index, _frame); // zはそのまま保つ var p = curve.transform.localPosition; p.x = tmpVector2.x; p.y = -tmpVector2.y; //yは反転。下プラス。 curve.transform.localPosition = p; } } if (_graphicOpacityCurves != null) { foreach (var curve in _graphicOpacityCurves) { float v = _resource.GetFloat(curve.index, _frame); var c = curve.graphic.color; c.a = v; curve.graphic.color = c; } } if (_canvasGroupOpacityCurves != null) { foreach (var curve in _canvasGroupOpacityCurves) { float v = _resource.GetFloat(curve.index, _frame); curve.canvasGroup.alpha = v; } } if (_spriteRendererOpacityCurves != null) { foreach (var curve in _spriteRendererOpacityCurves) { float v = _resource.GetFloat(curve.index, _frame); var c = curve.spriteRenderer.color; c.a = v; curve.spriteRenderer.color = c; } } if (_graphicVisibilityCurves != null) { foreach (var curve in _graphicVisibilityCurves) { var v = _resource.GetBool(curve.index, _frame); curve.graphic.enabled = v; } } if (_spriteRendererVisibilityCurves != null) { foreach (var curve in _spriteRendererVisibilityCurves) { var v = _resource.GetBool(curve.index, _frame); curve.spriteRenderer.enabled = v; } } if (_gameObjectVisibilityCurves != null) { foreach (var curve in _gameObjectVisibilityCurves) { var v = _resource.GetBool(curve.index, _frame); var obj = curve.gameObject; if (v) { if (obj.activeSelf == false) { curve.gameObject.SetActive(true); } } else { if (obj.activeSelf) { curve.gameObject.SetActive(false); } } } } } if (_nextIsEnd) //最終フレーム反映終了。完全に止める { _isPlaying = false; _nextIsEnd = false; if (_onEnd != null) { _onEnd(); } } else { var oldFrame = _frame; _frame += deltaTime * _resource.frameRate * speed; if (_callbackTimeline != null) { _callbackTimeline.Execute(oldFrame, _frame); } // ループ処理 float loopEnd = _cut.loopStart + _cut.loopDuration; while ((_frame > loopEnd) && (_currentLoopCount < _cut.loopCount)) { _frame -= _cut.loopDuration; //startに戻すわけではない。 if (_onLoop != null) { _onLoop(); } _currentLoopCount++; } // 終了判定 float end = _cut.start + _cut.duration; if (_frame > end) // ピッタリは終わらせない。最終フレームでspeed0で滞留、みたいなのを許す。 { bool nextExist = false; if (_cut.nextCutName != null) // 自動遷移が設定されている { var cut = _resource.FindCut(_cut.nextCutName); if (cut != null) { _cut = cut; _frame = _cut.start + (_frame - end); // 余り分進めておく _currentLoopCount = 0; nextExist = true; } else { Debug.LogError("auto transition failed. " + _cut.nextCutName + " is not found."); } } if (!nextExist) { _nextIsEnd = true; _frame = end - _endTimeEpsilon; // 最終時刻寸前に合わせる } } } }