2025-11-08 14:03:17 +08:00
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
namespace SceneView
|
|
|
|
|
{
|
|
|
|
|
public class CanvasControl : MonoBehaviour
|
|
|
|
|
{
|
2025-11-13 16:24:49 +08:00
|
|
|
public const string ViewCanvasName = "_SceneViewCanvas";
|
|
|
|
|
public const string ShowAim = "";
|
|
|
|
|
|
2025-11-08 14:03:17 +08:00
|
|
|
public static Vector2 panelSize = new Vector2(500, 800);
|
|
|
|
|
|
2025-11-13 16:24:49 +08:00
|
|
|
private static GameObject _focusObject;
|
|
|
|
|
public SceneViewPanel? sceneViewPanel;
|
|
|
|
|
public ParametersPanel? parametersPanel;
|
|
|
|
|
|
|
|
|
|
public static GameObject FocusGameObject
|
|
|
|
|
{
|
|
|
|
|
get => _focusObject;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != _focusObject)
|
|
|
|
|
{
|
|
|
|
|
if (_focusObject)
|
|
|
|
|
{
|
|
|
|
|
var outline=_focusObject.GetComponent<Outline>();
|
|
|
|
|
if (outline)
|
|
|
|
|
Destroy(outline);
|
|
|
|
|
}
|
|
|
|
|
_focusObject = value;
|
|
|
|
|
_focusObject.AddComponent<Outline>();
|
|
|
|
|
OnChangeFocusObject?.Invoke(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 14:03:17 +08:00
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
InitCanvas();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
|
{
|
2025-11-13 16:24:49 +08:00
|
|
|
if (Input.GetKeyDown(KeyCode.F3))
|
2025-11-08 14:03:17 +08:00
|
|
|
{
|
2025-11-13 16:24:49 +08:00
|
|
|
// Debug.Log("切换");
|
|
|
|
|
parametersPanel.gameObject.SetActive(!parametersPanel.gameObject.activeSelf);
|
2025-11-08 14:03:17 +08:00
|
|
|
sceneViewPanel.gameObject.SetActive(!sceneViewPanel.gameObject.activeSelf);
|
|
|
|
|
}
|
2025-11-13 16:24:49 +08:00
|
|
|
// if (sceneViewPanel.gameObject.activeSelf)
|
|
|
|
|
// {
|
|
|
|
|
// sceneViewPanel.Refresh();
|
|
|
|
|
// }
|
2025-11-08 14:03:17 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-13 16:24:49 +08:00
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
if (sceneViewPanel) Destroy(sceneViewPanel.gameObject);
|
|
|
|
|
if (parametersPanel) Destroy(parametersPanel.gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static event Action<GameObject> OnChangeFocusObject;
|
|
|
|
|
|
2025-11-08 14:03:17 +08:00
|
|
|
private void InitCanvas()
|
|
|
|
|
{
|
|
|
|
|
var canvasObj = new GameObject(ViewCanvasName);
|
|
|
|
|
var canvas = canvasObj.AddComponent<Canvas>();
|
|
|
|
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
|
|
|
|
canvas.sortingOrder = 1000;
|
|
|
|
|
canvasObj.AddComponent<CanvasScaler>();
|
|
|
|
|
canvasObj.AddComponent<GraphicRaycaster>();
|
|
|
|
|
DontDestroyOnLoad(canvasObj);
|
|
|
|
|
|
2025-11-13 16:24:49 +08:00
|
|
|
sceneViewPanel = CreateSceneViewPanel(canvasObj.transform);
|
|
|
|
|
parametersPanel = CreateParametersPanel(canvasObj.transform);
|
2025-11-08 14:03:17 +08:00
|
|
|
}
|
2025-11-13 16:24:49 +08:00
|
|
|
|
2025-11-08 14:03:17 +08:00
|
|
|
public static SceneViewPanel CreateSceneViewPanel(Transform parent)
|
|
|
|
|
{
|
2025-11-13 16:24:49 +08:00
|
|
|
var panelObj = new GameObject(ViewCanvasName);
|
2025-11-08 14:03:17 +08:00
|
|
|
panelObj.transform.SetParent(parent, false);
|
|
|
|
|
var sceneViewPanel = panelObj.AddComponent<SceneViewPanel>();
|
|
|
|
|
var panelImage = panelObj.AddComponent<Image>();
|
|
|
|
|
panelImage.color = new Color(0.5f, 0.5f, 0.5f, 0.3f);
|
|
|
|
|
|
|
|
|
|
var rectTransform = panelObj.GetComponent<RectTransform>();
|
|
|
|
|
rectTransform.sizeDelta = panelSize;
|
|
|
|
|
|
|
|
|
|
return sceneViewPanel;
|
|
|
|
|
}
|
2025-11-13 16:24:49 +08:00
|
|
|
|
|
|
|
|
public ParametersPanel CreateParametersPanel(Transform parent)
|
|
|
|
|
{
|
|
|
|
|
var panelObj = new GameObject(ViewCanvasName);
|
|
|
|
|
panelObj.transform.SetParent(parent, false);
|
|
|
|
|
var parametersViewPanel = panelObj.AddComponent<ParametersPanel>();
|
|
|
|
|
var panelImage = panelObj.AddComponent<Image>();
|
|
|
|
|
panelImage.color = new Color(0.5f, 0.5f, 0.5f, 0.3f);
|
|
|
|
|
return parametersViewPanel;
|
|
|
|
|
}
|
2025-11-08 14:03:17 +08:00
|
|
|
}
|
|
|
|
|
}
|