Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
大量のオブジェクトを何度もTweenさせる場合はSetRecyclable(true)にした方がよいかもと検証したサンプルコード
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
public class PooledDOTween : MonoBehaviour
{
private List<GameObject> objs = new List<GameObject>();
/// <summary>
/// TweenerのPoolフラグ
/// </summary>
public bool isPool = false;
private bool _isPlayed;
/// <summary>
/// カプセルをたくさん生成
/// </summary>
public void CreateCapsules()
{
for (int i = 0; i < 10000; i++)
{
var obj = GameObject.CreatePrimitive(PrimitiveType.Capsule);
// 不要なコンポーネントは削除
var collider = obj.GetComponent<Collider>();
Destroy(collider);
objs.Add(obj);
}
}
/// <summary>
/// Tween開始
/// </summary>
public void StartTween()
{
if (_isPlayed)
{
return;
}
_isPlayed = true;
StartCoroutine(_StartTween());
}
private IEnumerator _StartTween()
{
while (true)
{
foreach (var obj in objs)
{
var goal = new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(10f, 50f));
if (isPool)
{
// DOTweenのPoolを有効化する
obj.transform.DOLocalMove(goal, 1f).SetRecyclable();
}
else
{
obj.transform.DOLocalMove(goal, 1f);
}
}
yield return new WaitForSeconds(2f);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(PooledDOTween))]
public class PooledDOTweenEditor : Editor
{
private PooledDOTween _target;
private void OnEnable ()
{
_target = (PooledDOTween)target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("CreateCapsules"))
{
_target.CreateCapsules();
}
if (GUILayout.Button("StartTween"))
{
_target.StartTween();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment