TaskManager.cs 17 KB

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