GeneratingTarget.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using UnityEngine.SceneManagement;
  8. using SmartBowSDK;
  9. using System;
  10. using HyperspaceGame;
  11. using MathNet.Numerics;
  12. public class GeneratingTarget : MonoBehaviour
  13. {
  14. // 最小时间间隔(秒)
  15. float _minTimeBetweenEvents = 1.0f;
  16. public float minTimeBetweenEvents { get { return _minTimeBetweenEvents; } set { _minTimeBetweenEvents = value; } }
  17. float _maxTimeBetweenEvents = 3.0f;// 最大时间间隔(秒)
  18. public float maxTimeBetweenEvents { get { return _maxTimeBetweenEvents; } set { _maxTimeBetweenEvents = value; } }
  19. public GameObject target;
  20. public Transform UI;
  21. public TextMeshProUGUI scoreUI;
  22. public TextMeshProUGUI scoreUI1;
  23. public Text countDownUI;
  24. public TextMeshProUGUI clipsizeUI;
  25. public TextMeshProUGUI hitCountUI;
  26. public TextMeshProUGUI shootRate;
  27. public GameObject GameOver;
  28. public Score scoreCom;
  29. public Score scoreCom1;
  30. public GameObject Miss;
  31. public Image grade;
  32. public Transform tp;
  33. public GameObject bulletNull;//子弹不足UI
  34. public Sprite bulletHalf;
  35. public Sprite bulletFull;
  36. public GameObject[] bulletImages;//子弹图片数组
  37. private bool isThirtyBulletMode = false;
  38. public GameObject SelectLv;
  39. public Image imgSlider;
  40. public Text TopScore;
  41. public Text CurLvTxt;
  42. public Text MaxLvTxt;
  43. public int bulletCount;//子弹数量
  44. public int hitCount;//击中数量
  45. private float rate;//命中率
  46. public Button BtnRestart;
  47. public Button BtnNext;
  48. public Button BtnLast;
  49. int _score;
  50. public bool stop = false;
  51. public int score { get { return _score; }
  52. set {
  53. _score = value;
  54. ShowScore();
  55. }
  56. }
  57. /// <summary>
  58. /// 游戏时间
  59. /// </summary>
  60. private float countDownTime;
  61. int countDownTimeCache;
  62. /// <summary>
  63. /// 子弹数量
  64. /// </summary>
  65. private int clipsize;
  66. private int BulletCount = 15;
  67. /// <summary>
  68. /// 靶子存在最长时间
  69. /// </summary>
  70. float _targetExistenceTimeMax = 5;
  71. public float TargetExistenceTimeMax { get { return _targetExistenceTimeMax; } set { _targetExistenceTimeMax = value; } }
  72. /// <summary>
  73. /// 靶子存在最短时间
  74. /// </summary>
  75. float _targetExistenceTimeMin = 3;
  76. public float TargetExistenceTimeMin { get { return _targetExistenceTimeMin; } set { _targetExistenceTimeMin = value; } }
  77. float _scaleMin = 1.3f;
  78. public float ScaleMin { get { return _scaleMin; } set { _scaleMin = value; } }
  79. float _scaleMax = 1.8f;
  80. public float ScaleMax { get { return _scaleMax; } set { _scaleMax = value; } }
  81. public HyperspaceGame.Font font;
  82. public AudioSource ShootingSound;
  83. public AudioSource gameSatrtSound;
  84. public AudioClip[] gameSatrtClips;
  85. //public GameObject perfect;
  86. //public GameObject gaeat;
  87. //public GameObject good;
  88. //public Image goodImage;
  89. //public Image greatImage;
  90. public static GeneratingTarget gm { get; set; }
  91. public ShootingEvent shootingEvent;
  92. /// <summary>
  93. /// 记录当前射箭是否记录分数
  94. /// </summary>
  95. private bool bAddCountScore { get; set; } = false;
  96. [HideInInspector] public bool getAddCountScore => bAddCountScore;
  97. //分数统计
  98. [HideInInspector] public UserGameAnalyse1 userGameAnalyse1;
  99. public int Index = 0;
  100. private bool unload = false;
  101. /// <summary>
  102. /// 当前难度的配置
  103. /// </summary>
  104. public Level LvCfg = null;
  105. /// <summary>
  106. /// 当前序列的配置
  107. /// </summary>
  108. public Order orderCfg = null;
  109. float nextOrderWaitTime;
  110. int curOrderIdx;
  111. bool canEnterNextOrder = true;
  112. bool isbegin = false;
  113. int tragetLeftNum;
  114. /// <summary>
  115. /// 当前序列
  116. /// </summary>
  117. public int CurOrder = 0;
  118. public static int MaxLevel = 3;
  119. public List<Sprite> ScoreLevel;
  120. public List<Sprite> EnScoreLevel;
  121. public List<Sprite> JPScoreLevel;
  122. [SerializeField]
  123. RectTransform _canvasRectTransform;
  124. private void Awake()
  125. {
  126. gm = this;
  127. var scene = SceneManager.GetActiveScene();
  128. if(scene.name == "Hyperspace01")
  129. Index = 0;
  130. else if (scene.name == "Hyperspace02")
  131. Index = 1;
  132. else if(scene.name == "Hyperspace03")
  133. Index = 2;
  134. //初始化关卡配置
  135. LvCfg = Config.levels[Index];
  136. countDownTime = LvCfg.TotalTime;
  137. curOrderIdx = 0;
  138. UpdateCountDown(LvCfg.TotalTime);
  139. CurLvTxt.text = $"{Index + 1}";
  140. MaxLvTxt.text = $"/<color=#0F92D4>{MaxLevel}</color>";
  141. shootingEvent = FindObjectOfType<ShootingEvent>();
  142. scoreCom.gameObject.SetActive(false);
  143. scoreCom1.gameObject.SetActive(false);
  144. Miss.SetActive(false);
  145. stop = false;
  146. imgSlider.fillAmount = 1;
  147. ShowScore();
  148. bulletNull.SetActive(false);
  149. BtnRestart.onClick.AddListener(OnRestart);
  150. BtnNext.onClick.AddListener(OnBtnNext);
  151. BtnNext.gameObject.SetActive(Index + 1 < MaxLevel);
  152. BtnLast.onClick.AddListener(OnBtnLast);
  153. BtnLast.gameObject.SetActive(true);
  154. GameOver.SetActive(false);
  155. CreateUIManager();
  156. CreateTargetObject2D();
  157. //是否关闭背景音效
  158. if (!UserSettings.ins.openBGM) {
  159. AudioSource bgm = GetComponent<AudioSource>();
  160. bgm.Stop();
  161. }
  162. if (BluetoothAim.ins && BluetoothAim.ins.isMainConnectToGunType().isGun && BluetoothAim.ins.isMainConnectToGunType().gunType == AimDeviceType.RifleM416)
  163. {
  164. BulletCount = 30;
  165. isThirtyBulletMode = true;
  166. }
  167. else
  168. {
  169. BulletCount = 15;
  170. isThirtyBulletMode = false;
  171. }
  172. }
  173. public void OnRestart()
  174. {
  175. Restart(Index);
  176. }
  177. public void OnBtnNext()
  178. {
  179. Restart(Index + 1);
  180. }
  181. public void OnBtnLast()
  182. {
  183. //Restart(Index - 1);
  184. onUploadScore();
  185. ////结束游戏页面
  186. //userGameAnalyse1.UploadData(true);
  187. //userGameAnalyse1.onResetOverlayData();
  188. //SceneManager.LoadScene("Home", LoadSceneMode.Single);
  189. //结束游戏页面
  190. userGameAnalyse1.showResultView(() =>
  191. {
  192. gm = null;
  193. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  194. });
  195. }
  196. void Start()
  197. {
  198. if (ShootCheck.ins.bluetoothDeviceStatus == BluetoothDeviceStatus.MagazineLoading)
  199. OnLoading();
  200. else
  201. OnSeparation();
  202. //Screen.SetResolution(1280, 720, false); // 设置分辨率为1920x1080,‌并设置为全屏模式
  203. Invoke("GameStart", 4);
  204. Game1();
  205. Invoke("Game2", 1);
  206. Invoke("Game3", 2);
  207. var canvases = FindObjectsByType<Canvas>(FindObjectsSortMode.InstanceID);
  208. foreach (var canvas in canvases)
  209. {
  210. if (canvas.name == "Canvas")
  211. {
  212. _canvasRectTransform = canvas.GetComponent<RectTransform>();
  213. break;
  214. }
  215. }
  216. //靶子默认状态
  217. var show = UIManager._ins.GetArrowBtnState();
  218. var arrow = shootingEvent.GetComponent<Image>();
  219. var color = arrow.color;
  220. if (show)
  221. color.a = 1;
  222. else
  223. color.a = 0;
  224. arrow.color = color;
  225. UIManager._ins.ShowQuit();
  226. }
  227. public void OpenSelectLevel()
  228. {
  229. SelectLv.SetActive(true);
  230. }
  231. public Vector2 GetCanvasSize()
  232. {
  233. return _canvasRectTransform.sizeDelta;
  234. }
  235. void Game1()
  236. {
  237. gameSatrtSound.clip = gameSatrtClips[0];
  238. if (UserSettings.ins.openEffect) gameSatrtSound.Play();
  239. }
  240. void Game2()
  241. {
  242. gameSatrtSound.clip = gameSatrtClips[1];
  243. if (UserSettings.ins.openEffect) gameSatrtSound.Play();
  244. }
  245. void Game3()
  246. {
  247. gameSatrtSound.clip = gameSatrtClips[2];
  248. if (UserSettings.ins.openEffect) gameSatrtSound.Play();
  249. }
  250. void GameStart()
  251. {
  252. isbegin = true;
  253. }
  254. private float reloadTime = 0;
  255. private void Reload(bool delay = false)
  256. {
  257. if (delay)
  258. {
  259. reloadTime = 3f;
  260. }
  261. else
  262. {
  263. clipsize = BulletCount;
  264. bulletCount -= font.Clip;
  265. bulletCount += clipsize;
  266. font.Clip = clipsize;
  267. bulletNull.SetActive(false);
  268. //for (int i = 0; i < bulletImages.Length; i++)
  269. //{
  270. // bulletImages[i].SetActive(true);
  271. //}
  272. UpdateBulletUI();
  273. }
  274. }
  275. public bool Reloading()
  276. {
  277. return unload;
  278. //return reloadTime > 0;
  279. }
  280. float _lastShootTime = 0;
  281. /// <summary>
  282. /// 射击
  283. /// </summary>
  284. /// <param name="bAddCount">是否是有效射箭</param>
  285. public void Shooting(bool bAddCount = false)
  286. {
  287. //枪情况下间隔时间0.2,
  288. float interval = GlobalData.MyDeviceMode == DeviceMode.Gun ? 0.2f : 0.5f;
  289. //加个间隔
  290. if (Time.realtimeSinceStartup - _lastShootTime < interval) return;
  291. _lastShootTime = Time.realtimeSinceStartup;
  292. bAddCountScore = bAddCount;
  293. DoShoot();
  294. }
  295. private void DoShoot()
  296. {
  297. if (shootingEvent.ShootUIBtn())
  298. return;
  299. if (unload || clipsize <= 0)
  300. {
  301. Debug.Log("卸了弹夹 或者 没子弹了!");
  302. //没有子弹的声音
  303. //ShootingSound.Play();
  304. bulletImages[0].SetActive(false);
  305. bulletNull.SetActive(true);
  306. return;
  307. }
  308. else
  309. {
  310. //正常发射的声音
  311. if (UserSettings.ins.openEffect) ShootingSound.Play();
  312. }
  313. clipsize--;
  314. if (clipsize <= 0)
  315. {
  316. //Reload(true);//不自动装弹
  317. }
  318. font.Clip = clipsize;
  319. //bulletImages[clipsize].SetActive(false);
  320. UpdateBulletUI();
  321. shootingEvent.OnShooting();
  322. }
  323. private void UpdateBulletUI()
  324. {
  325. if (isThirtyBulletMode)
  326. {
  327. // 每2发对应1格,总共15格
  328. int fullGrids = clipsize / 2;
  329. bool hasHalf = (clipsize % 2 == 1);
  330. for (int i = 0; i < bulletImages.Length; i++)
  331. {
  332. Image raw = bulletImages[i].GetComponent<Image>();
  333. if (i < fullGrids)
  334. {
  335. bulletImages[i].SetActive(true);
  336. raw.sprite = bulletFull; // 默认满格图
  337. }
  338. else if (i == fullGrids && hasHalf)
  339. {
  340. bulletImages[i].SetActive(true);
  341. raw.sprite = bulletHalf;
  342. }
  343. else
  344. {
  345. bulletImages[i].SetActive(false);
  346. }
  347. }
  348. }
  349. else
  350. {
  351. // 正常模式,每发子弹对应一个格
  352. for (int i = 0; i < bulletImages.Length; i++)
  353. {
  354. bulletImages[i].SetActive(i < clipsize);
  355. }
  356. }
  357. }
  358. //int tempScore = 0;
  359. private void Update()
  360. {
  361. //if (reloadTime > 0)
  362. //{
  363. // reloadTime -= Time.deltaTime;
  364. // if (reloadTime <= 0)
  365. // Reload();
  366. //}
  367. #if UNITY_EDITOR
  368. if (Input.GetMouseButtonDown(0))
  369. {
  370. shootingEvent.OnPositionUpdate(Input.mousePosition);
  371. Shooting(true);
  372. }
  373. if (Input.GetKeyDown(KeyCode.W))
  374. {
  375. countDownTime = 0;
  376. }
  377. if (Input.GetKeyDown(KeyCode.R))
  378. {
  379. UIManager._ins.Reload();
  380. }
  381. //if (Input.GetKeyDown(KeyCode.T))
  382. //{
  383. // GameOverGrade(tempScore);
  384. // tempScore += 10;
  385. //}
  386. #endif
  387. if (isbegin)
  388. {
  389. if (canEnterNextOrder)//靶子全消失了 开始走序列休息时间
  390. {
  391. if (nextOrderWaitTime <= 0)
  392. {
  393. //开始下一序列
  394. TriggerRandomEvent();
  395. }
  396. nextOrderWaitTime -= Time.deltaTime;
  397. }
  398. //游戏倒计时
  399. if (countDownTime <= 0)
  400. {
  401. //游戏时间到了 结束
  402. GameEnd();
  403. }
  404. else
  405. {
  406. countDownTime -= Time.deltaTime;
  407. var timeInt = Mathf.CeilToInt(countDownTime);
  408. if(timeInt != countDownTimeCache)
  409. {
  410. countDownTimeCache = timeInt;
  411. UpdateCountDown(countDownTimeCache);
  412. }
  413. imgSlider.fillAmount = countDownTime / (float)LvCfg.TotalTime;
  414. }
  415. if (curMoveCtrl != null && curMoveCtrl is IUpdate)
  416. {
  417. (curMoveCtrl as IUpdate).Update();
  418. }
  419. }
  420. }
  421. private void UpdateCountDown(int value)
  422. {
  423. countDownUI.text = $"{value}";
  424. }
  425. private void GameEnd()
  426. {
  427. stop = true;
  428. GameOver.SetActive(true);
  429. GameOverGrade(score);
  430. isbegin = false;
  431. countDownTimeCache = 0;
  432. //游戏结束上传分数
  433. onUploadScore();
  434. }
  435. /// <summary>
  436. /// 下一轮
  437. /// </summary>
  438. void TriggerRandomEvent()
  439. {
  440. canEnterNextOrder = false;
  441. Debug.Log($"新一轮创建 curOrderIdx={curOrderIdx}");
  442. if (LvCfg.orders.Length > curOrderIdx)
  443. {
  444. orderCfg = LvCfg.orders[curOrderIdx];
  445. CreateTargets();
  446. curOrderIdx++;
  447. }
  448. else
  449. GameEnd();
  450. }
  451. Dictionary<MoveType, Move> movesCtrl = new Dictionary<MoveType, Move>()
  452. {
  453. [MoveType.Stay] = new Stay(),
  454. [MoveType.VET] = new VET(),
  455. [MoveType.ROT] = new ROT(),
  456. [MoveType.W] = new W(),
  457. [MoveType.W2] = new W2(),
  458. [MoveType.RelativeHor] = new RelativeHor(),
  459. [MoveType.RelativeVet] = new RelativeVet(),
  460. [MoveType.HOR] = new HOR(),
  461. [MoveType.Diagonal] = new Diagonal(),
  462. [MoveType.LeftToRight] = new LeftToRight(),
  463. [MoveType.RightToLeft] = new RightToLeft(),
  464. };
  465. private Move curMoveCtrl;
  466. /// <summary>
  467. /// 创建当前轮靶子
  468. /// </summary>
  469. private void CreateTargets()
  470. {
  471. var createCount = orderCfg.Big + orderCfg.Middle + orderCfg.Small;
  472. Debug.Log($"创建数量 Big={orderCfg.Big} Middle={orderCfg.Middle} Small={orderCfg.Small}");
  473. tragetLeftNum = createCount;
  474. List<SpineAnimationLoader> gos = new List<SpineAnimationLoader>();
  475. for (int i = 0; i < createCount; i++)
  476. {
  477. var go = Instantiate(target, tp);
  478. var targetIns = go.GetComponent<SpineAnimationLoader>();
  479. if(i < orderCfg.Big)
  480. targetIns.Init(0.8f * 0.732f, orderCfg, OnTargetDestory);
  481. else if (i < orderCfg.Big + orderCfg.Middle)
  482. targetIns.Init(0.5f * 0.692f, orderCfg, OnTargetDestory);
  483. else
  484. targetIns.Init(0.3f * 0.733f, orderCfg, OnTargetDestory);
  485. go.SetActive(true);
  486. gos.Add(targetIns);
  487. }
  488. movesCtrl.TryGetValue(orderCfg.MoveType, out curMoveCtrl);
  489. if (curMoveCtrl == null)
  490. curMoveCtrl = movesCtrl[MoveType.Stay];
  491. curMoveCtrl.SetGo(gos);
  492. }
  493. /// <summary>
  494. /// 靶子被射击或者到时间销毁
  495. /// </summary>
  496. private void OnTargetDestory()
  497. {
  498. tragetLeftNum--;
  499. if (tragetLeftNum <= 0)
  500. {
  501. curMoveCtrl = null;
  502. Debug.Log("当前轮结束");
  503. canEnterNextOrder = true;
  504. //重置休息时间
  505. nextOrderWaitTime = orderCfg.WaitTime;
  506. }
  507. }
  508. public void ShowScore()
  509. {
  510. //font.Text = score;
  511. TopScore.text = score.ToString().PadLeft(3, '0');
  512. }
  513. public Score scoreSprite;
  514. public void ShowScoreCom(int score, Vector3 pos, Vector2 scorepos, Transform tf, int posIdx)
  515. {
  516. Debug.Log($"得分 score{score}");
  517. if (score >= 6)
  518. {
  519. var go = GameObject.Instantiate(scoreCom, pos, Quaternion.identity, _canvasRectTransform);
  520. go.gameObject.SetActive(true);
  521. go.transform.localScale = Vector3.one * 0.55f;
  522. go.ShowScore(score, scorepos);
  523. }
  524. else
  525. {
  526. var go = GameObject.Instantiate(scoreCom1, tf.position, Quaternion.identity, _canvasRectTransform);
  527. go.gameObject.SetActive(true);
  528. var halfRadius = 547f * 0.5f;
  529. var offest = tf.localScale.x * halfRadius + 80f;
  530. var x = (posIdx == 1 || posIdx == 2) ? offest : -offest;
  531. var y = (posIdx == 1 || posIdx == 4) ? offest : -offest;
  532. go.ShowScore(score, new Vector3(x, y, 0));
  533. }
  534. }
  535. public void ShowMiss(Vector3 position)
  536. {
  537. Debug.LogWarning("MISS");
  538. if (stop)
  539. return;
  540. var go = GameObject.Instantiate(Miss, position, Quaternion.identity, tp);//_canvasRectTransform
  541. go.AddComponent<CountDown>().time = 5f;
  542. go.transform.SetAsFirstSibling();
  543. go.SetActive(true);
  544. }
  545. public void GameOverGrade(int score)
  546. {
  547. hitCountUI.text = hitCount.ToString();
  548. var bulletUse = bulletCount - font.Clip;
  549. clipsizeUI.text = bulletUse.ToString();
  550. float rate = 0;
  551. if (hitCount != 0)
  552. rate = (float)hitCount / (float)bulletUse;
  553. if(rate == 0)
  554. {
  555. rate = 0;
  556. }
  557. shootRate.text = string.Format("{0:P0}", rate);
  558. font.Text = score;
  559. var maxScore = 0;
  560. foreach (var item in LvCfg.orders)
  561. {
  562. maxScore += item.Big + item.Middle + item.Small;
  563. }
  564. maxScore *= 10;
  565. var percent = (float)score / (float)maxScore * 100f;
  566. Debug.Log($"得分比例:{percent} score={score} maxScore={maxScore}");
  567. var index = Mathf.CeilToInt(percent / 10) - 1;
  568. if (index < 0) index = 0;
  569. RectTransform rectTransform = grade.GetComponent<RectTransform>();
  570. //grade.sprite = TextAutoLanguage2.GetLanguage() == LanguageEnum.Chinese ? ScoreLevel[index]: EnScoreLevel[index];
  571. // 设置图片
  572. LanguageEnum lang = TextAutoLanguage2.GetLanguage(); // 获取当前语言
  573. switch (lang)
  574. {
  575. case LanguageEnum.Chinese:
  576. grade.sprite = ScoreLevel[index];
  577. grade.SetNativeSize();
  578. break;
  579. case LanguageEnum.English:
  580. grade.sprite = EnScoreLevel[index];
  581. grade.SetNativeSize();
  582. break;
  583. case LanguageEnum.Japan:
  584. grade.sprite = JPScoreLevel[index];
  585. rectTransform.sizeDelta = new Vector2(500,500);
  586. break;
  587. default:
  588. grade.sprite = ScoreLevel[index]; // 默认中文
  589. grade.SetNativeSize();
  590. break;
  591. }
  592. rectTransform.anchoredPosition = TextAutoLanguage2.GetLanguage() == LanguageEnum.Chinese ? new Vector3(-152, 15, 1) : new Vector3(-144, 15, 1);
  593. rectTransform.localScale = TextAutoLanguage2.GetLanguage() == LanguageEnum.Chinese ? new Vector3(0.55f, 0.55f, 1) : new Vector3(0.25f, 0.25f, 1);
  594. //
  595. }
  596. public void Restart(int index)
  597. {
  598. string name = "";
  599. if (index == 0)
  600. name = "Hyperspace01";
  601. else if (index == 1)
  602. name = "Hyperspace02";
  603. else if (index == 2)
  604. name = "Hyperspace03";
  605. SceneManager.LoadScene(name);
  606. }
  607. //public void SetTarget(int index)
  608. //{
  609. // goodImage.sprite = font.sprites[index];
  610. // good.SetActive(true);
  611. //}
  612. //public void SetGreat(int index)
  613. //{
  614. // greatImage.sprite = font.sprites[index];
  615. // gaeat.SetActive(true);
  616. //}
  617. /// <summary>
  618. /// 弹夹分离
  619. /// </summary>
  620. public void OnSeparation()
  621. {
  622. unload = true;
  623. clipsize = 0;
  624. bulletCount -= font.Clip;
  625. font.Clip = clipsize;
  626. }
  627. /// <summary>
  628. /// 弹夹上膛
  629. /// </summary>
  630. public void OnLoading()
  631. {
  632. unload = false;
  633. Reload();
  634. }
  635. #region 新增 UIManager 和 TargetObject2D
  636. private GameObject uiManagerInstance;
  637. private GameObject targetObject2D;
  638. // 动态加载并生成 UIManager
  639. public void CreateUIManager()
  640. {
  641. GameObject uiManagerPrefab = Resources.Load<GameObject>("UIManager"); // 从 Resources 中加载 UIManager 预制
  642. uiManagerInstance = Instantiate(uiManagerPrefab); // 实例化 UIManager
  643. //管理分数上传
  644. userGameAnalyse1 = UserGameAnalyse1.CreateWhenGameStartAndReturn(16);
  645. }
  646. //校准靶子
  647. public void CreateTargetObject2D() {
  648. GameObject targetObject2DPrefab = Resources.Load<GameObject>("TargetObject2D");
  649. targetObject2D = Instantiate(targetObject2DPrefab, UI);
  650. }
  651. // 销毁 UIManager 实例
  652. public void DestroyUIManager()
  653. {
  654. if (UIManager._ins != null)
  655. {
  656. Destroy(UIManager._ins.gameObject); // 销毁 UIManager 实例
  657. uiManagerInstance = null;
  658. }
  659. }
  660. /// <summary>
  661. /// 射击次数
  662. /// </summary>
  663. /// <param name="count"></param>
  664. public void onShootCount(int count)
  665. {
  666. userGameAnalyse1.changeShootingCount(count);
  667. }
  668. /// <summary>
  669. /// 上传分数到服务器
  670. /// </summary>
  671. public void onUploadScore()
  672. {
  673. if (_score > 0)
  674. {
  675. Debug.Log("上传的积分为:" + _score);
  676. RankComp.Instance.uploadSinglePlayerGameRes(_score);
  677. _score = 0;
  678. }
  679. }
  680. #endregion
  681. }