TaskManager.cs 17 KB

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