1. using UnityEngine;
  2. using System;
  3.  
  4. namespace RoadCrossing.Types
  5. {
  6. /// <summary>
  7. /// 小路
  8. /// </summary>
  9. [Serializable]
  10. public class Lane
  11. {
  12. /// <summary>
  13. /// 小路相关的游戏物体
  14. /// </summary>
  15. public Transform laneObject;
  16. /// <summary>
  17. /// 生成这条路的几率
  18. /// </summary>
  19. ;
  20. /// <summary>
  21. /// 小路的宽度
  22. /// </summary>
  23. ;
  24. /// <summary>
  25. /// 生成物品的几率
  26. /// </summary>
  27. ;
  28. }
  29. }

Lane

  1. using UnityEngine;
  2. using System;
  3. namespace RoadCrossing.Types
  4. {
  5. /// <summary>
  6. /// 掉落物品
  7. /// </summary>
  8. [Serializable]
  9. public class ObjectDrop
  10. {
  11. /// <summary>
  12. /// 掉落物品
  13. /// </summary>
  14. public Transform droppedObject;
  15.  
  16. /// <summary>
  17. /// 掉落几率
  18. /// </summary>
  19. ;
  20. }
  21. }

ObjectDrop

  1. using UnityEngine;
  2. using System;
  3.  
  4. namespace RoadCrossing.Types
  5. {
  6. /// <summary>
  7. /// 商店道具
  8. /// </summary>
  9. [Serializable]
  10. public class ShopItem
  11. {
  12. /// <summary>
  13. /// 这个道具关联的按钮
  14. /// </summary>
  15. public Transform itemButton;
  16.  
  17. /// <summary>
  18. /// 解锁状态,0 未解锁 1 已解锁
  19. /// </summary>
  20. ;
  21.  
  22. /// <summary>
  23. /// 解锁这个道具的花费
  24. /// </summary>
  25. ;
  26.  
  27. /// <summary>
  28. /// 这个道具在存档中的键名
  29. /// </summary>
  30. public string playerPrefsName = "FroggyUnlock";
  31. }
  32. }

ShopItem

  1. using System;
  2.  
  3. namespace RoadCrossing.Types
  4. {
  5. /// <summary>
  6. /// 接触后调用的函数信息
  7. /// </summary>
  8. [Serializable]
  9. public class TouchFunction
  10. {
  11. /// <summary>
  12. /// 调用的函数名称
  13. /// </summary>
  14. public string functionName = "CancelMove";
  15.  
  16. /// <summary>
  17. /// 在哪个目标上调用函数
  18. /// </summary>
  19. public string targetTag = "Player";
  20.  
  21. /// <summary>
  22. /// 传递给函数的参数
  23. /// </summary>
  24. ;
  25. }
  26. }

TouchFunction

  1. using UnityEngine;
  2. using RoadCrossing.Types;
  3.  
  4. namespace RoadCrossing
  5. {
  6. /// <summary>
  7. /// 能与玩家交互的块,可以是岩石,墙,敌人,金币
  8. /// </summary>
  9. public class RCGBlock : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// 能够接触这个块的物体的标签
  13. /// </summary>
  14. public string touchTargetTag = "Player";
  15.  
  16. /// <summary>
  17. /// 这个块被目标接触后调用的函数列表
  18. /// </summary>
  19. public TouchFunction[] touchFunctions;
  20.  
  21. /// <summary>
  22. /// 经过多少次接触后删除这个物体
  23. /// </summary>
  24. ;
  25. /// <summary>
  26. /// 这个物体是否可以被移除
  27. /// </summary>
  28. internal bool isRemovable = false;
  29.  
  30. /// <summary>
  31. /// 当这个物体被接触后的动画剪辑
  32. /// </summary>
  33. public AnimationClip hitAnimation;
  34.  
  35. /// <summary>
  36. /// 当这个物体被接触后的声音剪辑
  37. /// </summary>
  38. public AudioClip soundHit;
  39.  
  40. /// <summary>
  41. /// AudioSource所属物体的标签
  42. /// </summary>
  43. public string soundSourceTag = "GameController";
  44.  
  45. /// <summary>
  46. /// 物体死亡后创建的死亡的特效
  47. /// </summary>
  48. public Transform deathEffect;
  49.  
  50. void Start()
  51. {
  52. )
  53. isRemovable = true;
  54. }
  55.  
  56. void OnTriggerEnter(Collider other)
  57. {
  58. if( other.tag == touchTargetTag )
  59. {
  60. //调用目标物体上的函数
  61. foreach( var touchFunction in touchFunctions )
  62. {
  63. if( touchFunction.targetTag != string.Empty && touchFunction.functionName != string.Empty )
  64. {
  65. GameObject.FindGameObjectWithTag(touchFunction.targetTag).SendMessage(touchFunction.functionName, touchFunction.functionParameter);
  66. }
  67. }
  68.  
  69. //满足条件,播放动画
  70. if( GetComponent<Animation>() && hitAnimation )
  71. {
  72. GetComponent<Animation>().Stop();
  73. GetComponent<Animation>().Play(hitAnimation.name);
  74. }
  75.  
  76. //满足条件,删除物体,创建死亡特效
  77. if( isRemovable == true )
  78. {
  79. removeAfterTouches--;
  80.  
  81. )
  82. {
  83. if( deathEffect )
  84. Instantiate(deathEffect, transform.position, Quaternion.identity);
  85.  
  86. Destroy(gameObject);
  87. }
  88. }
  89.  
  90. //满足条件,播放声音
  91. if( soundSourceTag != string.Empty && soundHit )
  92. GameObject.FindGameObjectWithTag(soundSourceTag).GetComponent<AudioSource>().PlayOneShot(soundHit);
  93. }
  94. }
  95. }
  96. }

RCGBlock

  1. using UnityEngine;
  2.  
  3. namespace RoadCrossing
  4. {
  5. /// <summary>
  6. /// 控制上下左右点击的按钮(collider)
  7. /// </summary>
  8. public class RCGButtonFunction : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 要执行函数的物体
  12. /// </summary>
  13. public Transform functionTarget;
  14.  
  15. /// <summary>
  16. /// 要执行的函数名称
  17. /// </summary>
  18. public string functionName;
  19.  
  20. /// <summary>
  21. /// 传递给函数的参数
  22. /// </summary>
  23. public string functionParameter;
  24.  
  25. /// <summary>
  26. /// 点击声音
  27. /// </summary>
  28. public AudioClip soundClick;
  29. /// <summary>
  30. /// AudioSource 所属的物体
  31. /// </summary>
  32. public Transform soundSource;
  33.  
  34. void OnMouseDrag()
  35. {
  36. ExecuteFunction();
  37. }
  38.  
  39. /// <summary>
  40. /// 执行函数
  41. /// </summary>
  42. void ExecuteFunction()
  43. {
  44. if( soundSource )
  45. if( soundSource.GetComponent<AudioSource>() )
  46. soundSource.GetComponent<AudioSource>().PlayOneShot(soundClick);
  47.  
  48. if( functionName != string.Empty )
  49. {
  50. if( functionTarget )
  51. {
  52. functionTarget.SendMessage(functionName, functionParameter);
  53. }
  54. }
  55. }
  56. }
  57. }

RCGButtonFunction

  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using RoadCrossing.Types;
  5.  
  6. namespace RoadCrossing
  7. {
  8.  
  9. /// <summary>
  10. /// 游戏控制
  11. /// </summary>
  12. public class RCGGameController : MonoBehaviour
  13. {
  14. /// <summary>
  15. /// 玩家物体列表
  16. /// </summary>
  17. public Transform[] playerObjects;
  18. /// <summary>
  19. /// 当前玩家物体索引
  20. /// </summary>
  21. public int currentPlayer;
  22.  
  23. /// <summary>
  24. /// 跟随玩家的相机
  25. /// </summary>
  26. public Transform cameraObject;
  27.  
  28. /// <summary>
  29. /// 控制玩家移动的按钮(collider),如果是滑动控制,这些物体会被禁用
  30. /// </summary>
  31. public Transform moveButtonsObject;
  32.  
  33. /// <summary>
  34. /// 是否使用滑动控制
  35. /// </summary>
  36. public bool swipeControls = false;
  37.  
  38. /// <summary>
  39. /// 滑动起点
  40. /// </summary>
  41. internal Vector3 swipeStart;
  42. /// <summary>
  43. /// 滑动终点
  44. /// </summary>
  45. internal Vector3 swipeEnd;
  46.  
  47. ;
  48.  
  49. /// <summary>
  50. /// 滑动命令取消前的等待时间
  51. /// </summary>
  52. ;
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. internal float swipeTimeoutCount;
  57.  
  58. /// <summary>
  59. /// 所有的小路预设列表
  60. /// </summary>
  61. public Lane[] lanes;
  62. internal Lane[] lanesList;
  63.  
  64. public ObjectDrop[] objectDrops;
  65. internal Transform[] objectDropList;
  66.  
  67. ;
  68.  
  69. /// <summary>
  70. /// 游戏开始前与预创建的小路数量
  71. /// </summary>
  72. ;
  73. /// <summary>
  74. /// 下一条小路的位置
  75. /// </summary>
  76. ;
  77.  
  78. /// <summary>
  79. /// 得分
  80. /// </summary>
  81. ;
  82. /// <summary>
  83. /// 显示得分的文本
  84. /// </summary>
  85. public Transform scoreText;
  86. /// <summary>
  87. /// 最高分数
  88. /// </summary>
  89. ;
  90.  
  91. /// <summary>
  92. /// 游戏中总共获得的分数
  93. /// </summary>
  94. public string coinsPlayerPrefs = "Coins";
  95.  
  96. /// <summary>
  97. /// 游戏速度
  98. /// </summary>
  99. ;
  100.  
  101. /// <summary>
  102. /// 玩家升级需要的点数
  103. /// </summary>
  104. ;
  105. /// <summary>
  106. /// 增加的分数
  107. /// </summary>
  108. ;
  109.  
  110. /// <summary>
  111. /// 在玩家身后追逐玩家的物体
  112. /// </summary>
  113. public Transform deathLineObject;
  114. /// <summary>
  115. /// 追杀物体的移动速度
  116. /// </summary>
  117. ;
  118. /// <summary>
  119. /// 追杀物体每次增加的速度的值
  120. /// </summary>
  121. ;
  122. /// <summary>
  123. /// 追杀物体的最大移动速度
  124. /// </summary>
  125. public float deathLineSpeedMax = 1.5f;
  126.  
  127. /// <summary>
  128. /// 游戏面板
  129. /// </summary>
  130. public Transform gameCanvas;
  131. /// <summary>
  132. /// 暂停面板
  133. /// </summary>
  134. public Transform pauseCanvas;
  135. /// <summary>
  136. /// 游戏结束面板
  137. /// </summary>
  138. public Transform gameOverCanvas;
  139.  
  140. /// <summary>
  141. /// 游戏是否结束
  142. /// </summary>
  143. internal bool isGameOver = false;
  144.  
  145. /// <summary>
  146. /// 主目录场景
  147. /// </summary>
  148. public string mainMenuLevelName = "MainMenu";
  149.  
  150. /// <summary>
  151. /// 升级声音
  152. /// </summary>
  153. public AudioClip soundLevelUp;
  154. /// <summary>
  155. /// 游戏结束声音
  156. /// </summary>
  157. public AudioClip soundGameOver;
  158.  
  159. /// <summary>
  160. /// 使用AudioSource的物体的标签
  161. /// </summary>
  162. public string soundSourceTag = "GameController";
  163. /// <summary>
  164. /// 确认按钮
  165. /// </summary>
  166. public string confirmButton = "Submit";
  167.  
  168. //暂停按钮
  169. public string pauseButton = "Cancel";
  170. /// <summary>
  171. /// 游戏是否暂停
  172. /// </summary>
  173. internal bool isPaused = false;
  174. /// <summary>
  175. ///
  176. /// </summary>
  177. ;
  178.  
  179. void Start()
  180. {
  181. ChangeScore();
  182.  
  183. //隐藏游戏结束面板
  184. if( gameOverCanvas )
  185. gameOverCanvas.gameObject.SetActive(false);
  186.  
  187. //获取存档中的最高分数
  188. highScore = PlayerPrefs.GetInt(Application.loadedLevelName + );
  189.  
  190. ;
  191. ;
  192.  
  193. ; index < lanes.Length; index++)
  194. {
  195. totalLanes += lanes[index].laneChance;
  196. }
  197.  
  198. lanesList = new Lane[totalLanes];
  199.  
  200. ; index < lanes.Length; index++)
  201. {
  202. ;
  203.  
  204. while( laneChanceCount < lanes[index].laneChance )
  205. {
  206. lanesList[totalLanesIndex] = lanes[index];
  207.  
  208. laneChanceCount++;
  209.  
  210. totalLanesIndex++;
  211. }
  212. }
  213.  
  214. ;
  215. ;
  216.  
  217. ; index < objectDrops.Length; index++)
  218. {
  219. totalDrops += objectDrops[index].dropChance;
  220. }
  221.  
  222. objectDropList = new Transform[totalDrops];
  223.  
  224. ; index < objectDrops.Length; index++)
  225. {
  226. ;
  227.  
  228. while( dropChanceCount < objectDrops[index].dropChance )
  229. {
  230. objectDropList[totalDropsIndex] = objectDrops[index].droppedObject;
  231.  
  232. dropChanceCount++;
  233.  
  234. totalDropsIndex++;
  235. }
  236. }
  237.  
  238. //从存档中获取当前玩家
  239. currentPlayer = PlayerPrefs.GetInt("CurrentPlayer", currentPlayer);
  240.  
  241. SetPlayer(currentPlayer);
  242.  
  243. if( cameraObject == null )
  244. cameraObject = GameObject.FindGameObjectWithTag("MainCamera").transform;
  245.  
  246. CreateLane(precreateLanes);
  247.  
  248. Pause();
  249. }
  250.  
  251. void Update()
  252. {
  253. if( isGameOver == true )
  254. {
  255. if( Input.GetButtonDown(confirmButton) )
  256. {
  257. Restart();
  258. }
  259.  
  260. if( Input.GetButtonDown(pauseButton) )
  261. {
  262. MainMenu();
  263. }
  264. }
  265. else
  266. {
  267. if( Input.GetButtonDown(pauseButton) )
  268. {
  269. if( isPaused == true )
  270. Unpause();
  271. else
  272. Pause();
  273. }
  274.  
  275. if ( swipeControls == true )
  276. {
  277. ) swipeTimeoutCount -= Time.deltaTime;
  278.  
  279. foreach ( Touch touch in Input.touches )
  280. {
  281. if ( touch.phase == TouchPhase.Began )
  282. {
  283. swipeStart = touch.position;
  284. swipeEnd = touch.position;
  285.  
  286. swipeTimeoutCount = swipeTimeout;
  287. }
  288.  
  289. if ( touch.phase == TouchPhase.Moved )
  290. {
  291. swipeEnd = touch.position;
  292. }
  293.  
  294. )
  295. {
  296. if( (swipeStart.x - swipeEnd.x) > swipeDistance && (swipeStart.y - swipeEnd.y) < -swipeDistance )
  297. {
  298. MovePlayer("left");
  299. }
  300. else if((swipeStart.x - swipeEnd.x) < -swipeDistance && (swipeStart.y - swipeEnd.y) > swipeDistance )
  301. {
  302. MovePlayer("right");
  303. }
  304. else if((swipeStart.y - swipeEnd.y) < -swipeDistance && (swipeStart.x - swipeEnd.x) < -swipeDistance )
  305. {
  306. MovePlayer("forward");
  307. }
  308. else if((swipeStart.y - swipeEnd.y) > swipeDistance && (swipeStart.x - swipeEnd.x) > swipeDistance )
  309. {
  310. MovePlayer("backward");
  311. }
  312. }
  313. }
  314. }
  315. }
  316.  
  317. if( nextLanePosition - cameraObject.position.x < precreateLanes )
  318. CreateLane();
  319.  
  320. if( cameraObject )
  321. {
  322. if( playerObjects[currentPlayer] )
  323. cameraObject.position = ), , Mathf.Lerp(cameraObject.position.z, playerObjects[currentPlayer].position.z, Time.deltaTime * ));
  324.  
  325. if( deathLineObject )
  326. {
  327. Vector3 newVector3 = deathLineObject.position;
  328.  
  329. if( cameraObject.position.x > deathLineObject.position.x )
  330. newVector3.x = cameraObject.position.x;
  331.  
  332. if( isGameOver == false )
  333. newVector3.x += deathLineSpeed * Time.deltaTime;
  334.  
  335. deathLineObject.position = newVector3;
  336. }
  337. }
  338. }
  339.  
  340. /// <summary>
  341. /// 创建小路
  342. /// </summary>
  343. /// <param name="laneCount"小路数量></param>
  344. void CreateLane(int laneCount)
  345. {
  346. )
  347. {
  348. laneCount--;
  349.  
  350. , lanesList.Length));
  351.  
  352. Transform newLane = Instantiate(lanesList[randomLane].laneObject, , ), Quaternion.identity) as Transform;
  353.  
  354. if( Random.value < lanesList[randomLane].itemChance )
  355. {
  356. Transform newObject = Instantiate(objectDropList[Mathf.FloorToInt(Random.Range(, objectDropList.Length))]) as Transform;
  357.  
  358. Vector3 newVector = new Vector3();
  359.  
  360. newVector = newLane.position;
  361.  
  362. newVector.z += Mathf.Round(Random.Range(-objectDropOffset, objectDropOffset));
  363.  
  364. newObject.position = newVector;
  365. }
  366.  
  367. nextLanePosition += lanesList[randomLane].laneWidth;
  368. }
  369. }
  370.  
  371. /// <summary>
  372. /// 改变分数
  373. /// </summary>
  374. /// <param name="changeValue">改变的分数</param>
  375. void ChangeScore(int changeValue)
  376. {
  377.  
  378. score += changeValue;
  379.  
  380. //更新文本的分数显示
  381. if( scoreText )
  382. scoreText.GetComponent<Text>().text = score.ToString();
  383.  
  384. increaseCount += changeValue;
  385.  
  386. //达到升级的条件,升级
  387. if( increaseCount >= levelUpEveryCoins )
  388. {
  389. increaseCount -= levelUpEveryCoins;
  390.  
  391. LevelUp();
  392. }
  393. }
  394.  
  395. /// <summary>
  396. /// 升级,并增加游戏难度
  397. /// </summary>
  398. void LevelUp()
  399. {
  400. //如果追杀物没有到达最大速度,增加它的速度
  401. if( deathLineSpeed + deathLineSpeedIncrease < deathLineSpeedMax )
  402. deathLineSpeed += deathLineSpeedIncrease;
  403.  
  404. //播放升级声音
  405. if( soundSourceTag != string.Empty && soundLevelUp )
  406. GameObject.FindGameObjectWithTag(soundSourceTag).GetComponent<AudioSource>().PlayOneShot(soundLevelUp);
  407. }
  408.  
  409. /// <summary>
  410. /// 暂停游戏
  411. /// </summary>
  412. void Pause()
  413. {
  414. isPaused = true;
  415.  
  416. Time.timeScale = ;
  417.  
  418. //显示暂停Canvas
  419. if( pauseCanvas )
  420. pauseCanvas.gameObject.SetActive(true);
  421. //隐藏游戏Canvas
  422. if( gameCanvas )
  423. gameCanvas.gameObject.SetActive(false);
  424. }
  425.  
  426. /// <summary>
  427. /// 取消暂停
  428. /// </summary>
  429. void Unpause()
  430. {
  431. isPaused = false;
  432.  
  433. Time.timeScale = gameSpeed;
  434.  
  435. //隐藏暂停Canvas
  436. if( pauseCanvas )
  437. pauseCanvas.gameObject.SetActive(false);
  438. //显示游戏Canvas
  439. if( gameCanvas )
  440. gameCanvas.gameObject.SetActive(true);
  441. }
  442.  
  443. /// <summary>
  444. /// 游戏结束协程
  445. /// </summary>
  446. /// <param name="delay">等待时间</param>
  447. IEnumerator GameOver(float delay)
  448. {
  449. yield return new WaitForSeconds(delay);
  450.  
  451. isGameOver = true;
  452.  
  453. //删除暂停面板
  454. if( pauseCanvas )
  455. Destroy(pauseCanvas.gameObject);
  456. //删除游戏面板
  457. if( gameCanvas )
  458. Destroy(gameCanvas.gameObject);
  459.  
  460. //从存档中获取所有金币数量
  461. );
  462.  
  463. //添加本局游戏获得的得分
  464. totalCoins += score;
  465.  
  466. //将新的总得分写入存档中
  467. PlayerPrefs.SetInt( coinsPlayerPrefs, totalCoins);
  468.  
  469. if( gameOverCanvas )
  470. {
  471. //激活游戏结束面板
  472. gameOverCanvas.gameObject.SetActive(true);
  473.  
  474. //显示本次游戏得分
  475. gameOverCanvas.Find("TextScore").GetComponent<Text>().text = "SCORE " + score.ToString();
  476.  
  477. //设置最高得分
  478. if( score > highScore )
  479. {
  480. highScore = score;
  481.  
  482. PlayerPrefs.SetInt(Application.loadedLevelName + "_HighScore", score);
  483. }
  484.  
  485. //将最高得分显示在文本上
  486. gameOverCanvas.Find("TextHighScore").GetComponent<Text>().text = "HIGH SCORE " + highScore.ToString();
  487. }
  488. }
  489.  
  490. /// <summary>
  491. /// 重新开始
  492. /// </summary>
  493. void Restart()
  494. {
  495. Application.LoadLevel(Application.loadedLevelName);
  496. }
  497.  
  498. /// <summary>
  499. /// 回到主目录
  500. /// </summary>
  501. void MainMenu()
  502. {
  503. Application.LoadLevel(mainMenuLevelName);
  504. }
  505.  
  506. /// <summary>
  507. /// 根据玩家编号,激活选择的玩家
  508. /// </summary>
  509. /// <param name="playerNumber"></param>
  510. void SetPlayer( int playerNumber )
  511. {
  512. ; index < playerObjects.Length; index++)
  513. {
  514. if ( index != playerNumber )
  515. playerObjects[index].gameObject.SetActive(false);
  516. else
  517. playerObjects[index].gameObject.SetActive(true);
  518. }
  519. }
  520.  
  521. /// <summary>
  522. /// 移动玩家
  523. /// </summary>
  524. /// <param name="moveDirection">移动方向</param>
  525. void MovePlayer( string moveDirection )
  526. {
  527. if ( playerObjects[currentPlayer] ) playerObjects[currentPlayer].SendMessage("Move", moveDirection);
  528. }
  529. }
  530. }

RCGGameController

  1. using UnityEngine;
  2.  
  3. namespace RoadCrossing
  4. {
  5. /// <summary>
  6. /// 小路
  7. /// </summary>
  8. public class RCGLane : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// transform缓存
  12. /// </summary>
  13. internal Transform thisTransform;
  14.  
  15. /// <summary>
  16. /// 起始位置
  17. /// </summary>
  18. , , -);
  19. /// <summary>
  20. /// 结束位置
  21. /// </summary>
  22. , , );
  23.  
  24. /// <summary>
  25. /// 小路上候选的移动物体数组
  26. /// </summary>
  27. public Transform[] movingObjects;
  28.  
  29. /// <summary>
  30. /// 小路上的移动物体数组
  31. /// </summary>
  32. internal Transform[] movingObjectsList;
  33.  
  34. /// <summary>
  35. /// 小路上允许存在的移动物体的数量范围
  36. /// </summary>
  37. , );
  38.  
  39. /// <summary>
  40. /// 移动物体之间的间隔
  41. /// </summary>
  42. ;
  43.  
  44. /// <summary>
  45. /// 所有物体的移动速度范围
  46. /// </summary>
  47. );
  48.  
  49. /// <summary>
  50. /// 物体的移动方向
  51. /// </summary>
  52. ;
  53.  
  54. /// <summary>
  55. /// 是否随机方向
  56. /// </summary>
  57. public bool randomDirection = true;
  58.  
  59. void Start()
  60. {
  61. thisTransform = transform;
  62.  
  63. //满足条件改变移动方向
  64. if( randomDirection == true && Random.value > 0.5f )
  65. moveDirection = -;
  66.  
  67. //随机选择移动速度
  68. moveSpeed.x = Random.Range(moveSpeed.x, moveSpeed.y);
  69.  
  70. //计算路的长度
  71. float laneLength = Vector3.Distance(laneStart, laneEnd);
  72.  
  73. //设置这条路上移动物体的数量
  74. int currentMovingObjectsNumber = Mathf.FloorToInt(Random.Range(movingObjectsNumber.x, movingObjectsNumber.y));
  75.  
  76. //创建移动物体列表
  77. movingObjectsList = new Transform[currentMovingObjectsNumber];
  78.  
  79. ; index < currentMovingObjectsNumber; index++)
  80. {
  81. //创建一个随机的移动物体
  82. Transform newMovingObject = Instantiate(movingObjects[Mathf.FloorToInt(Random.Range(, movingObjects.Length))]) as Transform;
  83.  
  84. //在小路起点放置这个物体
  85. newMovingObject.position = thisTransform.position + laneStart;
  86. //让这个物体朝向路的终点
  87. newMovingObject.LookAt(thisTransform.position + laneEnd);
  88.  
  89. newMovingObject.Translate(Vector3.forward * index * laneLength / currentMovingObjectsNumber, Space.Self);
  90.  
  91. newMovingObject.Translate(Vector3.forward * Random.Range(minimumObjectGap, laneLength / movingObjectsNumber.x - minimumObjectGap), Space.Self);
  92.  
  93. //将这个物体添加到移动物体列表
  94. movingObjectsList[index] = newMovingObject;
  95. }
  96.  
  97. foreach( Transform movingObject in movingObjectsList )
  98. {
  99. //根据方向设置移动物体的朝向,起点或终点
  100. )
  101. movingObject.LookAt(thisTransform.position + laneEnd);
  102. else
  103. movingObject.LookAt(thisTransform.position + laneStart);
  104.  
  105. //如果又动画组件,播放动画
  106. if( movingObject.GetComponent<Animation>() )
  107. {
  108. //根据物体的移动速度设置动画的播放速度
  109. movingObject.GetComponent<Animation>()[movingObject.GetComponent<Animation>().clip.name].speed = moveSpeed.x;
  110. }
  111. }
  112. }
  113.  
  114. void Update()
  115. {
  116. foreach( Transform movingObject in movingObjectsList )
  117. {
  118. if( movingObject )
  119. {
  120. //如果移动方向等于1
  121. )
  122. {
  123. //从起点向小路终点移动
  124. movingObject.position = Vector3.MoveTowards(movingObject.position, thisTransform.position + laneEnd, moveSpeed.x * Time.deltaTime);
  125.  
  126. //如果到达终点,将游戏物体放到起点
  127. if( movingObject.position == thisTransform.position + laneEnd )
  128. {
  129. movingObject.position = thisTransform.position + laneStart;
  130. movingObject.LookAt(thisTransform.position + laneEnd);
  131. }
  132. }
  133. //如果移动方向不等于1
  134. else
  135. {
  136. //从终点向小路起点移动
  137. movingObject.position = Vector3.MoveTowards(movingObject.position, thisTransform.position + laneStart, moveSpeed.x * Time.deltaTime);
  138.  
  139. //如果到达起点,将游戏物体放到终点
  140. if( movingObject.position == thisTransform.position + laneStart )
  141. {
  142. movingObject.position = thisTransform.position + laneEnd;
  143. movingObject.LookAt(thisTransform.position + laneStart);
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. /// <summary>
  151. /// Gizmos下画起始点和结束点
  152. /// </summary>
  153. void OnDrawGizmos()
  154. {
  155. Gizmos.color = Color.green;
  156. Gizmos.DrawSphere(transform.position + laneStart, 0.2f);
  157.  
  158. Gizmos.color = Color.red;
  159. Gizmos.DrawSphere(transform.position + laneEnd, 0.2f);
  160. }
  161. }
  162. }

RCGLane

  1. using UnityEngine;
  2.  
  3. namespace RoadCrossing
  4. {
  5. /// <summary>
  6. /// 加载场景和URL
  7. /// </summary>
  8. public class RCGLoadLevel : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 加载URL
  12. /// </summary>
  13. /// <param name="urlName"></param>
  14. public void LoadURL(string urlName)
  15. {
  16. Application.OpenURL(urlName);
  17. }
  18.  
  19. /// <summary>
  20. /// 加载指定场景
  21. /// </summary>
  22. /// <param name="levelName"></param>
  23. public void LoadLevel(string levelName)
  24. {
  25. Application.LoadLevel(levelName);
  26. }
  27.  
  28. /// <summary>
  29. /// 重新加载当前场景
  30. /// </summary>
  31. public void RestartLevel()
  32. {
  33. Application.LoadLevel(Application.loadedLevelName);
  34. }
  35. }
  36. }

RCGLoadLevel

  1. using UnityEngine;
  2. using RoadCrossing.Types;
  3.  
  4. namespace RoadCrossing
  5. {
  6. /// <summary>
  7. /// 玩家
  8. /// </summary>
  9. public class RCGPlayer : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// 缓存transform
  13. /// </summary>
  14. internal Transform thisTransform;
  15. /// <summary>
  16. /// 游戏控制器
  17. /// </summary>
  18. internal GameObject gameController;
  19.  
  20. /// <summary>
  21. /// 移动速度
  22. /// </summary>
  23. public float speed = 0.1f;
  24. /// <summary>
  25. /// 是否正在移动
  26. /// </summary>
  27. internal bool isMoving = false;
  28. /// <summary>
  29. /// 上一个位置
  30. /// </summary>
  31. internal Vector3 previousPosition;
  32. /// <summary>
  33. /// 目标位置
  34. /// </summary>
  35. internal Vector3 targetPosition;
  36.  
  37. /// <summary>
  38. /// 限制玩家移动的物体
  39. /// </summary>
  40. public Transform moveLimits;
  41.  
  42. /// <summary>
  43. /// 移动动画
  44. /// </summary>
  45. public AnimationClip animationMove;
  46. /// <summary>
  47. /// 获得金币的动画
  48. /// </summary>
  49. public AnimationClip animationCoin;
  50.  
  51. /// <summary>
  52. /// 死亡效果的动画列表
  53. /// </summary>
  54. public Transform[] deathEffect;
  55.  
  56. /// <summary>
  57. /// 从不能移动到能移动的持续时间
  58. /// </summary>
  59. ;
  60.  
  61. /// <summary>
  62. /// 移动声音
  63. /// </summary>
  64. public AudioClip[] soundMove;
  65. /// <summary>
  66. /// 获得金币的声音
  67. /// </summary>
  68. public AudioClip[] soundCoin;
  69.  
  70. /// <summary>
  71. /// AudioSource所属的游戏物体标签
  72. /// </summary>
  73. public string soundSourceTag = "GameController";
  74.  
  75. void Start()
  76. {
  77. thisTransform = transform;
  78.  
  79. targetPosition = thisTransform.position;
  80.  
  81. gameController = GameObject.FindGameObjectWithTag("GameController");
  82. }
  83.  
  84. void Update()
  85. {
  86. //让限制玩家移动的物体 跟随玩家移动
  87. if( moveLimits )
  88. {
  89. Vector3 newPosition = new Vector3();
  90.  
  91. newPosition.x = thisTransform.position.x;
  92.  
  93. moveLimits.position = newPosition;
  94. }
  95.  
  96. //计算移动延迟
  97. )
  98. moveDelay -= Time.deltaTime;
  99.  
  100. //不处于移动状态 移动延迟<=0 时间缩放>0
  101. && Time.timeScale > )
  102. {
  103. //垂直抽没有输入值时,才可以左右移动
  104. )
  105. {
  106. //向右移动
  107. )
  108. {
  109. Move("right");
  110. }
  111.  
  112. //向左移动
  113. )
  114. {
  115. // Move one step to the left
  116. Move("left");
  117. }
  118. }
  119.  
  120. //水平轴没有输入值,才可以前后移动
  121. )
  122. {
  123. //向前移动
  124. )
  125. {
  126. Move("forward");
  127. }
  128.  
  129. //向后移动
  130. )
  131. {
  132. Move("backward");
  133. }
  134. }
  135. }
  136. //其余的情况 移动玩家到目标位置
  137. else
  138. {
  139. //保持向前移动直到 到达目标位置
  140. if( thisTransform.position != targetPosition )
  141. {
  142. //将玩家移动到目标位置
  143. thisTransform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * speed);
  144. }
  145. else
  146. {
  147. //表示玩家不在移动状态中
  148. isMoving = false;
  149. }
  150. }
  151. }
  152.  
  153. /// <summary>
  154. /// 移动
  155. /// </summary>
  156. /// <param name="moveDirection">移动方向</param>
  157. void Move(string moveDirection)
  158. {
  159. )
  160. {
  161.  
  162. isMoving = true;
  163.  
  164. switch( moveDirection.ToLower() )
  165. {
  166. //向前移动
  167. case "forward":
  168. //让玩家朝前
  169. Vector3 newEulerAngle = new Vector3();
  170. newEulerAngle.y = ;
  171. thisTransform.eulerAngles = newEulerAngle;
  172.  
  173. //计算目标位置
  174. targetPosition = thisTransform.position + , , );
  175.  
  176. //让玩家在固定的网格里移动
  177. targetPosition.x = Mathf.Round(targetPosition.x);
  178. targetPosition.z = Mathf.Round(targetPosition.z);
  179.  
  180. //记录上一个位置,用来在遇到障碍物的时候,让玩家回到上一个位置
  181. previousPosition = thisTransform.position;
  182.  
  183. break;
  184.  
  185. case "backward":
  186. //让玩家朝后
  187. newEulerAngle = new Vector3();
  188. newEulerAngle.y = ;
  189. thisTransform.eulerAngles = newEulerAngle;
  190.  
  191. //记录上一个位置,用来在遇到障碍物的时候,让玩家回到上一个位置
  192. previousPosition = thisTransform.position;
  193.  
  194. //让玩家在固定的网格里移动
  195. targetPosition.x = Mathf.Round(targetPosition.x);
  196. targetPosition.z = Mathf.Round(targetPosition.z);
  197.  
  198. //计算目标位置
  199. targetPosition = thisTransform.position + , , );
  200.  
  201. break;
  202.  
  203. case "right":
  204. //让玩家朝向右
  205. newEulerAngle = new Vector3();
  206. newEulerAngle.y = ;
  207. thisTransform.eulerAngles = newEulerAngle;
  208.  
  209. //记录上一个位置,用来在遇到障碍物的时候,让玩家回到上一个位置
  210. previousPosition = thisTransform.position;
  211.  
  212. //让玩家在固定的网格里移动
  213. targetPosition.x = Mathf.Round(targetPosition.x);
  214. targetPosition.z = Mathf.Round(targetPosition.z);
  215.  
  216. //计算目标位置
  217. targetPosition = thisTransform.position + , , -);
  218.  
  219. break;
  220.  
  221. case "left":
  222. //让玩家朝向左
  223. newEulerAngle = new Vector3();
  224. newEulerAngle.y = -;
  225. thisTransform.eulerAngles = newEulerAngle;
  226.  
  227. //记录上一个位置,用来在遇到障碍物的时候,让玩家回到上一个位置
  228. previousPosition = thisTransform.position;
  229.  
  230. //让玩家在固定的网格里移动
  231. targetPosition.x = Mathf.Round(targetPosition.x);
  232. targetPosition.z = Mathf.Round(targetPosition.z);
  233.  
  234. //计算目标位置
  235. targetPosition = thisTransform.position + , , );
  236.  
  237. break;
  238.  
  239. default:
  240. //让玩家朝前
  241. newEulerAngle = new Vector3();
  242. newEulerAngle.y = ;
  243. thisTransform.eulerAngles = newEulerAngle;
  244.  
  245. //计算目标位置
  246. targetPosition = thisTransform.position + , , );
  247.  
  248. //归一化目标位置
  249. targetPosition.Normalize();
  250.  
  251. //记录上一个位置,用来在遇到障碍物的时候,让玩家回到上一个位置
  252. previousPosition = thisTransform.position;
  253.  
  254. break;
  255. }
  256.  
  257. //满足条件 播放移动动画
  258. if( GetComponent<Animation>() && animationMove )
  259. {
  260. GetComponent<Animation>().Stop();
  261. GetComponent<Animation>().Play(animationMove.name);
  262.  
  263. //基于移动速度设置动画速度
  264. GetComponent<Animation>()[animationMove.name].speed = speed;
  265.  
  266. //满足条件播放移动声音
  267. )
  268. GameObject.FindGameObjectWithTag(soundSourceTag).GetComponent<AudioSource>().PlayOneShot(soundMove[Mathf.FloorToInt(Random.value * soundMove.Length)]);
  269. }
  270. }
  271. }
  272.  
  273. /// <summary>
  274. /// 取消玩家的移动,将玩家弹回到前一个位置
  275. /// </summary>
  276. void CancelMove(float moveDelayTime)
  277. {
  278. //满足条件播放动画
  279. if( GetComponent<Animation>() && animationMove )
  280. {
  281. //基于移动速度,倒放动画
  282. GetComponent<Animation>()[animationMove.name].speed = -speed;
  283. }
  284.  
  285. //将目标位置设置为前一个位置
  286. targetPosition = previousPosition;
  287.  
  288. //设置移动延迟
  289. moveDelay = moveDelayTime;
  290. }
  291.  
  292. /// <summary>
  293. /// 获得金币
  294. /// </summary>
  295. void AddCoin(int addValue)
  296. {
  297. //调用游戏控制里的改变分数方法
  298. gameController.SendMessage("ChangeScore", addValue);
  299.  
  300. //播放获得金币动画
  301. if( GetComponent<Animation>() && animationCoin && animationMove )
  302. {
  303. GetComponent<Animation>()[animationCoin.name].layer = ;
  304. GetComponent<Animation>().Play(animationCoin.name);
  305. GetComponent<Animation>()[animationCoin.name].weight = 0.5f;
  306. }
  307.  
  308. //播放获得金币声音
  309. )
  310. GameObject.FindGameObjectWithTag(soundSourceTag).GetComponent<AudioSource>().PlayOneShot(soundCoin[Mathf.FloorToInt(Random.value * soundCoin.Length)]);
  311. }
  312.  
  313. /// <summary>
  314. /// 死亡
  315. /// </summary>
  316. /// <param name="deathType">死亡效果类型</param>
  317. void Die(int deathType)
  318. {
  319. //调用游戏控制器的游戏结束方法
  320. gameController.SendMessage("GameOver", 0.5f);
  321.  
  322. //创建死亡特效
  323. )
  324. {
  325. Instantiate(deathEffect[deathType], thisTransform.position, thisTransform.rotation);
  326. }
  327.  
  328. //销毁游戏物体
  329. Destroy(gameObject);
  330. }
  331. }
  332. }

RCGPlayer

  1. using UnityEngine;
  2.  
  3. namespace RoadCrossing
  4. {
  5. /// <summary>
  6. /// 播放声音
  7. /// </summary>
  8. public class RCGPlaySound : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 声音数组
  12. /// </summary>
  13. public AudioClip[] audioList;
  14.  
  15. /// <summary>
  16. /// AudioSource所在物体的标签
  17. /// </summary>
  18. public string audioSourceTag = "GameController";
  19. /// <summary>
  20. /// 游戏开始时是否播放
  21. /// </summary>
  22. public bool playOnStart = true;
  23.  
  24. void Start()
  25. {
  26. if( playOnStart == true )
  27. PlaySound();
  28. }
  29.  
  30. /// <summary>
  31. /// 播放声音
  32. /// </summary>
  33. void PlaySound()
  34. {
  35. )
  36. GameObject.FindGameObjectWithTag(audioSourceTag).GetComponent<AudioSource>().PlayOneShot(audioList[Mathf.FloorToInt(Random.value * audioList.Length)]);
  37. }
  38.  
  39. /// <summary>
  40. /// 根据索引播放声音
  41. /// </summary>
  42. void PlaySound(int soundIndex)
  43. {
  44. )
  45. GameObject.FindGameObjectWithTag(audioSourceTag).GetComponent<AudioSource>().PlayOneShot(audioList[soundIndex]);
  46. }
  47. }
  48. }

RCGPlaySound

  1. using UnityEngine;
  2.  
  3. namespace RoadCrossing
  4. {
  5. /// <summary>
  6. /// 随机化物体的颜色
  7. /// </summary>
  8. public class RCGRandomizeColor : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 颜色数组
  12. /// </summary>
  13. public Color[] colorList;
  14.  
  15. void Start()
  16. {
  17. //随机选择一个颜色
  18. , colorList.Length));
  19. //将颜色应用到物体和子物体上
  20. Component[] comps = GetComponentsInChildren<Renderer>();
  21. foreach( Renderer part in comps )
  22. part.GetComponent<Renderer>().material.color = colorList[randomColor];
  23. }
  24. }
  25. }

RCGRandomizeColor

  1. using UnityEngine;
  2.  
  3. namespace RoadCrossing
  4. {
  5. /// <summary>
  6. /// 随机化物体的旋转,缩放和颜色
  7. /// </summary>
  8. public class RCGRandomizeTransform : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// x轴的旋转范围
  12. /// </summary>
  13. , );
  14. /// <summary>
  15. /// y轴的旋转范围
  16. /// </summary>
  17. , );
  18. /// <summary>
  19. /// z轴的旋转范围
  20. /// </summary>
  21. , );
  22.  
  23. /// <summary>
  24. /// x轴缩放范围
  25. /// </summary>
  26. , 1.3f);
  27. /// <summary>
  28. /// y轴缩放范围
  29. /// </summary>
  30. , 1.3f);
  31. /// <summary>
  32. /// z轴缩放范围
  33. /// </summary>
  34. , 1.3f);
  35.  
  36. /// <summary>
  37. /// 是否统一比例缩放
  38. /// </summary>
  39. public bool uniformScale = true;
  40.  
  41. /// <summary>
  42. /// 颜色列表
  43. /// </summary>
  44. public Color[] colorList;
  45.  
  46. void Start()
  47. {
  48. //随机旋转
  49. transform.localEulerAngles = new Vector3(Random.Range(rotationRangeX.x, rotationRangeX.y), Random.Range(rotationRangeY.x, rotationRangeY.y), Random.Range(rotationRangeZ.x, rotationRangeZ.y));
  50.  
  51. //如果是统一缩放,让y,z轴基于x轴的比例缩放
  52. if( uniformScale == true )
  53. scaleRangeY = scaleRangeZ = scaleRangeX;
  54.  
  55. //随机缩放
  56. transform.localScale = new Vector3(Random.Range(scaleRangeX.x, scaleRangeX.y), Random.Range(scaleRangeY.x, scaleRangeY.y), Random.Range(scaleRangeZ.x, scaleRangeZ.y));
  57.  
  58. //随机颜色
  59. , colorList.Length));
  60. Component[] comps = GetComponentsInChildren<Renderer>();
  61. foreach( Renderer part in comps )
  62. part.GetComponent<Renderer>().material.color = colorList[randomColor];
  63. }
  64. }
  65. }

RCGRandomizeTransform

  1. using UnityEngine;
  2.  
  3. namespace RoadCrossing
  4. {
  5. /// <summary>
  6. /// 经过一段时间后删除物体,用来在特效播放完成后删除特效
  7. /// </summary>
  8. public class RCGRemoveAfter : MonoBehaviour
  9. {
  10. //删除物体的等待时间
  11. ;
  12.  
  13. void Update()
  14. {
  15. removeAfter -= Time.deltaTime;
  16.  
  17. )
  18. Destroy(gameObject);
  19. }
  20. }
  21. }

RCGRemoveAfter

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using RoadCrossing.Types;
  5.  
  6. namespace RoadCrossing
  7. {
  8. /// <summary>
  9. /// 商店
  10. /// </summary>
  11. public class RCGShop : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// 剩余的金币数量
  15. /// </summary>
  16. ;
  17.  
  18. /// <summary>
  19. /// 显示金币的文本
  20. /// </summary>
  21. public Transform coinsText;
  22.  
  23. /// <summary>
  24. /// 玩家的金币值
  25. /// </summary>
  26. public string coinsPlayerPrefs = "Coins";
  27.  
  28. /// <summary>
  29. /// 商店道具列表
  30. /// </summary>
  31. public ShopItem[] shopItems;
  32.  
  33. /// <summary>
  34. /// 当前道具
  35. /// </summary>
  36. ;
  37.  
  38. /// <summary>
  39. /// 存档里的当前玩家键
  40. /// </summary>
  41. public string playerPrefsName = "CurrentPlayer";
  42.  
  43. /// <summary>
  44. /// 道具的未被选择时的颜色
  45. /// </summary>
  46. );
  47.  
  48. /// <summary>
  49. /// 道具的选中颜色
  50. /// </summary>
  51. ,,,);
  52.  
  53. /// <summary>
  54. /// 暂未使用
  55. /// </summary>
  56. ,,,);
  57.  
  58. void Start()
  59. {
  60. //获取剩余金币
  61. coinsLeft = PlayerPrefs.GetInt(coinsPlayerPrefs, coinsLeft);
  62.  
  63. //更新金币文本
  64. coinsText.GetComponent<Text>().text = coinsLeft.ToString();
  65.  
  66. //获取当前道具
  67. currentItem = PlayerPrefs.GetInt(playerPrefsName, currentItem);
  68.  
  69. //更新所有道具
  70. UpdateItems();
  71. }
  72.  
  73. /// <summary>
  74. /// 更新道具
  75. /// </summary>
  76. void UpdateItems()
  77. {
  78. ; index < shopItems.Length ; index++ )
  79. {
  80. //从存档中获取这个道具的解锁状态
  81. shopItems[index].lockState = PlayerPrefs.GetInt(shopItems[index].playerPrefsName, shopItems[index].lockState);
  82.  
  83. //设置道具的颜色为 未选择的颜色
  84. shopItems[index].itemButton.GetComponent<Image>().color = unselectedColor;
  85.  
  86. //如果这个道具已经解锁
  87. )
  88. {
  89. //禁用显示价格的物体
  90. shopItems[index].itemButton.Find("TextPrice").gameObject.SetActive(false);
  91.  
  92. //高亮当前选择的物体的颜色
  93. if ( index == currentItem ) shopItems[index].itemButton.GetComponent<Image>().color = selectedColor;
  94. }
  95. //如果没有解锁
  96. else
  97. {
  98. //更新这个道具的价格
  99. shopItems[index].itemButton.Find("TextPrice").GetComponent<Text>().text = shopItems[index].costToUnlock.ToString();
  100. }
  101. }
  102. }
  103.  
  104. /// <summary>
  105. /// 买道具
  106. /// </summary>
  107. /// <param name="itemNumber"></param>
  108. void BuyItem( int itemNumber )
  109. {
  110. //如果这个道具已解锁,选择这个道具
  111. )
  112. {
  113. SelectItem(itemNumber);
  114. }
  115. //如果有足够的金币,消耗金币买这个道具
  116. else if ( shopItems[itemNumber].costToUnlock <= coinsLeft )
  117. {
  118. //解锁道具
  119. shopItems[itemNumber].lockState = ;
  120.  
  121. //在存档中更新这个道具的状态
  122. PlayerPrefs.SetInt(shopItems[itemNumber].playerPrefsName, shopItems[itemNumber].lockState);
  123.  
  124. //扣除金币
  125. coinsLeft -= shopItems[itemNumber].costToUnlock;
  126.  
  127. //更新文本
  128. coinsText.GetComponent<Text>().text = coinsLeft.ToString();
  129.  
  130. //将剩余金币写入Prefs
  131. PlayerPrefs.SetInt(coinsPlayerPrefs, coinsLeft);
  132.  
  133. //选择道具
  134. SelectItem(itemNumber);
  135. }
  136.  
  137. UpdateItems();
  138. }
  139.  
  140. /// <summary>
  141. /// 选择道具
  142. /// </summary>
  143. /// <param name="itemNumber"></param>
  144. void SelectItem( int itemNumber )
  145. {
  146. currentItem = itemNumber;
  147.  
  148. PlayerPrefs.SetInt( playerPrefsName, itemNumber);
  149. }
  150. }
  151. }

RCGShop

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. namespace RoadCrossing
  6. {
  7. /// <summary>
  8. /// 显示文本
  9. /// </summary>
  10. public class RCGTextByPlatform : MonoBehaviour
  11. {
  12.  
  13. /// <summary>
  14. /// 显示在PC/Mac/Webplayer中的文本
  15. /// </summary>
  16. public string computerText = "CLICK TO START";
  17.  
  18. /// <summary>
  19. /// 显示在Android/iOS/WinPhone中的文本
  20. /// </summary>
  21. public string mobileText = "TAP TO START";
  22.  
  23. /// <summary>
  24. /// 显示在Playstation/Xbox/Wii中的文本
  25. /// </summary>
  26. public string consoleText = "PRESS 'A' TO START";
  27.  
  28. void Start()
  29. {
  30. #if UNITY_STANDALONE || UNITY_WEBPLAYER
  31. GetComponent<Text>().text = computerText;
  32. #endif
  33.  
  34. #if IPHONE || ANDROID || UNITY_BLACKBERRY || UNITY_WP8 || UNITY_METRO
  35. GetComponent<Text>().text = mobileText;
  36. #endif
  37.  
  38. #if UNITY_WII || UNITY_PS3 || UNITY_XBOX360
  39. GetComponent<Text>().text = consoleText;
  40. #endif
  41. }
  42. }
  43. }

RCGTextByPlatform

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. namespace RoadCrossing
  6. {
  7. /// <summary>
  8. /// 声音开关
  9. /// </summary>
  10. public class RCGToggleSound:MonoBehaviour
  11. {
  12. /// <summary>
  13. /// 播放声音的物体
  14. /// </summary>
  15. public Transform soundObject;
  16.  
  17. /// <summary>
  18. /// 存档中的名称
  19. /// </summary>
  20. public string playerPref = "SoundVolume";
  21.  
  22. /// <summary>
  23. /// 声音的当前音量
  24. /// </summary>
  25. ;
  26.  
  27. void Awake()
  28. {
  29. if( soundObject )
  30. currentState = PlayerPrefs.GetFloat(playerPref, soundObject.GetComponent<AudioSource>().volume);
  31. else
  32. currentState = PlayerPrefs.GetFloat(playerPref, currentState);
  33.  
  34. SetSound();
  35. }
  36.  
  37. /// <summary>
  38. /// 设置声音大小
  39. /// </summary>
  40. void SetSound()
  41. {
  42. //设置声音存档
  43. PlayerPrefs.SetFloat(playerPref, currentState);
  44.  
  45. Color newColor = GetComponent<Image>().material.color;
  46.  
  47. //根据声音存档值设置按钮透明度
  48. )
  49. newColor.a = ;
  50. else
  51. newColor.a = 0.5f;
  52.  
  53. //应用透明度
  54. GetComponent<Image>().color = newColor;
  55.  
  56. //设置音量
  57. if( soundObject )
  58. soundObject.GetComponent<AudioSource>().volume = currentState;
  59. }
  60.  
  61. /// <summary>
  62. /// 开关声音
  63. /// </summary>
  64. void ToggleSound()
  65. {
  66. currentState = - currentState;
  67.  
  68. SetSound();
  69. }
  70.  
  71. /// <summary>
  72. /// 播放声音
  73. /// </summary>
  74. void StartSound()
  75. {
  76. if( soundObject )
  77. soundObject.GetComponent<AudioSource>().Play();
  78. }
  79. }
  80. }

RCGToggleSound

视频:https://pan.baidu.com/s/1qYPhpsK

项目:https://pan.baidu.com/s/1mi5C7s0

Road Crossing Game Template 学习的更多相关文章

  1. template学习一函数模板

    要点: 1.模板参数在实体化的时候不能自动类型转换,只有非模板函数才可以 例如: int max(int,int); template <class T> T max(T,T); 在调用的 ...

  2. C++ template学习二 类模板定义及实例化

    一个类模板(也称为类属类或类生成类)允许用户为类定义一种模式,使得类中的某些数据成员.默写成员函数的参数.某些成员函数的返回值,能够取任意类型(包括系统预定义的和用户自定义的). 如果一个类中数据成员 ...

  3. C++ template学习一(函数模板和模板函数)

    函数模板和模板函数(1)函数模板函数模板可以用来创建一个通用的函数,以支持多种不同的形参,避免重载函数的函数体重复设计.它的最大特点是把函数使用的数据类型作为参数.函数模板的声明形式为:templat ...

  4. jQuery .tmpl(), .template()学习资料小结

    昨晚无意中发现一个有趣的jQuery插件.tmpl(),其文档在这里.官方解释对该插件的说明:将匹配的第一个元素作为模板,render指定的数据,签名如下: .tmpl([data,][options ...

  5. Elasticsearch template学习

    Elasticsearch template Elasticsearch存在一个关键问题就是索引的设置及字段的属性指定,最常见的问题就是,某个字段我们并不希望ES对其进行分词,但如果使用自动模板创建索 ...

  6. jQuery.template.js 简单使用

    之前看了一篇文章<我们为什么要尝试前后端分离>,深有同感,并有了下面的评论: 我最近也和前端同事在讨论这个问题,比如有时候前端写好页面给后端了,然后后端把这些页面拆分成很多的 views, ...

  7. 模板学习实践二 pointer

    c++ template学习记录 使用模板将实际类型的指针进行封装 当变量退出作用域 自动delete // 1111.cpp : 定义控制台应用程序的入口点. // #include "s ...

  8. DCN模型

    1. DCN优点 使用Cross Network,在每一层都运用了Feature Crossing,高效学习高阶特征. 网络结构简单且高效 相比DNN,DCN的Logloss值更低,而且参数的数量少了 ...

  9. The Road to learn React书籍学习笔记(第三章)

    The Road to learn React书籍学习笔记(第三章) 代码详情 声明周期方法 通过之前的学习,可以了解到ES6 类组件中的生命周期方法 constructor() 和 render() ...

随机推荐

  1. node(3)Buffer缓冲区

    buffer 专门用来存放二进制数据的缓冲区:处理文件流 TCP流 const buf = Buffer.from('runoob', 'ascii'); // 创建一个长度为 10.且用 0x1 填 ...

  2. ngnix笔记

    ngnix可通过-s 参数控制,如quit正常退出:reload重载配置文件,具体参考:http://nginx.org/en/docs/switches.html ngnix的指令解释请参考这里:h ...

  3. :状态模式:GumballMachine

    #ifndef __STATE_H__ #define __STATE_H__ #include <iostream> #include<stdlib.h> using nam ...

  4. SQL-35 对于表actor批量插入如下数据,如果数据已经存在,请忽略,不使用replace操作

    题目描述 对于表actor批量插入如下数据,如果数据已经存在,请忽略,不使用replace操作CREATE TABLE IF NOT EXISTS actor (actor_id smallint(5 ...

  5. Windows下安装Tensorflow报错 “DLL load failed:找不到指定的模块"

    Windows下安装完tensorflow后,在cmd下运行python后import tensorflow出现如下错误: Traceback (most recent call last):  Fi ...

  6. 1.Python爬虫入门一之综述

    要学习Python爬虫,我们要学习的共有以下几点: Python基础知识 Python中urllib和urllib2库的用法 Python正则表达式 Python爬虫框架Scrapy Python爬虫 ...

  7. 【Python】xpath-1

    1.coverage包实现代码覆盖率 (1)pip install coverage (2)coverage run XX.py(测试脚本文件) (3)coverage report -m 在控制台打 ...

  8. js--未来元素

    通过动态生成的标签,在生成标签直接绑定事件是无效的. eg:html标签 <div id="tree"> </div> <script> $(' ...

  9. [opencvjichu]cv::Mat::type() 返回值

    opencv opencv中Mat存在各种类型,其中mat有一个type()的函数可以返回该Mat的类型.类型表示了矩阵中元素的类型以及矩阵的通道个数,它是一系列的预定义的常量,其命名规则为CV_(位 ...

  10. 用jq修改css

    $(".tag_add").css("background","#ffffff"); $(".tag_add").css ...