备份CatanBuilding瘦身独立工程
This commit is contained in:
70
Assets/Scripts/EventPinball/tips/FloatingText.cs
Normal file
70
Assets/Scripts/EventPinball/tips/FloatingText.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
namespace game
|
||||
{
|
||||
[RequireComponent(typeof(TextMeshPro))]
|
||||
public class FloatingText : MonoBehaviour
|
||||
{
|
||||
[Header("Type")]
|
||||
public EventPinballBonusType FloatingTextType;
|
||||
|
||||
[Header("Movement Settings")]
|
||||
public float moveSpeed = 1f;
|
||||
public float fadeDuration = 1f;
|
||||
|
||||
[Header("Renderer")]
|
||||
public int OrderInLayer = 3;
|
||||
|
||||
private TextMeshPro textMesh;
|
||||
private float currentAlpha = 1f;
|
||||
private float timer = 0f;
|
||||
private Vector3 moveDirection = Vector3.up;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
textMesh = GetComponent<TextMeshPro>();
|
||||
|
||||
var renderer = GetComponent<Renderer>();
|
||||
renderer.sortingOrder = OrderInLayer;
|
||||
}
|
||||
|
||||
public void Initialize(string text, Vector3 position)
|
||||
{
|
||||
transform.position = position;
|
||||
textMesh.text = text;
|
||||
|
||||
currentAlpha = 1f;
|
||||
timer = 0f;
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// 移动
|
||||
transform.position += moveDirection * moveSpeed * Time.deltaTime;
|
||||
|
||||
// 淡出
|
||||
timer += Time.deltaTime;
|
||||
if (timer >= fadeDuration)
|
||||
{
|
||||
if (FloatingTextType == EventPinballBonusType.TypeScore)
|
||||
{
|
||||
FloatingTextPool.Instance.ReturnToScorePool(this);
|
||||
}
|
||||
else if (FloatingTextType == EventPinballBonusType.TypeMultiple)
|
||||
{
|
||||
FloatingTextPool.Instance.ReturnToMultiplePool(this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
currentAlpha = Mathf.Lerp(1f, 0f, timer / fadeDuration);
|
||||
var color = textMesh.color;
|
||||
color.a = currentAlpha;
|
||||
textMesh.color = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/EventPinball/tips/FloatingText.cs.meta
Normal file
11
Assets/Scripts/EventPinball/tips/FloatingText.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fce2a3c44f5cc2d4ead5a2ced75e2732
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
106
Assets/Scripts/EventPinball/tips/FloatingTextPool.cs
Normal file
106
Assets/Scripts/EventPinball/tips/FloatingTextPool.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace game
|
||||
{
|
||||
public class FloatingTextPool : MonoBehaviour
|
||||
{
|
||||
public static FloatingTextPool Instance { get; private set; }
|
||||
|
||||
[SerializeField] private GameObject floatingScoreTextPrefab;
|
||||
[SerializeField] private GameObject floatingMultipleTextPrefab;
|
||||
[SerializeField] private int initialScorePoolSize = 12;
|
||||
[SerializeField] private int initialMultiplePoolSize = 2;
|
||||
|
||||
private Queue<FloatingText> m_ScorePool = new Queue<FloatingText>();
|
||||
private Queue<FloatingText> m_MultiplePool = new Queue<FloatingText>();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
InitializePool();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
m_ScorePool.Clear();
|
||||
m_ScorePool = null;
|
||||
|
||||
m_MultiplePool.Clear();
|
||||
m_MultiplePool = null;
|
||||
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
private void InitializePool()
|
||||
{
|
||||
for (int i = 0; i < initialScorePoolSize; i++)
|
||||
{
|
||||
CreateNewScoreTextObject();
|
||||
}
|
||||
|
||||
for (int i = 0; i < initialMultiplePoolSize; i++)
|
||||
{
|
||||
CreateNewMultipleTextObject();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateNewScoreTextObject()
|
||||
{
|
||||
var obj = Instantiate(floatingScoreTextPrefab, transform);
|
||||
var floatingText = obj.GetComponent<FloatingText>();
|
||||
floatingText.gameObject.SetActive(false);
|
||||
m_ScorePool.Enqueue(floatingText);
|
||||
}
|
||||
|
||||
private void CreateNewMultipleTextObject()
|
||||
{
|
||||
var obj = Instantiate(floatingMultipleTextPrefab, transform);
|
||||
var floatingText = obj.GetComponent<FloatingText>();
|
||||
floatingText.gameObject.SetActive(false);
|
||||
m_MultiplePool.Enqueue(floatingText);
|
||||
}
|
||||
|
||||
public FloatingText GetFloatingScoreText()
|
||||
{
|
||||
if (m_ScorePool.Count == 0)
|
||||
{
|
||||
CreateNewScoreTextObject();
|
||||
}
|
||||
|
||||
var text = m_ScorePool.Dequeue();
|
||||
text.gameObject.SetActive(true);
|
||||
return text;
|
||||
}
|
||||
|
||||
public FloatingText GetFloatingMultipleText()
|
||||
{
|
||||
if (m_ScorePool.Count == 0)
|
||||
{
|
||||
CreateNewMultipleTextObject();
|
||||
}
|
||||
|
||||
var text = m_MultiplePool.Dequeue();
|
||||
text.gameObject.SetActive(true);
|
||||
return text;
|
||||
}
|
||||
|
||||
public void ReturnToScorePool(FloatingText text)
|
||||
{
|
||||
text.gameObject.SetActive(false);
|
||||
m_ScorePool.Enqueue(text);
|
||||
}
|
||||
public void ReturnToMultiplePool(FloatingText text)
|
||||
{
|
||||
text.gameObject.SetActive(false);
|
||||
m_MultiplePool.Enqueue(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/EventPinball/tips/FloatingTextPool.cs.meta
Normal file
11
Assets/Scripts/EventPinball/tips/FloatingTextPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 333a4cb1ba0446646b7439ef7923e586
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user