123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- 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<WfFadeJob>().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<GameObject> list = new List<GameObject>();
- 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<WfFadeJob>();
- 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<WfFadeJob>();
- 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<WfFadeJob>();
- }
- gameObject.name = gameObject.GetInstanceID().ToString();
- return gameObject.GetComponent<WfFadeJob>();
- }
- 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<iTween>();
- 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;
- }
- }
|