You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SnowFlakeBehaviour : MonoBehaviour
{
[AutoUI] public Vector2 Position;
[AutoUI] public float DisappearTime = 5;
[AutoUI] public float DisappearTimeRandomRange = 2;
[AutoUI] public bool ViewCollider = false;
[AutoUI] public Vector2 ColliderPosition = new Vector2(460, 0);
[AutoUI] public Vector2 ColliderSize = new Vector2(400, 400);
[SerializeField] private RectTransform _colliderRectTransform;
private RawImage _colliderImage;
public SnowFlake[] SnowFlakes;
[SerializeField] private SnowFlake _currentFlake;
private RectTransform _rectTransform;
// Start is called before the first frame update
void Start()
{
foreach(var flake in SnowFlakes)
{
flake._snowFlakeBehaviour = this;
}
Play();
_colliderImage = _colliderRectTransform.GetComponent<RawImage>();
_rectTransform = GetComponent<RectTransform>();
}
// Update is called once per frame
void Update()
{
if((Time.frameCount & 0x19) == 0)
{
_colliderImage.color = ViewCollider ? Color.white : new Color(1,1,1,0);
_rectTransform.anchoredPosition = Position;
}
if(ViewCollider)
{
_colliderRectTransform.anchoredPosition = ColliderPosition;
_colliderRectTransform.sizeDelta = ColliderSize;
}
}
IEnumerator WaitToPlay()
{
float waitTime = DisappearTime + Random.Range(-DisappearTimeRandomRange, DisappearTimeRandomRange) / 2.0f;
waitTime = Mathf.Max(0, waitTime);
Debug.Log(waitTime);
yield return new WaitForSeconds(waitTime);
Play();
}
public void Play()
{
_currentFlake = SnowFlakes[Random.Range(0, SnowFlakes.Length)];
Debug.Log(_currentFlake.gameObject.name);
_currentFlake.Play();
}
public void Trigger()
{
_currentFlake.Trigger();
}
public void ResetFlake()
{
StartCoroutine(WaitToPlay());
}
}