Last active
April 24, 2024 03:20
-
-
Save baobao/e9def067325170dee80cc45986472cf2 to your computer and use it in GitHub Desktop.
大量のオブジェクトを何度もTweenさせる場合はSetRecyclable(true)にした方がよいかもと検証したサンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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