Skip to content

Instantly share code, notes, and snippets.

@rngtm
Last active May 14, 2022 03:13
Show Gist options
  • Save rngtm/b151f29a7a58fdbfadfbad8bafe93502 to your computer and use it in GitHub Desktop.
Save rngtm/b151f29a7a58fdbfadfbad8bafe93502 to your computer and use it in GitHub Desktop.
EditorWindowからDOTweenのDOMoveを実行するサンプル
using DG.Tweening;
using UnityEditor;
using UnityEngine;
public class DOTweenMoveWindow : EditorWindow
{
#region Class
/// <summary>
/// Tweenの状態
/// </summary>
private class TweenStatusData
{
public bool IsMove { get; set; }
public Tween Tween { get; set; } // 実行中のTween
public float LastUpdateTime { get; set; }
}
/// <summary>
/// EditorWindowの入力値
/// </summary>
[System.Serializable]
private class EditorInputValue
{
[field: SerializeField] public Transform Transform { get; set; } // 動かしたい対象のTransform
[field: SerializeField] public Vector3 Movement { get; set; } = new Vector3(1, 0, 0); // 移動量
[field: SerializeField] public float TimeLength { get; set; } = 1f; // 移動時間
[field: SerializeField] public Ease Ease { get; set; } = Ease.OutCubic;
}
#endregion // End of Class
const float TargetFrameRate = 30f;
const float TargetDeltaTime = 1f / TargetFrameRate;
[SerializeField] EditorInputValue inputValue = new EditorInputValue();
[System.NonSerialized] readonly TweenStatusData _tweenStatus = new TweenStatusData();
float CurrentTime => Time.realtimeSinceStartup;
[MenuItem("Tools/Tween Move Window")]
static void ShowWindow()
{
GetWindow<DOTweenMoveWindow>();
}
private void OnGUI()
{
inputValue.Transform = EditorGUILayout.ObjectField("Transform", inputValue.Transform, typeof(Transform), true)
as Transform;
inputValue.Movement = EditorGUILayout.Vector3Field("Movement", inputValue.Movement);
inputValue.Ease = (Ease) EditorGUILayout.EnumPopup("Ease", inputValue.Ease);
inputValue.TimeLength = EditorGUILayout.FloatField("Duration[s]", inputValue.TimeLength);
// Moveボタンを押すと、DOTweenで移動を開始
EditorGUI.BeginDisabledGroup(inputValue.Transform == null || _tweenStatus.IsMove);
if (GUILayout.Button("Move"))
{
// 移動開始
StartMove(inputValue);
this.Repaint();
}
EditorGUI.EndDisabledGroup();
if (inputValue.Transform == null)
{
EditorGUILayout.HelpBox($"{nameof(inputValue.Transform)}が指定されていません", MessageType.Error);
}
}
private void StartMove(EditorInputValue inputValue)
{
_tweenStatus.LastUpdateTime = CurrentTime;
var targetTransform = inputValue.Transform;
var moveTo = targetTransform.position + inputValue.Movement;
_tweenStatus.Tween = targetTransform.DOMove(moveTo, inputValue.TimeLength)
.SetEase(inputValue.Ease)
.OnComplete(() => _tweenStatus.IsMove = false);
_tweenStatus.IsMove = true; // 移動開始
}
private void Update()
{
UpdateTween();
}
private void UpdateTween()
{
if (!_tweenStatus.IsMove) return;
float deltaTime = CurrentTime - _tweenStatus.LastUpdateTime;
if (deltaTime > TargetDeltaTime)
{
UnityEngine.Debug.Log($"Update Tween : deltaTime = {deltaTime}");
_tweenStatus.Tween.ManualUpdate(deltaTime, deltaTime);
_tweenStatus.LastUpdateTime = CurrentTime;
}
}
}
@rngtm
Copy link
Author

rngtm commented May 14, 2022

動かしたい対象のTransformを指定して、Moveを押すとTransformが動きます
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment