TaskManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Imagine.WebAR;
  5. using Nfynt;
  6. using TMPro;
  7. using TriLibCore;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using DG.Tweening;
  11. using static TaskConfig;
  12. using static TaskConfig.windowItemGameObject;
  13. using System.Runtime.InteropServices;
  14. public class TaskManager : MonoBehaviour
  15. {
  16. [DllImport("__Internal")] private static extern void WebGLShowWx(string settings);
  17. public GameObject backgo;
  18. public GameObject HtmlBG;
  19. public UserInfo myInfo;
  20. //public ARCamera ar;
  21. public List<TestConfigExcelData> tconfigs;
  22. public TestConfigExcelData ntconfig;
  23. public RawImage bg;
  24. public RawImage imgbg;
  25. public RawImage centerbg;
  26. public RawImage centerbg2;
  27. public RawImage topbg;
  28. public GameObject prebg;
  29. public GameObject Closebt;
  30. public GameObject Closebt2;
  31. //public TextMeshProUGUI dialogueText;
  32. public Text dialogueText;
  33. public GameObject Main;
  34. public GameObject Sence;
  35. public GameObject daoju;
  36. public GameObject video;
  37. public GameObject allimg;
  38. public GameObject allimg2;
  39. public GameObject ar_1img;
  40. public GameObject ar_2img;
  41. public GameObject ar_1glb;
  42. public GameObject ar_2glb;
  43. public RawImage bigbg;
  44. public GameObject GamePIntu;
  45. public List<GameObject> daojulist;
  46. public ImageTracker itracker;
  47. public WorldTracker wtracker;
  48. public GameObject tuodongbg;
  49. public GameObject shibie;
  50. public GameObject Inputgo;
  51. public GameObject duihuakuang;
  52. TestConfigExcelItem nowTaskItem;
  53. public GameObject xiansuo;
  54. public int nowTaskIndex;
  55. public int nowTaskCt;
  56. public GameObject main_click;
  57. public GameObject inputbt;
  58. private void Awake()
  59. {
  60. // PlayerPrefs.DeleteAll();
  61. StartCoroutine(InitGame());
  62. }
  63. public void saveindex(bool isFinish)
  64. {
  65. if (PlayerPrefs.GetInt("isend")!=1)
  66. {
  67. PlayerPrefs.SetInt("nowTaskCt", nowTaskCt);
  68. PlayerPrefs.SetInt("nowTaskIndex", nowTaskIndex);
  69. if (isFinish)
  70. {
  71. PlayerPrefs.SetInt("isend", 1);
  72. }
  73. }
  74. }
  75. private IEnumerator InitGame()
  76. {
  77. Main.SetActive(false);
  78. Sence.SetActive(false);
  79. // yield return new WaitForSeconds(1f);
  80. // ar.PauseCamera();
  81. // yield return new WaitForSeconds(2f);
  82. yield return StartCoroutine(InitUser());
  83. if (PlayerPrefs.GetInt("isend") != 1)
  84. {
  85. backgo.SetActive(false);
  86. myInfo.taskIndex = PlayerPrefs.GetInt("nowTaskIndex");
  87. myInfo.taskCt = PlayerPrefs.GetInt("nowTaskCt");
  88. nowTaskIndex = myInfo.taskIndex;
  89. nowTaskCt = myInfo.taskCt;
  90. ntconfig = tconfigs[nowTaskCt];
  91. nowTaskItem = ntconfig.items[nowTaskIndex];
  92. yield return StartCoroutine(checktaskSence(nowTaskItem));
  93. }
  94. else
  95. {
  96. backgo.SetActive(false);
  97. Main.SetActive(true);
  98. Sence.SetActive(false);
  99. }
  100. }
  101. bool isclickxumu;
  102. public void gotoxumu()
  103. {
  104. SetWebGLText.Instance.clearTexture();
  105. if (PlayerPrefs.GetInt("isend") == 1)
  106. {
  107. backgo.SetActive(true);
  108. isEnd = false;
  109. isclickxumu = true;
  110. nowTaskCt = 0;
  111. nowTaskIndex = -1;
  112. ntconfig = tconfigs[nowTaskCt];
  113. nowTaskItem = ntconfig.items[0];
  114. nextTask();
  115. Main.SetActive(false);
  116. Sence.SetActive(true);
  117. }
  118. }
  119. public void gotodadian()
  120. {
  121. isEnd = false;
  122. isclickxumu = false;
  123. nowTaskCt = 1;
  124. nowTaskIndex = 8;
  125. ntconfig = tconfigs[nowTaskCt];
  126. nowTaskItem = ntconfig.items[nowTaskIndex];
  127. nextTask();
  128. Main.SetActive(false);
  129. Sence.SetActive(true);
  130. backgo.SetActive(PlayerPrefs.GetInt("isend") == 1);
  131. }
  132. GameObject hc;
  133. public RawImage lihui;
  134. public List<GameObject> items;
  135. public GameObject rawjingzi;
  136. private IEnumerator checktaskSence(TestConfigExcelItem ti)
  137. {
  138. backgo.SetActive(PlayerPrefs.GetInt("isend") == 1);
  139. if(hc)
  140. {
  141. Destroy(hc);
  142. hc = null;
  143. }
  144. dialogueText.DOKill();
  145. dialogueText.text="";
  146. dialogueText.DOText(ti.info,2f);
  147. // dialogueText.text = ti.info;
  148. if(iidex==1&& ti.info2!="")
  149. dialogueText.DOText(ti.info2,2f);
  150. //dialogueText.text = ti.info2;
  151. getTexture(ti.bgName, (tex) => {
  152. if (tex)
  153. {
  154. bg.texture = tex;
  155. bg.color = new Color(bg.color.r, bg.color.g, bg.color.b, 1);
  156. }
  157. else
  158. {
  159. bg.color = new Color(bg.color.r, bg.color.g, bg.color.b, 0);
  160. }
  161. });
  162. getTexture(ti.lihui, (tex) => {
  163. if (tex)
  164. {
  165. lihui.GetComponent<RectTransform>().sizeDelta = new Vector2(tex.width, tex.height);
  166. lihui.texture = tex;
  167. lihui.color = new Color(bg.color.r, bg.color.g, bg.color.b, 1);
  168. lihui.transform.localScale = new Vector3(ti.lihuisize, ti.lihuisize, ti.lihuisize);
  169. }
  170. else
  171. {
  172. lihui.color = new Color(bg.color.r, bg.color.g, bg.color.b, 0);
  173. }
  174. });
  175. getTexture(ti.imgName, (tex) => {
  176. if (tex)
  177. {
  178. imgbg.texture = tex;
  179. imgbg.color = new Color(bg.color.r, bg.color.g, bg.color.b, 1);
  180. }
  181. else
  182. {
  183. imgbg.color = new Color(bg.color.r, bg.color.g, bg.color.b, 0);
  184. }
  185. duihuakuang.SetActive(ti.centerName != "one_end");
  186. });
  187. getTexture(ti.centerName, (tex) => {
  188. if (tex)
  189. {
  190. centerbg.GetComponent<RectTransform>().sizeDelta = new Vector2(tex.width, tex.height);
  191. centerbg.texture = tex;
  192. centerbg.color = new Color(bg.color.r, bg.color.g, bg.color.b, 1);
  193. centerbg.gameObject.SetActive(true);
  194. centerbg.transform.localScale = new Vector3(ti.centersize, ti.centersize, ti.centersize);
  195. }
  196. else
  197. {
  198. centerbg.color = new Color(bg.color.r, bg.color.g, bg.color.b, 0);
  199. }
  200. });
  201. GetBD(ti.perbName, (go) => {
  202. if(go)
  203. {
  204. go.SetActive(true);
  205. go.transform.parent = prebg.transform;
  206. go.transform.localPosition = Vector3.zero;
  207. go.transform.localEulerAngles = Vector3.zero;
  208. go.transform.localScale = Vector3.one;
  209. hc = go;
  210. }
  211. });
  212. if(ti.type != TaskType.ARTask)
  213. {
  214. bigbg.color = new Color(bigbg.color.r, bigbg.color.g, bigbg.color.b, 1);
  215. imgbg.color = new Color(imgbg.color.r, imgbg.color.g, imgbg.color.b, 1);
  216. }
  217. for (int i = 0; i < items.Count; i++)
  218. {
  219. if(ti.daojus.Length>i)
  220. {
  221. items[i].SetActive(ti.daojus[i]);
  222. }else
  223. {
  224. items[i].SetActive(false);
  225. }
  226. }
  227. dialogueText.GetComponent<Button>().enabled = ti.isTextNext;
  228. ar_1glb.SetActive(false);
  229. ar_2glb.SetActive(false);
  230. inputbt.SetActive(false);
  231. HtmlBG.SetActive(ti.type!=TaskType.ARTask);
  232. shibie.SetActive(false);
  233. switch (ti.type)
  234. {
  235. case TaskType.Dialogue:
  236. ar_2img.SetActive(false);
  237. ar_1img.SetActive(false);
  238. Main.SetActive(false);
  239. Sence.SetActive(true);
  240. allimg.SetActive(ti.isClose);
  241. Closebt.SetActive(ti.isClosebtShow);
  242. allimg.GetComponent<Button>().enabled = !ti.isClosebtShow;
  243. tuodongbg.SetActive(ti.isTuo);
  244. rawjingzi.SetActive(ti.isTuo);
  245. break;
  246. case TaskType.Xiansuo:
  247. Main.SetActive(false);
  248. Sence.SetActive(true);
  249. xiansuo.SetActive(true);
  250. break;
  251. case TaskType.ARTask:
  252. WebGLShowWx(ti.info);
  253. nextTask();
  254. // Main.SetActive(false);
  255. break;
  256. case TaskType.SkipSence:
  257. switch (ti.guochang)
  258. {
  259. case "daoju":
  260. daoju.SetActive(true);
  261. Invoke("nextTask", 1f);
  262. break;
  263. case "main":
  264. Main.SetActive(true);
  265. Sence.SetActive(false);
  266. main_click.SetActive(true);
  267. break;
  268. default:
  269. if(ti.guochang!="")
  270. {
  271. GameObject v = video;
  272. if (ti.isguochangskip)
  273. v = end2;
  274. v.SetActive(true);
  275. v.GetComponent<NVideoPlayer>().enabled = true;
  276. yield return null;
  277. v.GetComponent<NVideoPlayer>().Config.PlayOnAwake = false;
  278. v.GetComponent<NVideoPlayer>().Config.VideoSrcPath = Application.streamingAssetsPath + "/" + ti.guochang + ".mp4";
  279. v.GetComponent<NVideoPlayer>().Config.LoopPlayer = ti.isguochangskip;
  280. if (ti.isguochangskip)
  281. v.GetComponent<NVideoPlayer>().Unmute();
  282. else
  283. v.GetComponent<NVideoPlayer>().Mute();
  284. yield return null;
  285. if(!v.GetComponent<NVideoPlayer>().enabled)
  286. {
  287. v.GetComponent<NVideoPlayer>().enabled = true;
  288. yield return null;
  289. if (!v.GetComponent<NVideoPlayer>().IsPlaying)
  290. v.GetComponent<NVideoPlayer>().Play();
  291. }
  292. else{
  293. if (!v.GetComponent<NVideoPlayer>().IsPlaying)
  294. v.GetComponent<NVideoPlayer>().Play();
  295. }
  296. if (!ti.isguochangskip)
  297. {
  298. duihuakuang.SetActive(ti.centerName != "one_end");
  299. Invoke("videoclose", 3f);
  300. }
  301. }
  302. else
  303. {
  304. }
  305. break;
  306. }
  307. break;
  308. case TaskType.GameTask:
  309. switch (ti.info)
  310. {
  311. case "pintu":
  312. GamePIntu.SetActive(true);
  313. break;
  314. }
  315. break;
  316. case TaskType.InputTask:
  317. inputbt.SetActive(true);
  318. ar_2img.SetActive(false);
  319. ar_1img.SetActive(false);
  320. Main.SetActive(false);
  321. Sence.SetActive(true);
  322. allimg.SetActive(ti.isClose);
  323. Closebt.SetActive(ti.isClosebtShow);
  324. allimg.GetComponent<Button>().enabled = !ti.isClosebtShow;
  325. tuodongbg.SetActive(ti.isTuo);
  326. Inputgo.SetActive(true);
  327. break;
  328. case TaskType.ChooseTask:
  329. ar_2img.SetActive(false);
  330. ar_1img.SetActive(false);
  331. Main.SetActive(false);
  332. Sence.SetActive(true);
  333. allimg.SetActive(ti.isClose);
  334. dialogueText.GetComponent<Button>().enabled =false;
  335. Closebt.SetActive(ti.isClosebtShow);
  336. allimg.GetComponent<Button>().enabled = !ti.isClosebtShow;
  337. choosego.SetActive(true);
  338. tuodongbg.SetActive(ti.isTuo);
  339. break;
  340. }
  341. yield return null;
  342. if(ti.texiao!=null&& ti.texiao!="")
  343. {
  344. nongwu.SetActive(true);
  345. yield return null;
  346. string path = nongwu.GetComponent<NVideoPlayer>().Config.VideoSrcPath;
  347. string[] parr = nongwu.GetComponent<NVideoPlayer>().Config.VideoSrcPath.Split('/');
  348. string pname = parr[parr.Length - 1];
  349. if (pname.Split('.')[0] != ti.texiao)
  350. {
  351. nongwu.GetComponent<NVideoPlayer>().Config.VideoSrcPath = Application.streamingAssetsPath + "/" + ti.texiao + ".mp4";
  352. nongwu.GetComponent<NVideoPlayer>().Config.LoopPlayer = true;
  353. }
  354. nongwu.GetComponent<NVideoPlayer>().Mute();
  355. yield return null;
  356. //
  357. if (!nongwu.GetComponent<NVideoPlayer>().IsPlaying)
  358. nongwu.GetComponent<NVideoPlayer>().Play();
  359. }
  360. else
  361. {
  362. nongwu.SetActive(false);
  363. }
  364. //
  365. }
  366. int arcount;
  367. bool isarinit=false;
  368. public GameObject textgo;
  369. public void arFound(string name)
  370. {
  371. if (arcount == 1 && name == "ar_1")
  372. {
  373. wtracker.PlaceOrigin();
  374. textgo.SetActive(false);
  375. nextTask();
  376. }
  377. if (arcount == 2 && name == "ar_2")
  378. {
  379. wtracker.PlaceOrigin();
  380. textgo.SetActive(false);
  381. nextTask();
  382. }
  383. }
  384. public GameObject errortext;
  385. public void checkinput()
  386. {
  387. if(Inputgo.GetComponent<InputField>().text == nowTaskItem.taskcheck)
  388. {
  389. Inputgo.SetActive(false);
  390. nextTask();
  391. }else
  392. {
  393. errortext.SetActive(true);
  394. }
  395. }
  396. public void yanchi()
  397. {
  398. bigbg.color = new Color(bigbg.color.r, bigbg.color.g, bigbg.color.b, 0);
  399. imgbg.color = new Color(imgbg.color.r, imgbg.color.g, imgbg.color.b, 0);
  400. }
  401. public GameObject showBT;
  402. public void videoclose()
  403. {
  404. showBT.SetActive(true);
  405. // showBT.GetComponent<>(true);
  406. //nextTask();
  407. }
  408. void jieshu()
  409. {
  410. Main.SetActive(true);
  411. Sence.SetActive(false);
  412. saveindex(true);
  413. }
  414. void isClickfalse()
  415. {
  416. isClick=false;
  417. }
  418. bool isClick =false;
  419. bool isEnd;
  420. public void nextTask()
  421. {
  422. if(isClick)
  423. {
  424. //return;
  425. }
  426. Invoke("isClickfalse",1f);
  427. isClick=true;
  428. if(nowTaskItem.type == TaskType.InputTask)
  429. {
  430. Inputgo.GetComponent<InputField>().text = "";
  431. }
  432. if(!allimg.activeSelf||(allimg.activeSelf&& nowTaskItem.isClose)|| nowTaskItem.type== TaskType.ChooseTask)
  433. {
  434. if (nowTaskIndex + 1 >= ntconfig.items.Length)
  435. {
  436. if(nowTaskCt+1>=tconfigs.Count)
  437. {
  438. if(!isEnd)
  439. {
  440. Invoke("jieshu", 2f);
  441. isEnd = true;
  442. }
  443. }
  444. else
  445. {
  446. if(!isclickxumu)
  447. {
  448. nowTaskCt++;
  449. ntconfig = tconfigs[nowTaskCt];
  450. nowTaskIndex = 0;
  451. nowTaskItem = ntconfig.items[nowTaskIndex];
  452. StartCoroutine(checktaskSence(nowTaskItem));
  453. saveindex(false);
  454. }
  455. else
  456. {
  457. if (!isEnd)
  458. {
  459. Invoke("jieshu", 2f);
  460. isEnd = true;
  461. }
  462. // Main.SetActive(true);
  463. // Sence.SetActive(false);
  464. }
  465. }
  466. }
  467. else
  468. {
  469. nowTaskIndex++;
  470. nowTaskItem = ntconfig.items[nowTaskIndex];
  471. StartCoroutine(checktaskSence(nowTaskItem));
  472. if(nowTaskItem.type == TaskType.Dialogue)
  473. saveindex(false);
  474. }
  475. }
  476. }
  477. public void gotomain()
  478. {
  479. backgo.SetActive(false);
  480. Main.SetActive(true);
  481. Sence.SetActive(false);
  482. centerbg.color = new Color(bg.color.r, bg.color.g, bg.color.b, 0);
  483. }
  484. public static void getTexture(string m_url, Action<Texture2D> callback)
  485. {
  486. if (m_url == "" || m_url == null)
  487. {
  488. callback.Invoke(null);
  489. }
  490. else
  491. {
  492. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath + "/ui", m_url);
  493. SetWebGLText.Instance.GetTexture(filePath + ".png", (Texture2D t2d) => {
  494. callback.Invoke(t2d);
  495. });
  496. }
  497. }
  498. public static void GetBD(string m_url, Action<GameObject> callback)
  499. {
  500. if (m_url == "" || m_url == null)
  501. {
  502. callback.Invoke(null);
  503. }
  504. else
  505. {
  506. SetWebGLText.Instance.GetBD(m_url, (GameObject t2d) => {
  507. callback.Invoke(t2d);
  508. });
  509. }
  510. }
  511. public void checkisNextTask()
  512. {
  513. //nextTask();
  514. }
  515. public void showdaoju(string urlname)
  516. {
  517. allimg2.SetActive(true);
  518. Closebt2.SetActive(true);
  519. allimg2.GetComponent<Button>().enabled = false;
  520. getTexture(urlname, (tex) => {
  521. if (tex)
  522. {
  523. centerbg2.GetComponent<RectTransform>().sizeDelta = new Vector2(tex.width, tex.height);
  524. centerbg2.texture = tex;
  525. centerbg2.color = new Color(bg.color.r, bg.color.g, bg.color.b, 1);
  526. centerbg2.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
  527. centerbg2.gameObject.SetActive(true);
  528. }
  529. else
  530. {
  531. centerbg2.color = new Color(bg.color.r, bg.color.g, bg.color.b, 0);
  532. }
  533. });
  534. }
  535. public static void loadUrl(string ModelURL, Action<AssetLoaderContext> OnLoad, Action<AssetLoaderContext> OnMaterialsLoad, Action<AssetLoaderContext, float> OnProgress, Action<IContextualizedError> OnError, GameObject go = null)
  536. {
  537. var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  538. var webRequest = AssetDownloader.CreateWebRequest(ModelURL);
  539. AssetDownloader.LoadModelFromUri(webRequest, OnLoad, OnMaterialsLoad, OnProgress, OnError, go, assetLoaderOptions);
  540. }
  541. public static void loadFile(string ModelPath, Action<AssetLoaderContext> OnLoad, Action<AssetLoaderContext> OnMaterialsLoad, Action<AssetLoaderContext, float> OnProgress, Action<IContextualizedError> OnError, GameObject go = null)
  542. {
  543. var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  544. AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, go, assetLoaderOptions);
  545. }
  546. private IEnumerator InitUser()
  547. {
  548. myInfo = new UserInfo();
  549. yield return null;
  550. }
  551. public int iidex;
  552. public void chooseitem(int index)
  553. {
  554. iidex = index;
  555. nextTask();
  556. }
  557. public GameObject choosego;
  558. public GameObject end2;
  559. public GameObject nongwu;
  560. }