当前位置:   article > 正文

Unity实现模拟弹球小游戏过程与收获_unity 2d 模拟踩气球

unity 2d 模拟踩气球

目录

一、游戏截图及源码

1.游戏截图与项目对象构成

2.重要脚本代码

二、C#实现过程的收获

1.项目实现阶段遇到Unity崩溃

2.unity协程使用

3.调整游戏运行速度

4.int与string相互转化

5.数据持久化PlayerPrefs

6.随机量获得与数学调用


一、游戏截图及源码

1.游戏截图与项目对象构成

2.重要脚本代码

2.1createBox.cs创建小球,控制射向速度,设置小球移动过程游戏加速

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class creatBox : MonoBehaviour
  6. {
  7. public Text text_ball_num;
  8. public int enemy_num;
  9. public GameObject m_prefab;
  10. public int ball_number;
  11. public string create_tag;
  12. private int num;
  13. private Vector2 v;
  14. private Rigidbody2D rb;
  15. private float Sw, Sh, Swm;
  16. private int touchCount;
  17. private LineRenderer aimLine;
  18. private Touch touch;
  19. private int key = 1;
  20. private void Start()
  21. {
  22. Sw = Screen.width;
  23. Sh = Screen.height;
  24. Swm = Sw / 2f;
  25. text_ball_num.text = "number:" + ball_number;
  26. num = ball_number;
  27. PlayerPrefs.SetInt("Key", 1);
  28. PlayerPrefs.SetInt("Num", 0);
  29. aimLine = GetComponent<LineRenderer>();
  30. }
  31. private void Update()
  32. {
  33. ball_number = (PlayerPrefs.GetInt("Score") / 5) + 10;
  34. text_ball_num.text = "Ball Number:"+ball_number.ToString();
  35. if(PlayerPrefs.GetInt("Key")==0)
  36. {
  37. if(Input.GetMouseButton(0))
  38. {
  39. Time.timeScale = 2;
  40. }
  41. else
  42. {
  43. Time.timeScale = 1;
  44. }
  45. }
  46. if(PlayerPrefs.GetInt("Key")==1)
  47. {
  48. aimandshoot();
  49. }
  50. if(PlayerPrefs.GetInt("Key")==2)
  51. {
  52. GameObject[] enemy = GameObject.FindGameObjectsWithTag("Enemy");
  53. for(int i=0;i<enemy.Length;i++)
  54. {
  55. enemy[i].transform.position = new Vector3(enemy[i].transform.position.x, enemy[i].transform.position.y - 1, enemy[i].transform.position.z);
  56. }
  57. StartCoroutine(this.transform.gameObject.GetComponent<creatEnemy>().createEnemy(enemy_num));
  58. PlayerPrefs.SetInt("Key", 1);
  59. }
  60. }
  61. private void aimandshoot()
  62. {
  63. if(Input.GetMouseButtonDown(0))
  64. {
  65. aimLine.SetPosition(0, transform.position);
  66. }
  67. if(Input.GetMouseButton(0))
  68. {
  69. touch = Input.GetTouch(0);
  70. if(touch.position.y>=Sh*9f/10f)
  71. {
  72. key = 0;
  73. }
  74. else
  75. {
  76. key = 1;
  77. }
  78. float x, y, z, deg;
  79. x = touch.position.x - Swm;
  80. y = touch.position.y;
  81. z = Mathf.Abs(x) / y;
  82. deg = (int)(Mathf.Atan(z) * 180 / 3.1415);
  83. v.x = x;
  84. v.y = y;
  85. v.Normalize();
  86. v.x = v.x * 20;
  87. v.y = v.y * 20;
  88. if (x < 0)
  89. {
  90. transform.rotation = Quaternion.AngleAxis(deg, Vector3.forward);
  91. }
  92. else
  93. {
  94. transform.rotation = Quaternion.AngleAxis(-deg, Vector3.forward);
  95. }
  96. aimLine.SetPosition(1, new Vector3(transform.position.x + (touch.position.x - Swm) / Swm, transform.position.y + touch.position.y / Swm, transform.position.z));
  97. }
  98. if(Input.GetMouseButtonUp(0))
  99. {
  100. aimLine.SetPosition(1, transform.position);
  101. if(key==1)
  102. {
  103. StartCoroutine(shoot());
  104. PlayerPrefs.SetInt("Key", 0);
  105. }
  106. }
  107. }
  108. IEnumerator shoot()
  109. {
  110. for(int i=0;i<ball_number;i++)
  111. {
  112. rb = GameObject.Instantiate(m_prefab, transform.position, Quaternion.identity).GetComponent<Rigidbody2D>();
  113. rb.velocity = v;
  114. PlayerPrefs.SetInt("Num", PlayerPrefs.GetInt("Num") + 1);
  115. yield return new WaitForSeconds(0.1f);
  116. }
  117. }
  118. }

2.2createEnemy.cs创建障碍物,随机生成障碍物元素

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class creatEnemy : MonoBehaviour
  6. {
  7. public Text text_score;
  8. public GameObject LU;
  9. public GameObject LD;
  10. public GameObject RU;
  11. public GameObject RD;
  12. public GameObject m_prefab1;
  13. public GameObject m_prefab2;
  14. public GameObject m_prefab3;
  15. public GameObject m_prefab4;
  16. public GameObject m_prefab5;
  17. public GameObject m_prefab6;
  18. private float size_x;
  19. private float size_y;
  20. private int x, y;
  21. private HashSet<Vector2> enemy_positions = new HashSet<Vector2>();
  22. void Start()
  23. {
  24. GameObject t = GameObject.Instantiate(m_prefab1, transform.position, Quaternion.identity);
  25. t.transform.position.Set(this.transform.position.x,this.transform.position.y,this.transform.position.z);
  26. size_x = t.transform.GetComponent<Renderer>().bounds.size.x;
  27. size_y = t.transform.GetComponent<Renderer>().bounds.size.y;
  28. GameObject.Destroy(t.transform.gameObject, 0f);
  29. x = (int)((RU.transform.position.x - LU.transform.position.x) / size_x);
  30. y = (int)((RU.transform.position.y - RD.transform.position.y) / size_y);
  31. CreateEnemy(5);
  32. text_score.text = "0";
  33. PlayerPrefs.SetInt("Score", 0);
  34. }
  35. public void CreateEnemy(int cnt)
  36. {
  37. for (int i = 0; i < cnt; i++)
  38. {
  39. randomcreate();
  40. }
  41. }
  42. private void Update()
  43. {
  44. text_score.text = PlayerPrefs.GetInt("Score").ToString();
  45. }
  46. public IEnumerator createEnemy(int cnt)
  47. {
  48. for (int i = 0; i < cnt; i++)
  49. {
  50. randomcreate();
  51. yield return null;
  52. }
  53. }
  54. private void randomcreate()
  55. {
  56. Vector2 tmp = new Vector2();
  57. do
  58. {
  59. tmp.Set(Random.Range(LU.transform.position.x, RU.transform.position.x), Random.Range((3 * LU.transform.position.y + LD.transform.position.y) / 4, LU.transform.position.y));
  60. } while (enemy_positions.Contains(tmp));
  61. enemy_positions.Add(tmp);
  62. int j = Random.Range(1, 6);
  63. GameObject m;
  64. if (j == 1)
  65. {
  66. m = GameObject.Instantiate(m_prefab1, transform.position, Quaternion.identity);
  67. }
  68. else if (j == 2)
  69. {
  70. m = GameObject.Instantiate(m_prefab2, transform.position, Quaternion.identity);
  71. }
  72. else if (j == 2)
  73. {
  74. m = GameObject.Instantiate(m_prefab3, transform.position, Quaternion.identity);
  75. }
  76. else if (j == 2)
  77. {
  78. m = GameObject.Instantiate(m_prefab4, transform.position, Quaternion.identity);
  79. }
  80. else if (j == 2)
  81. {
  82. m = GameObject.Instantiate(m_prefab5, transform.position, Quaternion.identity);
  83. }
  84. else
  85. {
  86. m = GameObject.Instantiate(m_prefab6, transform.position, Quaternion.identity);
  87. }
  88. m.transform.position = new Vector3(tmp.x, tmp.y, this.transform.position.z);
  89. m.GetComponent<CreateOrDestroy>().setLive(Random.Range(5 * PlayerPrefs.GetInt("Score")+1, 10 * PlayerPrefs.GetInt("Score") + 10));
  90. m.GetComponent<Renderer>().material.color = new Color(Random.Range(0, 255) / 255f, Random.Range(0, 255) / 255f, Random.Range(0, 255) / 255f);
  91. }
  92. }

二、C#实现过程的收获

1.项目实现阶段遇到Unity崩溃

根据网络上言论,通过打开Temp/_Backupscenes/0.backup文件修改后缀可以复原原来场景。这个方法未证实,但是确定了一件事:崩溃后重新打开unity后一切就没救了,所以一定要先尝试上面的方法后打开unity

2.unity协程使用

2.1基本函数:StartCoroutine(),以IEnumerator为返回值的函数

2.2代码示例:

  1. //StartCoroutine(shoot());
  2. IEnumerator shoot()
  3. {
  4. for(int i=0;i<ball_number;i++)
  5. {
  6. rb = GameObject.Instantiate(m_prefab, transform.position, Quaternion.identity).GetComponent<Rigidbody2D>();
  7. rb.velocity = v;
  8. PlayerPrefs.SetInt("Num", PlayerPrefs.GetInt("Num") + 1);
  9. yield return new WaitForSeconds(0.1f);
  10. }
  11. }

2.3知识点:开始协程函数调用以IEnumerator为返回值的函数,此函数中返回值通过yield return来达到返回要求,事实上yield return的含义是每次运行函数前先运行一遍return后面的代码,示例代码中函数作用为延迟0.1s

3.调整游戏运行速度

3.1需要通过控制Time.timeScale的值,0:暂停、1:正常速度运行、2:2倍速运行

4.int与string相互转化

4.1int->string:调用int对象函数tosting()

4.2string->int:调用Int.Parse(string)函数

5.数据持久化PlayerPrefs

5.1通过PlayerPrefs实现不同脚本简单数据共享

5.2方便设计使用状态机


个人博客:布纸刀 (gitee.io)

希望大家一起学习,共同进步,如果有疑问或错误欢迎联系笔者。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/146358
推荐阅读
相关标签
  

闽ICP备14008679号