// Draw Material Preview void CreateMaterialPreview() { var evt = Event.current; var property = serializedObject.FindProperty("Material"); var rowRect = GUILayoutUtility.GetRect(k_MaterialRowHeight, k_MaterialRowHeight); // Need to save this rect during Repaint and use it during Layout for the AreaScope, // otherwise the AreaScope is 1 by 1 px. if (Event.current.type == EventType.Repaint) { m_MaterialPreviewRect = rowRect; } var hoverPreviewBorderStyle = GUIStyle.none; if (rowRect.Contains(evt.mousePosition)) { hoverPreviewBorderStyle = m_PreviewBorderStyle; } // Scroll wheel can cycle through modules. if (rowRect.Contains(evt.mousePosition) && evt.type == EventType.ScrollWheel) { m_TurretBuilder.CycleModules("Material", (int)evt.delta.y); m_TurretBuilder.ResetAllModulePreviewEditors(); m_TurretBuilder.RegenerateTurret(); evt.Use(); } using (var previewScope = new GUILayout.AreaScope(m_MaterialPreviewRect)) { var material = property.objectReferenceValue as Material; var currentMaterialTexture = material?.mainTexture as Texture2D; if (currentMaterialTexture != null) { m_MaterialPreviewStyle.normal.background = currentMaterialTexture; } else if (material != null) { var background = new Texture2D(1, 1); background.SetPixel(0, 0, material.color); background.Apply(); m_MaterialPreviewStyle.normal.background = background; } GUI.Box(new Rect(-30, -330, 1000, 1000), GUIContent.none, m_MaterialPreviewStyle); } using (var previewScope = new GUILayout.AreaScope(m_MaterialPreviewRect)) { // Top hover border. GUILayout.Box( GUIContent.none, hoverPreviewBorderStyle, GUILayout.Height(k_BorderWidth)); using (new GUILayout.HorizontalScope()) { GUILayout.FlexibleSpace(); using (new GUILayout.VerticalScope()) { GUILayout.FlexibleSpace(); EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(property, GUILayout.Width(k_PreviewSize)); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); m_TurretBuilder.ResetAllModulePreviewEditors(); m_TurretBuilder.RegenerateTurret(); } } GUILayout.FlexibleSpace(); } // Bottom hover border. GUILayout.Box( GUIContent.none, hoverPreviewBorderStyle, GUILayout.Height(k_BorderWidth)); } }
// // // #endregion #region CreateInspectorGUI() // // // public override VisualElement CreateInspectorGUI() { // Reset root element and reuse. var root = m_RootElement; root.Clear(); // Turn the UXML into a VisualElement hierarchy under root. m_ModulesVisualTree.CloneTree(root); // Create Module Previews root.Query(classes: new string[] { "module-preview" }) .ForEach((preview) => { preview.Add(CreateModuleUI(preview.name)); }); // Change Detection root.RegisterCallback <ChangeEvent <Object> >(evt => { var element = evt.target as ObjectField; var moduleName = element.bindingPath; if (moduleName == "Material") { UpdateMaterialPreview(); m_TurretBuilder.ResetAllModulePreviewEditors(); } else { m_TurretBuilder.ResetModulePreviewEditor(moduleName); } m_TurretBuilder.RegenerateTurret(); }); // Material Preview { var turret = target as Turret; var material = turret.Material; var texture = material?.mainTexture as Texture2D; var imageElement = root.Q("MaterialImage"); imageElement.style.backgroundImage = null; if (texture != null) { imageElement.style.backgroundImage = texture; } else if (material != null) { imageElement.style.backgroundColor = material.color; } else { imageElement.style.backgroundColor = Color.clear; } } root.Q("MaterialImage").AddManipulator(new TextureDragger()); // Module Cycling via Scroll Wheel root.Query(classes: new string[] { "preview" }).ForEach((preview) => { preview.RegisterCallback <WheelEvent>(evt => { var element = evt.currentTarget as VisualElement; m_TurretBuilder.CycleModules(element.name, (int)evt.delta.y); evt.StopPropagation(); }); }); return(root); }