namespace PivecLabs.Minimap { using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; using UnityEditor; [ExecuteInEditMode] [CustomEditor(typeof(MapManager))] public class MapManagerEditor : Editor { private int headerWidthCorrectionForScaling = 38; public string headerFlexibleStyle = "Box"; private Texture2D header; public Color backgroundColorByDefault; SerializedProperty miniMapshowing; SerializedProperty fullMapshowing; SerializedProperty mapPanel; SerializedProperty rawImage; SerializedProperty mapCanvas; SerializedProperty overlayCanvas; SerializedProperty player; SerializedProperty selectedKey; SerializedProperty rotating; SerializedProperty mapPosition; SerializedProperty cameraSizeM; SerializedProperty cameraSizeF; SerializedProperty cameradistanceM; SerializedProperty cameradistanceF; SerializedProperty overlay; SerializedProperty lockMap; SerializedProperty overlayMap; SerializedProperty zoomMap; SerializedProperty zoomSensitivity; SerializedProperty dragMap; SerializedProperty dragbutton; SerializedProperty dragspeed; SerializedProperty Marker; SerializedProperty Markers; SerializedProperty markerSizeM; SerializedProperty markerSizeF; SerializedProperty centermap; SerializedProperty planedistance; SerializedProperty mapmarkers; SerializedProperty showmarkersM; SerializedProperty showmarkersF; SerializedProperty mapcenter; SerializedProperty updateMarkersM; SerializedProperty updateMarkersF; SerializedProperty cullingMaskmm; SerializedProperty occlusionCullingmm; SerializedProperty cullingMaskfm; SerializedProperty occlusionCullingfm; SerializedProperty compassshowing; SerializedProperty Compasscanvas; SerializedProperty CompassImage; SerializedProperty CompassDirectionText; SerializedProperty markerLayer; void OnEnable() { backgroundColorByDefault = EditorGUIUtility.isProSkin ? new Color(56, 56, 56, 255) : Color.white; header = Resources.Load ("piveclabs") as Texture2D; miniMapshowing = serializedObject.FindProperty("miniMapshowing"); fullMapshowing = serializedObject.FindProperty("fullMapshowing"); mapPanel = serializedObject.FindProperty("mapPanel"); rawImage = serializedObject.FindProperty("rawImage"); mapCanvas = serializedObject.FindProperty("mapCanvas"); overlayCanvas = serializedObject.FindProperty("overlayCanvas"); player = serializedObject.FindProperty("player"); selectedKey = serializedObject.FindProperty("selectedKey"); rotating = serializedObject.FindProperty("rotating"); mapPosition = serializedObject.FindProperty("mapPosition"); cameraSizeM = serializedObject.FindProperty("cameraSizeM"); cameraSizeF = serializedObject.FindProperty("cameraSizeF"); overlay = serializedObject.FindProperty("overlay"); lockMap = serializedObject.FindProperty("lockMap"); overlayMap = serializedObject.FindProperty("overlayMap"); zoomMap = serializedObject.FindProperty("zoomMap"); dragMap = serializedObject.FindProperty("dragMap"); dragbutton = serializedObject.FindProperty("dragbutton"); dragspeed = serializedObject.FindProperty("dragspeed"); Marker = serializedObject.FindProperty("Marker"); Markers = serializedObject.FindProperty("Markers"); markerSizeM = serializedObject.FindProperty("markerSizeM"); markerSizeF = serializedObject.FindProperty("markerSizeF"); cameradistanceM = serializedObject.FindProperty("cameraDistanceM"); cameradistanceF = serializedObject.FindProperty("cameraDistanceF"); planedistance = serializedObject.FindProperty("planedistance"); updateMarkersM = serializedObject.FindProperty("updateMarkersM"); updateMarkersF = serializedObject.FindProperty("updateMarkersF"); showmarkersM = serializedObject.FindProperty("showMarkersM"); showmarkersF = serializedObject.FindProperty("showMarkersF"); centermap = serializedObject.FindProperty("centerMap"); mapcenter = serializedObject.FindProperty("mapCenter"); zoomSensitivity = serializedObject.FindProperty("zoomSensitivity"); cullingMaskmm = serializedObject.FindProperty("cullingMaskmm"); occlusionCullingmm = serializedObject.FindProperty("occlusionCullingmm"); cullingMaskfm = serializedObject.FindProperty("cullingMaskfm"); occlusionCullingfm = serializedObject.FindProperty("occlusionCullingfm"); markerLayer = serializedObject.FindProperty("markerLayer"); CompassDirectionText = serializedObject.FindProperty("CompassDirectionText"); compassshowing = serializedObject.FindProperty("compassshowing"); Compasscanvas = serializedObject.FindProperty("Compasscanvas"); CompassImage = serializedObject.FindProperty("CompassImage"); CompassDirectionText = serializedObject.FindProperty("CompassDirectionText"); } public override void OnInspectorGUI() { DrawEditorByDefaultWithHeaderAndHelpBox(); serializedObject.Update(); EditorGUILayout.Space(); EditorGUILayout.LabelField("MAP MANAGER",EditorStyles.whiteLargeLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(selectedKey,new GUIContent("Toggle MAP Key")); EditorGUILayout.PropertyField(miniMapshowing,new GUIContent("MiniMap Showing")); EditorGUILayout.PropertyField(compassshowing,new GUIContent("Show Compass Bar")); if (compassshowing.boolValue == true) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(Compasscanvas, new GUIContent("Compass Canvas")); EditorGUILayout.PropertyField(CompassImage, new GUIContent("Compass Image")); EditorGUILayout.PropertyField(CompassDirectionText, new GUIContent("Compass Heading")); EditorGUI.indentLevel--; } EditorGUI.indentLevel--; EditorGUILayout.Space(); var rect1 = EditorGUILayout.BeginHorizontal(); Handles.color = Color.gray; Handles.DrawLine(new Vector2(rect1.x - 15, rect1.y), new Vector2(rect1.width + 15, rect1.y)); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.LabelField("MAP CANVAS CONFIGURATION",EditorStyles.whiteLargeLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(mapPanel,new GUIContent("MAP Panel")); EditorGUILayout.PropertyField(rawImage,new GUIContent("Raw Image")); EditorGUILayout.PropertyField(mapCanvas,new GUIContent("MAP Canvas")); EditorGUILayout.PropertyField(player,new GUIContent("Player")); EditorGUI.indentLevel--; EditorGUILayout.Space(); var rect2 = EditorGUILayout.BeginHorizontal(); Handles.color = Color.gray; Handles.DrawLine(new Vector2(rect2.x - 15, rect2.y), new Vector2(rect2.width + 15, rect2.y)); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.LabelField("MINIMAP PROPERTIES",EditorStyles.whiteLargeLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(mapPosition,new GUIContent("MiniMap Position")); EditorGUILayout.PropertyField(cameraSizeM,new GUIContent("Field of View")); EditorGUILayout.PropertyField(this.cullingMaskmm, new GUIContent("Culling Mask")); EditorGUILayout.PropertyField(this.occlusionCullingmm, new GUIContent("Occlusion Culling")); EditorGUILayout.PropertyField(rotating,new GUIContent("Rotate Frame")); EditorGUILayout.PropertyField(updateMarkersM, new GUIContent("Update Map Markers")); if (updateMarkersM.boolValue == true) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(showmarkersM, new GUIContent("Show/Hide Markers")); EditorGUILayout.PropertyField(markerSizeM, new GUIContent("Resize Markers")); EditorGUI.indentLevel--; } EditorGUI.indentLevel--; EditorGUILayout.Space(); var rect3 = EditorGUILayout.BeginHorizontal(); Handles.color = Color.gray; Handles.DrawLine(new Vector2(rect3.x - 15, rect3.y), new Vector2(rect3.width + 15, rect3.y)); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.LabelField("FULL MAP PROPERTIES",EditorStyles.whiteLargeLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(overlay,new GUIContent("Use Overlay Canvas")); if (overlay.boolValue == true) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(overlayCanvas, new GUIContent("Overlay Canvas")); EditorGUILayout.PropertyField(overlayMap,new GUIContent("Show Overlay")); EditorGUILayout.PropertyField(planedistance,new GUIContent("Plane Distance")); EditorGUI.indentLevel--; } EditorGUILayout.Space(); EditorGUILayout.PropertyField(cameraSizeF,new GUIContent("Field of View")); EditorGUILayout.PropertyField(cameradistanceF,new GUIContent("Camera Height")); EditorGUILayout.PropertyField(this.cullingMaskfm, new GUIContent("Culling Mask")); EditorGUILayout.PropertyField(this.occlusionCullingfm, new GUIContent("Occlusion Culling")); EditorGUILayout.Space(); EditorGUILayout.PropertyField(updateMarkersF, new GUIContent("Update Map Markers")); if (updateMarkersF.boolValue == true) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(showmarkersF, new GUIContent("Show/Hide Markers")); EditorGUILayout.PropertyField(markerSizeF, new GUIContent("Resize Markers")); EditorGUI.indentLevel--; } EditorGUILayout.Space(); EditorGUILayout.PropertyField(lockMap,new GUIContent("Freeze Map")); if (lockMap.boolValue == true) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(centermap, new GUIContent("Center Map")); EditorGUILayout.PropertyField(mapcenter, new GUIContent("Object to Center on")); EditorGUI.indentLevel--; } EditorGUILayout.Space(); EditorGUILayout.PropertyField(zoomMap,new GUIContent("Zoom on Scrollwheel")); if (zoomMap.boolValue == true) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(this.zoomSensitivity, new GUIContent("Scrollwheel Sensitivity")); EditorGUI.indentLevel--; } EditorGUILayout.Space(); EditorGUILayout.PropertyField(dragMap,new GUIContent("Drag Map with Mouse")); if (dragMap.boolValue == true) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(dragspeed, new GUIContent("Drag Speed")); EditorGUILayout.PropertyField(dragbutton, new GUIContent("Drag Button")); Rect position = EditorGUILayout.GetControlRect(false, 2 * EditorGUIUtility.singleLineHeight); position.height *= 0.5f; position.y += position.height - 10; position.x += EditorGUIUtility.labelWidth -30; position.width -= EditorGUIUtility.labelWidth + 26; GUIStyle style = GUI.skin.label; style.fontSize = 10; style.alignment = TextAnchor.UpperLeft; EditorGUI.LabelField(position, "Left", style); style.alignment = TextAnchor.UpperCenter; EditorGUI.LabelField(position, "Right", style); style.alignment = TextAnchor.UpperRight; EditorGUI.LabelField(position, "Middle", style); EditorGUI.indentLevel--; } EditorGUILayout.Space(); EditorGUI.indentLevel--; var rect4 = EditorGUILayout.BeginHorizontal(); Handles.color = Color.gray; Handles.DrawLine(new Vector2(rect4.x - 15, rect4.y), new Vector2(rect4.width + 15, rect4.y)); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.LabelField("MAP MARKERS",EditorStyles.whiteLargeLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(markerLayer, new GUIContent("Culling Layer")); SerializedProperty arraySizeProp = Markers.FindPropertyRelative("Array.size"); ArrayGUI(Markers, "Marker ", true); AddAlwaysIncludedShader("Unlit/Transparent Cutout") ; EditorGUI.indentLevel--; EditorGUILayout.Space(); serializedObject.ApplyModifiedProperties(); } private void ArrayGUI(SerializedProperty property, string itemType, bool visible) { EditorGUI.indentLevel++; visible = EditorGUILayout.Foldout(visible, new GUIContent("Markers")); if (visible) { EditorGUI.indentLevel++; SerializedProperty arraySizeProp = property.FindPropertyRelative("Array.size"); EditorGUILayout.PropertyField(arraySizeProp,new GUIContent("Marker Types")); for (int i = 0; i < arraySizeProp.intValue; i++) { EditorGUILayout.PropertyField(property.GetArrayElementAtIndex(i), new GUIContent(itemType + (i +1).ToString()), true); } EditorGUI.indentLevel--; } EditorGUI.indentLevel--; } #if UNITY_EDITOR public static void AddAlwaysIncludedShader(string shaderName) { var shader = Shader.Find(shaderName); if (shader == null) return; SerializedObject graphicsSettings = new SerializedObject(AssetDatabase.LoadAssetAtPath("ProjectSettings/GraphicsSettings.asset")); var arrayProp = graphicsSettings.FindProperty("m_AlwaysIncludedShaders"); bool hasShader = false; for (int i = 0; i < arrayProp.arraySize; ++i) { var arrayElem = arrayProp.GetArrayElementAtIndex(i); if (shader == arrayElem.objectReferenceValue) { hasShader = true; break; } } if (!hasShader) { int arrayIndex = arrayProp.arraySize; arrayProp.InsertArrayElementAtIndex(arrayIndex); var arrayElem = arrayProp.GetArrayElementAtIndex(arrayIndex); arrayElem.objectReferenceValue = shader; graphicsSettings.ApplyModifiedProperties(); AssetDatabase.SaveAssets(); Debug.Log("Shader Added"); } } public void DrawEditorByDefaultWithHeaderAndHelpBox() { DrawHeaderFlexible(header, Color.black); GUI.backgroundColor = backgroundColorByDefault; DrawHelpBox(); } public void DrawHeaderFlexible(Texture2D header, Color backgroundColor) { if (header) { GUI.backgroundColor = backgroundColor; if (header.width + headerWidthCorrectionForScaling < EditorGUIUtility.currentViewWidth) { EditorGUILayout.BeginVertical(headerFlexibleStyle); DrawHeader(header); EditorGUILayout.EndVertical(); } else { DrawHeaderIfScrollbar(header); } } } public void DrawHeaderIfScrollbar(Texture2D header) { EditorGUI.DrawTextureTransparent( GUILayoutUtility.GetRect( EditorGUIUtility.currentViewWidth - headerWidthCorrectionForScaling, header.height), header, ScaleMode.ScaleToFit); } public void DrawHeader(Texture2D header) { EditorGUI.DrawTextureTransparent( GUILayoutUtility.GetRect( header.width, header.height), header, ScaleMode.ScaleToFit); } public void DrawHelpBox() { GUI.backgroundColor = backgroundColorByDefault; LinkButton ("https://docs.piveclabs.com"); } private void LinkButton(string url) { var style = GUI.skin.GetStyle("HelpBox"); style.richText = true; style.alignment = TextAnchor.MiddleCenter; bool bClicked = GUILayout.Button("Online Documentation can be found at https://docs.piveclabs.com", style); var rect = GUILayoutUtility.GetLastRect(); EditorGUIUtility.AddCursorRect(rect, MouseCursor.Link); if (bClicked) Application.OpenURL(url); } #endif } }