| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Box : MonoBehaviour
- {
- [System.NonSerialized] public int bombDivideCount = 3;
- float boxWidth;
- float parentHeight;
- void Start()
- {
- boxWidth = (transform as RectTransform).rect.width;
- parentHeight = (transform.parent as RectTransform).rect.height;
- GetComponent<BoxCollider2D>().size = Vector2.one * boxWidth;
- }
- // Update is called once per frame
- void Update()
- {
- transform.localPosition += Vector3.down * GameMode.boxDownSpeed * Time.deltaTime;
- if (transform.localPosition.y < -parentHeight / 2 + boxWidth / 2) Bomb();
- }
- bool hasBomb = false;
- public void Bomb()
- {
- if (hasBomb) return;
- hasBomb = true;
- RectTransform boxRTF = transform as RectTransform;
- float boxWidth = boxRTF.rect.width;
- //左下角开始
- Vector2 childStartPoint = new Vector2();
- childStartPoint.x = childStartPoint.y = -(boxWidth/2)+(boxWidth/bombDivideCount)/2;
- float childWidth = boxWidth / bombDivideCount;
- GameObject childPrefab = transform.GetChild(0).gameObject;
- for (int x = 0; x < bombDivideCount; x++)
- {
- for (int y = 0; y < bombDivideCount; y++)
- {
- Vector2 pos = new Vector2(childStartPoint.x + childWidth * x, childStartPoint.y + childWidth * y);
- RectTransform childRTF = Instantiate(childPrefab, boxRTF).transform as RectTransform;
- childRTF.localPosition = pos;
- childRTF.sizeDelta = new Vector2(childWidth, childWidth);
- childRTF.gameObject.SetActive(true);
- BombChild bombChild = childRTF.gameObject.AddComponent<BombChild>();
- Vector2 pointer = (bombChild.transform.position - transform.position).normalized;
- if (pointer.Equals(Vector2.zero))
- {
- Destroy(childRTF.gameObject);
- continue;
- }
- bombChild.velocity = pointer * Random.Range(600, 800);
- childRTF.gameObject.transform.SetParent(transform.parent);
- }
- }
- Destroy(gameObject);
- }
- }
|