popcorn/popcorn/Assets/MyGame/Scripts/CoinManager.cs

71 lines
2.2 KiB
C#
Raw Normal View History

2021-07-16 05:06:57 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
2021-08-06 05:36:06 +00:00
using TMPro;
2021-07-16 05:06:57 +00:00
using UnityEngine;
using UnityEngine.UI;
public class CoinManager : SingletonMonoBehaviour<CoinManager>
{
2021-08-05 00:20:09 +00:00
private static readonly int Add = Animator.StringToHash("Add");
2021-07-16 05:06:57 +00:00
// CoinView
[SerializeField] private RectTransform coinIconTransform;
[SerializeField] private TextWithCountUpInt coinCountText;
[SerializeField] private Animator coinAnimator;
[SerializeField] private RectTransform coinPrefab;
// Animation
[SerializeField] private float duration = 0.5f;
[SerializeField] private RectTransform rootTransform;
private int ownCoin;
public int OwnCoin => ownCoin;
private string coinTextFormat = "{0}";
2021-08-05 00:20:09 +00:00
2021-07-16 05:06:57 +00:00
void Awake(){
if(CheckInstance()) return ;
}
public void ChangeCoin(int count)
{
ownCoin = count;
coinCountText.ChangeValue(coinTextFormat, ownCoin);
}
public void AddCoin(int count)
{
ownCoin += count;
coinCountText.CountUpAnimation(coinTextFormat, ownCoin, duration);
}
public void AddCoinWithEffect(int count, Action callback)
{
ownCoin += count;
// 生成枚数決定
// 生成ポジション決め、生成
2021-08-10 06:44:44 +00:00
InstantiateEffeect(coinPrefab, count, Vector3.zero, coinIconTransform.position, () =>
2021-07-16 05:06:57 +00:00
{
2021-08-10 06:44:44 +00:00
coinCountText.CountUpAnimation(coinTextFormat, ownCoin, duration + count * 0.1f);
2021-07-16 05:06:57 +00:00
callback.Invoke();
});
}
private void InstantiateEffeect(RectTransform prefab, int count, Vector3 from, Vector3 to, Action callback)
{
2021-08-05 00:20:09 +00:00
var effect = Instantiate(prefab, Vector3.zero, Quaternion.identity, rootTransform);
2021-08-06 05:36:06 +00:00
effect.GetComponentInChildren<TextMeshProUGUI>().text = count.ToString();
2021-08-05 00:20:09 +00:00
// アニメ50フレーム
this.CallWaitForSeconds(1.0f, () => {
// コインが到着したらSEとバイブ再生。コインはずみアニメーションを再生
VibrationManager.Instance.PlayVibrationOnceWeak();
coinAnimator.SetTrigger(Add);
// SoundManager.Instance.PlaySE("se_coin_count");
// コインは削除
Destroy(effect.gameObject);
callback.Invoke();
});
2021-07-16 05:06:57 +00:00
}
}