CreateStone.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CreateStone : MonoBehaviour {
  5. //创建单例
  6. public static CreateStone instance;
  7. private GameObject stone;
  8. private GameObject prop;
  9. private void Awake()
  10. {
  11. instance = this;
  12. }
  13. // Use this for initialization
  14. void Start () {
  15. //找到预设物
  16. stone = Resources.Load<GameObject>("Prefabs/Stone");
  17. prop = Resources.Load<GameObject>("Prefabs/Prop");
  18. //InvokeRepeating("createStone",1f,1f);
  19. }
  20. // Update is called once per frame
  21. void Update () {
  22. }
  23. //生成石头 x轴生成位置在一定范围内随机 y z坐标固定
  24. public void createStone()
  25. {
  26. GameObject go = Instantiate(stone);
  27. go.transform.position = new Vector3(Random.Range(-3.37f, 3.64f), Random.Range(0.595f,2.595f), -11.34f);
  28. }
  29. //生成道具 随机生成1-100的数字 如果num大于等于80 道具生成 位置跟石头位置一样
  30. public void createProp()
  31. {
  32. int num = Random.Range(1,100);
  33. if (num>=80)
  34. {
  35. GameObject go = Instantiate(prop);
  36. go.transform.position = new Vector3(Random.Range(-3.37f, 3.64f), Random.Range(0.595f, 2.595f), -11.34f);
  37. }
  38. }
  39. //停止石头和道具的生成 游戏结束时调用
  40. public void stopStone()
  41. {
  42. CancelInvoke("createStone");
  43. }
  44. public void stopProp()
  45. {
  46. CancelInvoke("createProp");
  47. }
  48. }