TaskManager.cs 17 KB

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