2018-12-19 21:09:21 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
|
|
|
|
|
using PrefabStageUtility = UnityEditor.Experimental.SceneManagement.PrefabStageUtility;
|
|
|
|
|
#endif
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
|
|
|
|
namespace Coffee.UIExtensions
|
|
|
|
|
{
|
2020-02-12 20:38:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ExecuteInEditMode]
|
|
|
|
|
[AddComponentMenu("")]
|
|
|
|
|
public class UIParticleOverlayCamera : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
//################################
|
|
|
|
|
// Public/Protected Members.
|
|
|
|
|
//################################
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get instance object.
|
|
|
|
|
/// If instance does not exist, Find instance in scene, or create new one.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static UIParticleOverlayCamera instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
|
2020-02-12 20:38:06 +08:00
|
|
|
|
// If current scene is prefab mode, create OverlayCamera for editor.
|
|
|
|
|
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
|
|
|
|
|
if (prefabStage != null && prefabStage.scene.isLoaded)
|
|
|
|
|
{
|
|
|
|
|
if (!s_InstanceForPrefabMode)
|
|
|
|
|
{
|
|
|
|
|
// This GameObject is not saved in prefab.
|
|
|
|
|
// This GameObject is not shown in the hierarchy view.
|
|
|
|
|
// When you exit prefab mode, this GameObject is destroyed automatically.
|
|
|
|
|
var go = new GameObject(typeof(UIParticleOverlayCamera).Name + "_ForEditor")
|
|
|
|
|
{
|
|
|
|
|
hideFlags = HideFlags.HideAndDontSave,
|
|
|
|
|
tag = "EditorOnly",
|
|
|
|
|
};
|
|
|
|
|
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(go, prefabStage.scene);
|
|
|
|
|
s_InstanceForPrefabMode = go.AddComponent<UIParticleOverlayCamera>();
|
|
|
|
|
}
|
|
|
|
|
return s_InstanceForPrefabMode;
|
|
|
|
|
}
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
// Find instance in scene, or create new one.
|
|
|
|
|
if (object.ReferenceEquals(s_Instance, null))
|
|
|
|
|
{
|
|
|
|
|
s_Instance = FindObjectOfType<UIParticleOverlayCamera>() ?? new GameObject(typeof(UIParticleOverlayCamera).Name, typeof(UIParticleOverlayCamera)).GetComponent<UIParticleOverlayCamera>();
|
|
|
|
|
s_Instance.gameObject.SetActive(true);
|
|
|
|
|
s_Instance.enabled = true;
|
|
|
|
|
}
|
|
|
|
|
return s_Instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
public static Camera GetCameraForOvrelay(Canvas canvas)
|
|
|
|
|
{
|
|
|
|
|
var i = instance;
|
|
|
|
|
var rt = canvas.rootCanvas.transform as RectTransform;
|
|
|
|
|
var cam = i.cameraForOvrelay;
|
|
|
|
|
var trans = i.transform;
|
|
|
|
|
cam.enabled = false;
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
var pos = rt.localPosition;
|
|
|
|
|
cam.orthographic = true;
|
|
|
|
|
cam.orthographicSize = Mathf.Max(pos.x, pos.y);
|
|
|
|
|
cam.nearClipPlane = 0.3f;
|
|
|
|
|
cam.farClipPlane = 1000f;
|
|
|
|
|
pos.z -= 100;
|
|
|
|
|
trans.localPosition = pos;
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
return cam;
|
|
|
|
|
}
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
//################################
|
|
|
|
|
// Private Members.
|
|
|
|
|
//################################
|
|
|
|
|
Camera cameraForOvrelay { get { return m_Camera ? m_Camera : (m_Camera = GetComponent<Camera>()) ? m_Camera : (m_Camera = gameObject.AddComponent<Camera>()); } }
|
|
|
|
|
Camera m_Camera;
|
|
|
|
|
static UIParticleOverlayCamera s_Instance;
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
|
2020-02-12 20:38:06 +08:00
|
|
|
|
static UIParticleOverlayCamera s_InstanceForPrefabMode;
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#endif
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Awake is called when the script instance is being loaded.
|
|
|
|
|
/// </summary>
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
|
2020-02-12 20:38:06 +08:00
|
|
|
|
// OverlayCamera for editor.
|
|
|
|
|
if (hideFlags == HideFlags.HideAndDontSave || s_InstanceForPrefabMode == this)
|
|
|
|
|
{
|
|
|
|
|
s_InstanceForPrefabMode = GetComponent<UIParticleOverlayCamera>();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
// Hold the instance.
|
|
|
|
|
if (s_Instance == null)
|
|
|
|
|
{
|
|
|
|
|
s_Instance = GetComponent<UIParticleOverlayCamera>();
|
|
|
|
|
}
|
|
|
|
|
// If the instance is duplicated, destroy itself.
|
|
|
|
|
else if (s_Instance != this)
|
|
|
|
|
{
|
|
|
|
|
UnityEngine.Debug.LogWarning("Multiple " + typeof(UIParticleOverlayCamera).Name + " in scene.", this.gameObject);
|
|
|
|
|
enabled = false;
|
2018-12-19 21:09:21 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
if (!Application.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
DestroyImmediate(gameObject);
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-12-19 21:09:21 +08:00
|
|
|
|
#endif
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
cameraForOvrelay.enabled = false;
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
// Singleton has DontDestroy flag.
|
|
|
|
|
if (Application.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-19 21:09:21 +08:00
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the MonoBehaviour will be destroyed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
void OnDestroy()
|
|
|
|
|
{
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
|
2020-02-12 20:38:06 +08:00
|
|
|
|
if (s_InstanceForPrefabMode == this)
|
|
|
|
|
{
|
|
|
|
|
s_InstanceForPrefabMode = null;
|
|
|
|
|
}
|
2019-01-15 14:49:39 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
// Clear instance on destroy.
|
|
|
|
|
if (s_Instance == this)
|
|
|
|
|
{
|
|
|
|
|
s_Instance = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-19 21:09:21 +08:00
|
|
|
|
}
|