using System; using System.Collections.Generic; using UnityEngine; public class WfFadeJob : MonoBehaviour { public static int Create(InterfaceWfFade inObject, InterfaceWfFade outObject, float time, iTween.EaseType easeType = iTween.EaseType.easeOutSine) { GameObject gameObject = WfFadeJob.FindJobObject(new InterfaceWfFade[] { inObject, outObject }); if (gameObject != null) { Debug.LogWarning("既にJobが実行されいているInterfaceWfFadeに対して、新たなJobの作成が指定されました"); WfFadeJob.Finish(gameObject.GetComponent().jobData.jobId); } WfFadeJob jobObject = WfFadeJob.GetJobObject(); WfFadeJob.JobData jobData; jobData.jobId = jobObject.gameObject.GetInstanceID(); jobData.inObject = inObject; jobData.outObject = outObject; jobData.easeType = easeType; jobData.time = time; jobObject.ExecJob(jobData); return jobData.jobId; } public static void Release() { List list = new List(); GameObject jobObjectParent = WfFadeJob.GetJobObjectParent(); for (int i = 0; i < jobObjectParent.transform.childCount; i++) { Transform child = jobObjectParent.transform.GetChild(i); if (child != null) { list.Add(child.gameObject); } } foreach (GameObject obj in list) { UnityEngine.Object.DestroyImmediate(obj); } jobObjectParent.transform.DetachChildren(); } public static void Finish(int jobId) { GameObject gameObject = WfFadeJob.FindJobObject(jobId); WfFadeJob component = gameObject.GetComponent(); if (component != null) { component.OnUpdateValue(1f); component.OnCompleteValue(); } } private static GameObject GetJobObjectParent() { GameObject gameObject = GameObject.Find("__WfFadeJob__"); if (gameObject == null) { gameObject = new GameObject(); gameObject.name = "__WfFadeJob__"; } return gameObject; } private static GameObject FindJobObject(int jobId) { string b = jobId.ToString(); Transform transform = WfFadeJob.GetJobObjectParent().transform; GameObject result = null; for (int i = 0; i < transform.childCount; i++) { if (transform.GetChild(i).gameObject.name == b) { result = transform.GetChild(i).gameObject; break; } } return result; } private static GameObject FindJobObject(params InterfaceWfFade[] objectList) { if (objectList.Length <= 0) { return null; } Transform transform = WfFadeJob.GetJobObjectParent().transform; for (int i = 0; i < transform.childCount; i++) { GameObject gameObject = transform.GetChild(i).gameObject; if (!(gameObject == null) && !string.IsNullOrEmpty(gameObject.name)) { WfFadeJob component = gameObject.GetComponent(); if (!(component == null)) { for (int j = 0; j < objectList.Length; j++) { if (objectList[j] != null) { if ((component.jobData.inObject != null && component.jobData.inObject == objectList[j]) || (component.jobData.outObject != null && component.jobData.outObject == objectList[j])) { return component.gameObject; } } } } } } return null; } private static WfFadeJob GetJobObject() { GameObject jobObjectParent = WfFadeJob.GetJobObjectParent(); GameObject gameObject = null; for (int i = 0; i < jobObjectParent.transform.childCount; i++) { if (string.IsNullOrEmpty(jobObjectParent.transform.GetChild(i).name)) { gameObject = jobObjectParent.transform.GetChild(i).gameObject; break; } } if (gameObject == null) { gameObject = new GameObject(); gameObject.transform.SetParent(jobObjectParent.transform, false); gameObject.AddComponent(); } gameObject.name = gameObject.GetInstanceID().ToString(); return gameObject.GetComponent(); } protected void ExecJob(WfFadeJob.JobData jobData) { this.jobData = jobData; if (jobData.inObject != null) { jobData.inObject.OnStartedFadeIn(); } if (jobData.outObject != null) { jobData.outObject.OnStartedFadeOut(); } if (jobData.time <= 0f) { this.OnUpdateValue(1f); this.OnCompleteValue(); } else { iTween.ValueTo(base.gameObject, iTween.Hash(new object[] { "easetype", jobData.easeType, "from", 0, "to", 1, "time", jobData.time, "delay", 0, "onUpdate", "OnUpdateValue", "onComplete", "OnCompleteValue" })); } } private void OnUpdateValue(float val) { if (this.jobData.inObject != null) { this.jobData.inObject.OnUpdateFadeIn(val); } if (this.jobData.outObject != null) { this.jobData.outObject.OnUpdateFadeOut(1f - val); } } private void OnCompleteValue() { iTween component = base.GetComponent(); if (component != null) { UnityEngine.Object.DestroyImmediate(component); } base.gameObject.name = string.Empty; if (this.jobData.inObject != null) { this.jobData.inObject.OnCompleteFadeIn(); } if (this.jobData.outObject != null) { this.jobData.outObject.OnCompleteFadeOut(); } } private WfFadeJob.JobData jobData; protected struct JobData { public int jobId; public InterfaceWfFade inObject; public InterfaceWfFade outObject; public iTween.EaseType easeType; public float time; } }