uGUITutorialPanel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. Transform uirootTrans = this.GetUIRootTrans();
  100. if (uirootTrans != null)
  101. {
  102. base.transform.SetParent(uirootTrans, false);
  103. }
  104. else
  105. {
  106. NDebug.Assert("SystemUI Rootが見つかりませんでした", false);
  107. }
  108. this.m_CacheSpriteArray = new Dictionary<string, Sprite>();
  109. this.m_CallbackClose = callback;
  110. this.m_ButtonOK.interactable = true;
  111. this.m_ButtonOK.onClick.AddListener(delegate
  112. {
  113. this.OnClose(false, true);
  114. });
  115. this.ReadCSV(type);
  116. if (this.IsMultiMode())
  117. {
  118. this.m_ParentMultiMode.SetActive(true);
  119. this.m_ParentSingleMode.SetActive(false);
  120. int itemCount = this.m_strFileNameArray.Length;
  121. this.m_ListViewerButtons.Show<Toggle>(itemCount, delegate(int i, Toggle toggle)
  122. {
  123. KeyValuePair<string, string> keyValuePair = this.m_strFileNameArray[i];
  124. if (this.index == i)
  125. {
  126. toggle.isOn = true;
  127. }
  128. Text componentInChildren = toggle.GetComponentInChildren<Text>();
  129. if (!string.IsNullOrEmpty(keyValuePair.Value))
  130. {
  131. componentInChildren.text = keyValuePair.Value;
  132. }
  133. else
  134. {
  135. componentInChildren.text = string.Format("(!項目名無し).{1}", i);
  136. }
  137. toggle.onValueChanged.AddListener(delegate(bool value)
  138. {
  139. if (!value)
  140. {
  141. return;
  142. }
  143. this.OpenPage(i);
  144. });
  145. });
  146. }
  147. else
  148. {
  149. this.m_ParentMultiMode.SetActive(false);
  150. this.m_ParentSingleMode.SetActive(true);
  151. }
  152. this.OpenPage(0);
  153. uGUICanvasFade fade = base.GetComponent<uGUICanvasFade>();
  154. fade.alpha = 0f;
  155. fade.interactable = false;
  156. fade.FadeIn(0.5f, delegate
  157. {
  158. fade.interactable = true;
  159. });
  160. }
  161. private void OnClose(bool immediate = false, bool enableCallback = true)
  162. {
  163. if (immediate)
  164. {
  165. uGUITutorialPanel.m_Instance = null;
  166. UnityEngine.Object.Destroy(base.gameObject);
  167. if (enableCallback && this.m_CallbackClose != null)
  168. {
  169. this.m_CallbackClose();
  170. }
  171. }
  172. else
  173. {
  174. uGUICanvasFade component = base.GetComponent<uGUICanvasFade>();
  175. component.interactable = false;
  176. component.FadeOut(0.5f, delegate
  177. {
  178. uGUITutorialPanel.m_Instance = null;
  179. UnityEngine.Object.Destroy(this.gameObject);
  180. if (enableCallback && this.m_CallbackClose != null)
  181. {
  182. this.m_CallbackClose();
  183. }
  184. });
  185. }
  186. }
  187. private bool OpenPage(int pageNumber)
  188. {
  189. if (!this.IsChangeIndex(pageNumber))
  190. {
  191. Debug.LogFormat("{0} ページを開けなかった", new object[]
  192. {
  193. pageNumber
  194. });
  195. return false;
  196. }
  197. this.SetImage(this.m_strFileNameArray[pageNumber].Key);
  198. this.m_IsOpenedPageArray[pageNumber] = true;
  199. return true;
  200. }
  201. private bool TryChangeIndex(int changeIndex)
  202. {
  203. if (!this.IsChangeIndex(changeIndex))
  204. {
  205. return false;
  206. }
  207. this.index = changeIndex;
  208. return true;
  209. }
  210. private bool IsChangeIndex(int changeIndex)
  211. {
  212. int num = this.m_strFileNameArray.Length;
  213. return changeIndex >= 0 && changeIndex < num;
  214. }
  215. private void SetImage(string fileName)
  216. {
  217. Sprite sprite;
  218. if (this.m_CacheSpriteArray.ContainsKey(fileName))
  219. {
  220. sprite = this.m_CacheSpriteArray[fileName];
  221. }
  222. else
  223. {
  224. string text = this.strFileNamePath + fileName;
  225. sprite = this.CreateSprite(fileName + ".tex");
  226. if (sprite != null)
  227. {
  228. this.m_CacheSpriteArray.Add(fileName, sprite);
  229. }
  230. else
  231. {
  232. NDebug.Warning(string.Format("[uGUITutorialPanel]チュートリアル画像「{0}」が見つかりませんでした", fileName));
  233. }
  234. }
  235. if (this.IsMultiMode())
  236. {
  237. this.m_ImageMultiTutorial.sprite = sprite;
  238. }
  239. else
  240. {
  241. this.m_ImageSingleTutorial.sprite = sprite;
  242. }
  243. }
  244. private bool IsMultiMode()
  245. {
  246. return this.m_strFileNameArray.Length > 1;
  247. }
  248. private static bool IsTutorialPassed(string tutorialName)
  249. {
  250. string flagName = "__チュートリアルフラグ_" + tutorialName;
  251. Status status = GameMain.Instance.CharacterMgr.status;
  252. return status.GetFlag(flagName) > 0;
  253. }
  254. private static void SetTutorialFlag(string tutorialName)
  255. {
  256. string flagName = "__チュートリアルフラグ_" + tutorialName;
  257. Status status = GameMain.Instance.CharacterMgr.status;
  258. status.SetFlag(flagName, 1);
  259. }
  260. private Transform GetUIRootTrans()
  261. {
  262. Transform result = null;
  263. GameObject gameObject = GameObject.Find("SystemUI Root");
  264. if (gameObject)
  265. {
  266. result = gameObject.transform;
  267. }
  268. return result;
  269. }
  270. private void ReadCSV(string type)
  271. {
  272. string text = "tutorial_list.nei";
  273. if (!GameUty.FileSystem.IsExistentFile(text))
  274. {
  275. NDebug.Assert("表がありません。" + text, false);
  276. }
  277. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(text))
  278. {
  279. using (CsvParser csvParser = new CsvParser())
  280. {
  281. bool condition = csvParser.Open(afileBase);
  282. NDebug.Assert(condition, text + "\nopen failed.");
  283. int i = 0;
  284. List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
  285. for (i = 1; i < csvParser.max_cell_y; i++)
  286. {
  287. if (csvParser.IsCellToExistData(0, i))
  288. {
  289. string cellAsString = csvParser.GetCellAsString(0, i);
  290. if (cellAsString == type)
  291. {
  292. break;
  293. }
  294. }
  295. }
  296. for (int j = 2; j < csvParser.max_cell_x; j += 2)
  297. {
  298. if (csvParser.IsCellToExistData(j, i))
  299. {
  300. string cellAsString2 = csvParser.GetCellAsString(j, i);
  301. string cellAsString3 = csvParser.GetCellAsString(j + 1, i);
  302. if (!string.IsNullOrEmpty(cellAsString2))
  303. {
  304. list.Add(new KeyValuePair<string, string>(cellAsString2, cellAsString3));
  305. }
  306. }
  307. }
  308. this.m_strFileNameArray = list.ToArray();
  309. this.m_IsOpenedPageArray = new bool[this.m_strFileNameArray.Length];
  310. }
  311. }
  312. if (this.m_strFileNameArray.Length <= 0)
  313. {
  314. NDebug.Warning(string.Format("チュートリアル「{0}」の表データが空でした", type));
  315. }
  316. }
  317. private void OnApplicationQuit()
  318. {
  319. this.m_IsQuittingApplication = true;
  320. }
  321. private void OnDestroy()
  322. {
  323. if (this.m_IsQuittingApplication)
  324. {
  325. return;
  326. }
  327. if (uGUITutorialPanel.m_Instance != null && uGUITutorialPanel.m_Instance == this)
  328. {
  329. uGUITutorialPanel.m_Instance = null;
  330. }
  331. this.m_ImageMultiTutorial.sprite = null;
  332. this.m_ImageSingleTutorial.sprite = null;
  333. if (this.m_CacheSpriteArray != null)
  334. {
  335. int count = this.m_CacheSpriteArray.Count;
  336. List<Sprite> list = new List<Sprite>(this.m_CacheSpriteArray.Values);
  337. this.m_CacheSpriteArray.Clear();
  338. for (int i = 0; i < count; i++)
  339. {
  340. UnityEngine.Object.DestroyImmediate(list[i].texture);
  341. UnityEngine.Object.DestroyImmediate(list[i]);
  342. }
  343. list.Clear();
  344. }
  345. this.m_CallbackClose = null;
  346. }
  347. private Sprite CreateSprite(string fileName)
  348. {
  349. Sprite sprite = null;
  350. if (GameUty.FileSystem.IsExistentFile(fileName))
  351. {
  352. Texture2D texture2D = ImportCM.CreateTexture(fileName);
  353. sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
  354. sprite.name = fileName;
  355. }
  356. return sprite;
  357. }
  358. [SerializeField]
  359. private Button m_ButtonOK;
  360. [Header("ページ数単体時のオブジェクト")]
  361. [SerializeField]
  362. private GameObject m_ParentSingleMode;
  363. [SerializeField]
  364. private Image m_ImageSingleTutorial;
  365. [Header("ページ数複数時のオブジェクト")]
  366. [SerializeField]
  367. private GameObject m_ParentMultiMode;
  368. [SerializeField]
  369. private Image m_ImageMultiTutorial;
  370. [SerializeField]
  371. private uGUIListViewer m_ListViewerButtons;
  372. private KeyValuePair<string, string>[] m_strFileNameArray;
  373. private bool[] m_IsOpenedPageArray;
  374. private readonly string strFileNamePath = "SceneTutorial/Sprites/";
  375. private Dictionary<string, Sprite> m_CacheSpriteArray;
  376. private Action m_CallbackClose;
  377. private static List<string> m_SceneNameArray;
  378. private static uGUITutorialPanel m_Instance;
  379. private bool m_IsQuittingApplication;
  380. }