﻿using UnityEngine;

	[ExecuteInEditMode]
public class WebViewCleaner : MonoBehaviour
{
	public const string Name = "WebViewCleaner";
	public static void Create()
	{
		GameObject gameObj = GameObject.Find(Name);
		if(gameObj)
		{
			WebViewCleaner cleaner = gameObj.GetComponent<WebViewCleaner>();
			if(cleaner)
				cleaner._noClean = true;
			DestroyImmediate(gameObj);
		}
		gameObj = new GameObject(Name, typeof(WebViewCleaner));
		gameObj.hideFlags = HideFlags.HideAndDontSave;
#if UNITY_EDITOR
		UnityEditor.EditorApplication.playmodeStateChanged -= Create;
		UnityEditor.EditorApplication.playmodeStateChanged += Create;
#endif
	}
	bool _noClean;
	void OnDestroy()
	{
		if(_noClean)//UnityEditor.EditorApplication.isPlaying || UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
			return;
		Debug.Log("WebViewCleaner(" + GetInstanceID() + ").OnDestroy,unload,all");
#if UNITY_EDITOR || UNITY_STANDALONE
		WebViewController.UnloadAllWebs();
#endif
	}
}
