加入很多新内容,这波一块交了
This commit is contained in:
+73
-8
@@ -28,11 +28,19 @@ public class iNumberPrefabController : MonoBehaviour
|
||||
|
||||
private bool _warnedMissingPrefab;
|
||||
private bool _warnedMissingParents;
|
||||
private Transform pooledRoot;
|
||||
private readonly System.Collections.Generic.Dictionary<GameObject, System.Collections.Generic.Stack<playerInstantNumbersPrefab>> pools
|
||||
= new System.Collections.Generic.Dictionary<GameObject, System.Collections.Generic.Stack<playerInstantNumbersPrefab>>();
|
||||
private readonly System.Collections.Generic.Dictionary<playerInstantNumbersPrefab, GameObject> sourcePrefabs
|
||||
= new System.Collections.Generic.Dictionary<playerInstantNumbersPrefab, GameObject>();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null) Instance = this;
|
||||
else if (Instance != this) { Destroy(gameObject); return; }
|
||||
|
||||
pooledRoot = new GameObject("__instant_number_pool").transform;
|
||||
pooledRoot.SetParent(transform, false);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -150,19 +158,76 @@ public class iNumberPrefabController : MonoBehaviour
|
||||
return;
|
||||
}
|
||||
|
||||
var go = Instantiate(prefab, parent, false);
|
||||
if (go == null) return;
|
||||
var view = GetPooledInstance(prefab, parent);
|
||||
if (view != null) view.Play(type, signedValue);
|
||||
}
|
||||
|
||||
if (prefab.scene.IsValid())
|
||||
private playerInstantNumbersPrefab GetPooledInstance(GameObject prefab, Transform parent)
|
||||
{
|
||||
if (prefab == null || parent == null)
|
||||
return null;
|
||||
|
||||
if (!pools.TryGetValue(prefab, out var pool))
|
||||
{
|
||||
if (prefab.activeSelf) prefab.SetActive(false);
|
||||
pool = new System.Collections.Generic.Stack<playerInstantNumbersPrefab>();
|
||||
pools[prefab] = pool;
|
||||
}
|
||||
|
||||
// Ensure the newly created object is active before playing animation
|
||||
if (!go.activeInHierarchy) go.SetActive(true);
|
||||
playerInstantNumbersPrefab view = null;
|
||||
while (pool.Count > 0 && view == null)
|
||||
{
|
||||
view = pool.Pop();
|
||||
}
|
||||
|
||||
var view = go.GetComponent<playerInstantNumbersPrefab>();
|
||||
if (view != null) view.Play(type, signedValue);
|
||||
if (view == null)
|
||||
{
|
||||
var go = Instantiate(prefab, parent, false);
|
||||
if (go == null)
|
||||
return null;
|
||||
|
||||
view = go.GetComponent<playerInstantNumbersPrefab>();
|
||||
if (view == null)
|
||||
return null;
|
||||
|
||||
sourcePrefabs[view] = prefab;
|
||||
}
|
||||
else
|
||||
{
|
||||
view.transform.SetParent(parent, false);
|
||||
view.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
view.ResetForReuse();
|
||||
return view;
|
||||
}
|
||||
|
||||
public static bool ReturnInstance(playerInstantNumbersPrefab view)
|
||||
{
|
||||
if (Instance == null || view == null)
|
||||
return false;
|
||||
|
||||
return Instance.ReturnInstanceInternal(view);
|
||||
}
|
||||
|
||||
private bool ReturnInstanceInternal(playerInstantNumbersPrefab view)
|
||||
{
|
||||
if (view == null)
|
||||
return false;
|
||||
|
||||
if (!sourcePrefabs.TryGetValue(view, out var prefab) || prefab == null)
|
||||
return false;
|
||||
|
||||
if (!pools.TryGetValue(prefab, out var pool))
|
||||
{
|
||||
pool = new System.Collections.Generic.Stack<playerInstantNumbersPrefab>();
|
||||
pools[prefab] = pool;
|
||||
}
|
||||
|
||||
view.transform.SetParent(pooledRoot, false);
|
||||
view.ResetForReuse();
|
||||
view.gameObject.SetActive(false);
|
||||
pool.Push(view);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void WarnMissingPrefabOnce()
|
||||
|
||||
+24
-1
@@ -51,14 +51,33 @@ public class playerInstantNumbersPrefab : MonoBehaviour
|
||||
_canvasGroup.alpha = 1f;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (_animationCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_animationCoroutine);
|
||||
_animationCoroutine = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Play(iNumberPrefabController.InstantNumberType type, int signedValue)
|
||||
{
|
||||
ResetForReuse();
|
||||
ApplyVisual(type, signedValue);
|
||||
|
||||
if (_animationCoroutine != null) StopCoroutine(_animationCoroutine);
|
||||
_animationCoroutine = StartCoroutine(Animate());
|
||||
}
|
||||
|
||||
public void ResetForReuse()
|
||||
{
|
||||
if (_canvasGroup != null) _canvasGroup.alpha = 1f;
|
||||
transform.localRotation = Quaternion.identity;
|
||||
transform.localScale = Vector3.one;
|
||||
if (_rectTransform != null) _rectTransform.anchoredPosition = Vector2.zero;
|
||||
else transform.localPosition = Vector3.zero;
|
||||
}
|
||||
|
||||
private void ApplyVisual(iNumberPrefabController.InstantNumberType type, int signedValue)
|
||||
{
|
||||
if (numberText != null)
|
||||
@@ -178,6 +197,10 @@ public class playerInstantNumbersPrefab : MonoBehaviour
|
||||
yield return null;
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
_animationCoroutine = null;
|
||||
if (!iNumberPrefabController.ReturnInstance(this))
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user