WfFadeJob.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class WfFadeJob : MonoBehaviour
  5. {
  6. public static int Create(InterfaceWfFade inObject, InterfaceWfFade outObject, float time, iTween.EaseType easeType = iTween.EaseType.easeOutSine)
  7. {
  8. GameObject gameObject = WfFadeJob.FindJobObject(new InterfaceWfFade[]
  9. {
  10. inObject,
  11. outObject
  12. });
  13. if (gameObject != null)
  14. {
  15. Debug.LogWarning("既にJobが実行されいているInterfaceWfFadeに対して、新たなJobの作成が指定されました");
  16. WfFadeJob.Finish(gameObject.GetComponent<WfFadeJob>().jobData.jobId);
  17. }
  18. WfFadeJob jobObject = WfFadeJob.GetJobObject();
  19. WfFadeJob.JobData jobData;
  20. jobData.jobId = jobObject.gameObject.GetInstanceID();
  21. jobData.inObject = inObject;
  22. jobData.outObject = outObject;
  23. jobData.easeType = easeType;
  24. jobData.time = time;
  25. jobObject.ExecJob(jobData);
  26. return jobData.jobId;
  27. }
  28. public static void Release()
  29. {
  30. List<GameObject> list = new List<GameObject>();
  31. GameObject jobObjectParent = WfFadeJob.GetJobObjectParent();
  32. for (int i = 0; i < jobObjectParent.transform.childCount; i++)
  33. {
  34. Transform child = jobObjectParent.transform.GetChild(i);
  35. if (child != null)
  36. {
  37. list.Add(child.gameObject);
  38. }
  39. }
  40. foreach (GameObject obj in list)
  41. {
  42. UnityEngine.Object.DestroyImmediate(obj);
  43. }
  44. jobObjectParent.transform.DetachChildren();
  45. }
  46. public static void Finish(int jobId)
  47. {
  48. GameObject gameObject = WfFadeJob.FindJobObject(jobId);
  49. WfFadeJob component = gameObject.GetComponent<WfFadeJob>();
  50. if (component != null)
  51. {
  52. component.OnUpdateValue(1f);
  53. component.OnCompleteValue();
  54. }
  55. }
  56. private static GameObject GetJobObjectParent()
  57. {
  58. GameObject gameObject = GameObject.Find("__WfFadeJob__");
  59. if (gameObject == null)
  60. {
  61. gameObject = new GameObject();
  62. gameObject.name = "__WfFadeJob__";
  63. }
  64. return gameObject;
  65. }
  66. private static GameObject FindJobObject(int jobId)
  67. {
  68. string b = jobId.ToString();
  69. Transform transform = WfFadeJob.GetJobObjectParent().transform;
  70. GameObject result = null;
  71. for (int i = 0; i < transform.childCount; i++)
  72. {
  73. if (transform.GetChild(i).gameObject.name == b)
  74. {
  75. result = transform.GetChild(i).gameObject;
  76. break;
  77. }
  78. }
  79. return result;
  80. }
  81. private static GameObject FindJobObject(params InterfaceWfFade[] objectList)
  82. {
  83. if (objectList.Length <= 0)
  84. {
  85. return null;
  86. }
  87. Transform transform = WfFadeJob.GetJobObjectParent().transform;
  88. for (int i = 0; i < transform.childCount; i++)
  89. {
  90. GameObject gameObject = transform.GetChild(i).gameObject;
  91. if (!(gameObject == null) && !string.IsNullOrEmpty(gameObject.name))
  92. {
  93. WfFadeJob component = gameObject.GetComponent<WfFadeJob>();
  94. if (!(component == null))
  95. {
  96. for (int j = 0; j < objectList.Length; j++)
  97. {
  98. if (objectList[j] != null)
  99. {
  100. if ((component.jobData.inObject != null && component.jobData.inObject == objectList[j]) || (component.jobData.outObject != null && component.jobData.outObject == objectList[j]))
  101. {
  102. return component.gameObject;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. return null;
  110. }
  111. private static WfFadeJob GetJobObject()
  112. {
  113. GameObject jobObjectParent = WfFadeJob.GetJobObjectParent();
  114. GameObject gameObject = null;
  115. for (int i = 0; i < jobObjectParent.transform.childCount; i++)
  116. {
  117. if (string.IsNullOrEmpty(jobObjectParent.transform.GetChild(i).name))
  118. {
  119. gameObject = jobObjectParent.transform.GetChild(i).gameObject;
  120. break;
  121. }
  122. }
  123. if (gameObject == null)
  124. {
  125. gameObject = new GameObject();
  126. gameObject.transform.SetParent(jobObjectParent.transform, false);
  127. gameObject.AddComponent<WfFadeJob>();
  128. }
  129. gameObject.name = gameObject.GetInstanceID().ToString();
  130. return gameObject.GetComponent<WfFadeJob>();
  131. }
  132. protected void ExecJob(WfFadeJob.JobData jobData)
  133. {
  134. this.jobData = jobData;
  135. if (jobData.inObject != null)
  136. {
  137. jobData.inObject.OnStartedFadeIn();
  138. }
  139. if (jobData.outObject != null)
  140. {
  141. jobData.outObject.OnStartedFadeOut();
  142. }
  143. if (jobData.time <= 0f)
  144. {
  145. this.OnUpdateValue(1f);
  146. this.OnCompleteValue();
  147. }
  148. else
  149. {
  150. iTween.ValueTo(base.gameObject, iTween.Hash(new object[]
  151. {
  152. "easetype",
  153. jobData.easeType,
  154. "from",
  155. 0,
  156. "to",
  157. 1,
  158. "time",
  159. jobData.time,
  160. "delay",
  161. 0,
  162. "onUpdate",
  163. "OnUpdateValue",
  164. "onComplete",
  165. "OnCompleteValue"
  166. }));
  167. }
  168. }
  169. private void OnUpdateValue(float val)
  170. {
  171. if (this.jobData.inObject != null)
  172. {
  173. this.jobData.inObject.OnUpdateFadeIn(val);
  174. }
  175. if (this.jobData.outObject != null)
  176. {
  177. this.jobData.outObject.OnUpdateFadeOut(1f - val);
  178. }
  179. }
  180. private void OnCompleteValue()
  181. {
  182. iTween component = base.GetComponent<iTween>();
  183. if (component != null)
  184. {
  185. UnityEngine.Object.DestroyImmediate(component);
  186. }
  187. base.gameObject.name = string.Empty;
  188. if (this.jobData.inObject != null)
  189. {
  190. this.jobData.inObject.OnCompleteFadeIn();
  191. }
  192. if (this.jobData.outObject != null)
  193. {
  194. this.jobData.outObject.OnCompleteFadeOut();
  195. }
  196. }
  197. private WfFadeJob.JobData jobData;
  198. protected struct JobData
  199. {
  200. public int jobId;
  201. public InterfaceWfFade inObject;
  202. public InterfaceWfFade outObject;
  203. public iTween.EaseType easeType;
  204. public float time;
  205. }
  206. }