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(); 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.renderMode = RenderMode.ScreenSpaceOverlay; result.AddComponent(); result.AddComponent(); 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(); if (parentCanvasRenderer) { GameObject go = new GameObject("Pinch and Zoom Image"); go.transform.SetParent(parent.transform, false); go.AddComponent(); go.AddComponent(); go.AddComponent(); 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"); } } }