TaskManager.cs 17 KB

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