You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.7 KiB

using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;
public class PinchZoomImageEditor : GraphicEditor
{
private static GameObject getCanvas()
{
if (Selection.activeGameObject)
return Selection.activeGameObject;
Canvas[] canvases = FindObjectsOfType<Canvas>();
foreach (Canvas canvas in canvases)
{
if (canvas.gameObject.activeInHierarchy)
return canvas.gameObject;
}
return null;
}
private static GameObject createCanvas()
{
GameObject result = new GameObject("Canvas");
Canvas canvas = result.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
result.AddComponent<CanvasScaler>();
result.AddComponent<GraphicRaycaster>();
return result;
}
[MenuItem("GameObject/UI/Pinch and Zoom Image", false, 0)]
public static void CreateGameObject()
{
GameObject parent = getCanvas();
if(parent == null)
parent = createCanvas();
RectTransform parentCanvasRenderer = parent.GetComponent<RectTransform>();
if (parentCanvasRenderer)
{
GameObject go = new GameObject("Pinch and Zoom Image");
go.transform.SetParent(parent.transform, false);
go.AddComponent<RectTransform>();
go.AddComponent<RawImage>();
go.AddComponent<PinchZoomImage>();
Selection.activeGameObject = go;
}
else
{
EditorUtility.DisplayDialog("Pinch and Zoom Image", "You must make the Pinch and Zoom Image component as a child of a Canvas.", "Ok");
}
}
}