uGUITutorialPanel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using System.Collections.Generic;
  3. using PlayerStatus;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class uGUITutorialPanel : MonoBehaviour
  7. {
  8. public int index { get; private set; }
  9. public static bool IsExistTutorial(string sceneName)
  10. {
  11. if (uGUITutorialPanel.m_SceneNameArray == null || uGUITutorialPanel.m_SceneNameArray.Count <= 0)
  12. {
  13. uGUITutorialPanel.m_SceneNameArray = new List<string>();
  14. string text = "tutorial_list.nei";
  15. if (!GameUty.FileSystem.IsExistentFile(text))
  16. {
  17. NDebug.Assert("表がありません。" + text, false);
  18. }
  19. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(text))
  20. {
  21. using (CsvParser csvParser = new CsvParser())
  22. {
  23. bool condition = csvParser.Open(afileBase);
  24. NDebug.Assert(condition, text + "\nopen failed.");
  25. int i = 0;
  26. List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
  27. for (i = 1; i < csvParser.max_cell_y; i++)
  28. {
  29. if (csvParser.IsCellToExistData(0, i))
  30. {
  31. string cellAsString = csvParser.GetCellAsString(0, i);
  32. uGUITutorialPanel.m_SceneNameArray.Add(cellAsString);
  33. }
  34. }
  35. }
  36. }
  37. }
  38. return uGUITutorialPanel.m_SceneNameArray.Contains(sceneName);
  39. }
  40. public static void OpenTutorial(string sceneName, Action closeCallback = null, bool absoluteCall = false)
  41. {
  42. if (string.IsNullOrEmpty(sceneName))
  43. {
  44. NDebug.Assert("チュートリアルの種類に空文字が指定されました", false);
  45. }
  46. if (uGUITutorialPanel.IsOpened())
  47. {
  48. NDebug.Warning("既にチュートリアル用パネルが開いています。");
  49. return;
  50. }
  51. if (!uGUITutorialPanel.IsExistTutorial(sceneName))
  52. {
  53. NDebug.Warning(string.Format("チュートリアル名「{0}」は存在しません。\n終了処理を実行します。", sceneName));
  54. if (closeCallback != null)
  55. {
  56. closeCallback();
  57. }
  58. return;
  59. }
  60. if (!absoluteCall && uGUITutorialPanel.IsTutorialPassed(sceneName))
  61. {
  62. Debug.Log("既に通過しているチュートリアルだったので、チュートリアル用UIは表示しません。\n終了時のコールバックを実行します。");
  63. if (closeCallback != null)
  64. {
  65. closeCallback();
  66. }
  67. return;
  68. }
  69. string path = "SceneTutorial/Canvas Tutorial Panel";
  70. GameObject gameObject = Resources.Load<GameObject>(path);
  71. if (!gameObject)
  72. {
  73. NDebug.Warning("チュートリアルパネルのプレハブが見つかりませんでした。\n終了時のコールバックを実行します。");
  74. if (closeCallback != null)
  75. {
  76. closeCallback();
  77. }
  78. return;
  79. }
  80. GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(gameObject);
  81. uGUITutorialPanel.m_Instance = gameObject2.GetComponent<uGUITutorialPanel>();
  82. uGUITutorialPanel.m_Instance.Initialize(sceneName, closeCallback);
  83. uGUITutorialPanel.SetTutorialFlag(sceneName);
  84. }
  85. public static void CloseTutorial(bool immediate = false, bool enableCallback = true)
  86. {
  87. if (!uGUITutorialPanel.IsOpened())
  88. {
  89. return;
  90. }
  91. uGUITutorialPanel.m_Instance.OnClose(immediate, enableCallback);
  92. }
  93. public static bool IsOpened()
  94. {
  95. return uGUITutorialPanel.m_Instance != null;
  96. }
  97. private void Initialize(string type, Action callback)
  98. {
  99. uGUITutorialPanel.<Initialize>c__AnonStorey1 <Initialize>c__AnonStorey = new uGUITutorialPanel.<Initialize>c__AnonStorey1();
  100. <Initialize>c__AnonStorey.$this = this;
  101. Transform uirootTrans = this.GetUIRootTrans();
  102. if (uirootTrans != null)
  103. {
  104. base.transform.SetParent(uirootTrans, false);
  105. }
  106. else
  107. {
  108. NDebug.Assert("SystemUI Rootが見つかりませんでした", false);
  109. }
  110. this.m_CacheSpriteArray = new Dictionary<string, Sprite>();
  111. this.m_CallbackClose = callback;
  112. this.m_ButtonOK.interactable = true;
  113. this.m_ButtonOK.onClick.AddListener(delegate()
  114. {
  115. <Initialize>c__AnonStorey.$this.OnClose(false, true);
  116. });
  117. this.ReadCSV(type);
  118. if (this.IsMultiMode())
  119. {
  120. this.m_ParentMultiMode.SetActive(true);
  121. this.m_ParentSingleMode.SetActive(false);
  122. int itemCount = this.m_strFileNameArray.Length;
  123. this.m_ListViewerButtons.Show<Toggle>(itemCount, delegate(int i, Toggle toggle)
  124. {
  125. KeyValuePair<string, string> keyValuePair = <Initialize>c__AnonStorey.$this.m_strFileNameArray[i];
  126. if (<Initialize>c__AnonStorey.$this.index == i)
  127. {
  128. toggle.isOn = true;
  129. }
  130. Text componentInChildren = toggle.GetComponentInChildren<Text>();
  131. if (!string.IsNullOrEmpty(keyValuePair.Value))
  132. {
  133. componentInChildren.text = keyValuePair.Value;
  134. }
  135. else
  136. {
  137. componentInChildren.text = string.Format("(!項目名無し).{1}", i);
  138. }
  139. toggle.onValueChanged.AddListener(delegate(bool value)
  140. {
  141. if (!value)
  142. {
  143. return;
  144. }
  145. <Initialize>c__AnonStorey.OpenPage(i);
  146. });
  147. });
  148. }
  149. else
  150. {
  151. this.m_ParentMultiMode.SetActive(false);
  152. this.m_ParentSingleMode.SetActive(true);
  153. }
  154. this.OpenPage(0);
  155. <Initialize>c__AnonStorey.fade = base.GetComponent<uGUICanvasFade>();
  156. <Initialize>c__AnonStorey.fade.alpha = 0f;
  157. <Initialize>c__AnonStorey.fade.interactable = false;
  158. <Initialize>c__AnonStorey.fade.FadeIn(0.5f, delegate
  159. {
  160. <Initialize>c__AnonStorey.fade.interactable = true;
  161. });
  162. }
  163. private void OnClose(bool immediate = false, bool enableCallback = true)
  164. {
  165. if (immediate)
  166. {
  167. uGUITutorialPanel.m_Instance = null;
  168. UnityEngine.Object.Destroy(base.gameObject);
  169. if (enableCallback && this.m_CallbackClose != null)
  170. {
  171. this.m_CallbackClose();
  172. }
  173. }
  174. else
  175. {
  176. uGUICanvasFade component = base.GetComponent<uGUICanvasFade>();
  177. component.interactable = false;
  178. component.FadeOut(0.5f, delegate
  179. {
  180. uGUITutorialPanel.m_Instance = null;
  181. UnityEngine.Object.Destroy(this.gameObject);
  182. if (enableCallback && this.m_CallbackClose != null)
  183. {
  184. this.m_CallbackClose();
  185. }
  186. });
  187. }
  188. }
  189. private bool OpenPage(int pageNumber)
  190. {
  191. if (!this.IsChangeIndex(pageNumber))
  192. {
  193. Debug.LogFormat("{0} ページを開けなかった", new object[]
  194. {
  195. pageNumber
  196. });
  197. return false;
  198. }
  199. this.SetImage(this.m_strFileNameArray[pageNumber].Key);
  200. this.m_IsOpenedPageArray[pageNumber] = true;
  201. return true;
  202. }
  203. private bool TryChangeIndex(int changeIndex)
  204. {
  205. if (!this.IsChangeIndex(changeIndex))
  206. {
  207. return false;
  208. }
  209. this.index = changeIndex;
  210. return true;
  211. }
  212. private bool IsChangeIndex(int changeIndex)
  213. {
  214. int num = this.m_strFileNameArray.Length;
  215. return changeIndex >= 0 && changeIndex < num;
  216. }
  217. private void SetImage(string fileName)
  218. {
  219. Sprite sprite;
  220. if (this.m_CacheSpriteArray.ContainsKey(fileName))
  221. {
  222. sprite = this.m_CacheSpriteArray[fileName];
  223. }
  224. else
  225. {
  226. string text = this.strFileNamePath + fileName;
  227. sprite = this.CreateSprite(fileName + ".tex");
  228. if (sprite != null)
  229. {
  230. this.m_CacheSpriteArray.Add(fileName, sprite);
  231. }
  232. else
  233. {
  234. NDebug.Warning(string.Format("[uGUITutorialPanel]チュートリアル画像「{0}」が見つかりませんでした", fileName));
  235. }
  236. }
  237. if (this.IsMultiMode())
  238. {
  239. this.m_ImageMultiTutorial.sprite = sprite;
  240. }
  241. else
  242. {
  243. this.m_ImageSingleTutorial.sprite = sprite;
  244. }
  245. }
  246. private bool IsMultiMode()
  247. {
  248. return this.m_strFileNameArray.Length > 1;
  249. }
  250. private static bool IsTutorialPassed(string tutorialName)
  251. {
  252. string flagName = "__チュートリアルフラグ_" + tutorialName;
  253. Status status = GameMain.Instance.CharacterMgr.status;
  254. return status.GetFlag(flagName) > 0;
  255. }
  256. private static void SetTutorialFlag(string tutorialName)
  257. {
  258. string flagName = "__チュートリアルフラグ_" + tutorialName;
  259. Status status = GameMain.Instance.CharacterMgr.status;
  260. status.SetFlag(flagName, 1);
  261. }
  262. private Transform GetUIRootTrans()
  263. {
  264. Transform result = null;
  265. GameObject gameObject = GameObject.Find("SystemUI Root");
  266. if (gameObject)
  267. {
  268. result = gameObject.transform;
  269. }
  270. return result;
  271. }
  272. private void ReadCSV(string type)
  273. {
  274. string text = "tutorial_list.nei";
  275. if (!GameUty.FileSystem.IsExistentFile(text))
  276. {
  277. NDebug.Assert("表がありません。" + text, false);
  278. }
  279. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(text))
  280. {
  281. using (CsvParser csvParser = new CsvParser())
  282. {
  283. bool condition = csvParser.Open(afileBase);
  284. NDebug.Assert(condition, text + "\nopen failed.");
  285. int i = 0;
  286. List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
  287. for (i = 1; i < csvParser.max_cell_y; i++)
  288. {
  289. if (csvParser.IsCellToExistData(0, i))
  290. {
  291. string cellAsString = csvParser.GetCellAsString(0, i);
  292. if (cellAsString == type)
  293. {
  294. break;
  295. }
  296. }
  297. }
  298. for (int j = 2; j < csvParser.max_cell_x; j += 2)
  299. {
  300. if (csvParser.IsCellToExistData(j, i))
  301. {
  302. string cellAsString2 = csvParser.GetCellAsString(j, i);
  303. string cellAsString3 = csvParser.GetCellAsString(j + 1, i);
  304. if (!string.IsNullOrEmpty(cellAsString2))
  305. {
  306. list.Add(new KeyValuePair<string, string>(cellAsString2, cellAsString3));
  307. }
  308. }
  309. }
  310. this.m_strFileNameArray = list.ToArray();
  311. this.m_IsOpenedPageArray = new bool[this.m_strFileNameArray.Length];
  312. }
  313. }
  314. if (this.m_strFileNameArray.Length <= 0)
  315. {
  316. NDebug.Warning(string.Format("チュートリアル「{0}」の表データが空でした", type));
  317. }
  318. }
  319. private void OnApplicationQuit()
  320. {
  321. this.m_IsQuittingApplication = true;
  322. }
  323. private void OnDestroy()
  324. {
  325. if (this.m_IsQuittingApplication)
  326. {
  327. return;
  328. }
  329. if (uGUITutorialPanel.m_Instance != null && uGUITutorialPanel.m_Instance == this)
  330. {
  331. uGUITutorialPanel.m_Instance = null;
  332. }
  333. this.m_ImageMultiTutorial.sprite = null;
  334. this.m_ImageSingleTutorial.sprite = null;
  335. if (this.m_CacheSpriteArray != null)
  336. {
  337. int count = this.m_CacheSpriteArray.Count;
  338. List<Sprite> list = new List<Sprite>(this.m_CacheSpriteArray.Values);
  339. this.m_CacheSpriteArray.Clear();
  340. for (int i = 0; i < count; i++)
  341. {
  342. UnityEngine.Object.DestroyImmediate(list[i].texture);
  343. UnityEngine.Object.DestroyImmediate(list[i]);
  344. }
  345. list.Clear();
  346. }
  347. this.m_CallbackClose = null;
  348. }
  349. private Sprite CreateSprite(string fileName)
  350. {
  351. Sprite sprite = null;
  352. if (GameUty.FileSystem.IsExistentFile(fileName))
  353. {
  354. Texture2D texture2D = ImportCM.CreateTexture(fileName);
  355. sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
  356. sprite.name = fileName;
  357. }
  358. return sprite;
  359. }
  360. [SerializeField]
  361. private Button m_ButtonOK;
  362. [Header("ページ数単体時のオブジェクト")]
  363. [SerializeField]
  364. private GameObject m_ParentSingleMode;
  365. [SerializeField]
  366. private Image m_ImageSingleTutorial;
  367. [Header("ページ数複数時のオブジェクト")]
  368. [SerializeField]
  369. private GameObject m_ParentMultiMode;
  370. [SerializeField]
  371. private Image m_ImageMultiTutorial;
  372. [SerializeField]
  373. private uGUIListViewer m_ListViewerButtons;
  374. private KeyValuePair<string, string>[] m_strFileNameArray;
  375. private bool[] m_IsOpenedPageArray;
  376. private readonly string strFileNamePath = "SceneTutorial/Sprites/";
  377. private Dictionary<string, Sprite> m_CacheSpriteArray;
  378. private Action m_CallbackClose;
  379. private static List<string> m_SceneNameArray;
  380. private static uGUITutorialPanel m_Instance;
  381. private bool m_IsQuittingApplication;
  382. }