TaskManager.cs 16 KB

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