void SendInitializeIfNecessary() { if (editorWindowModel == null) { return; } var window = editorWindowModel.window; if (window != null) { var rootElement = window.rootVisualElement; //we make sure styles have been applied UIElementsEditorUtility.AddDefaultEditorStyleSheets(rootElement); if (rootElement.GetProperty(k_InitializedWindowPropertyName) != null) { return; } if (EditorApplication.isUpdating) { EditorApplication.delayCall += SendInitializeIfNecessary; return; } rootElement.SetProperty(k_InitializedWindowPropertyName, true); Invoke("CreateGUI"); } }
private bool UpdatePreviewTexture(int width, int height) { var vta = target as VisualTreeAsset; bool dirty = false; int currentDirtyCount = EditorUtility.GetDirtyCount(target); if (vta != m_LastTree || !m_LastTree || currentDirtyCount != m_LastDirtyCount || vta.contentHash != m_LastContentHash) { m_LastTree = vta; m_Tree = vta.Instantiate(); m_Tree.StretchToParentSize(); m_LastDirtyCount = currentDirtyCount; m_LastContentHash = vta.contentHash; dirty = true; } if (m_Panel == null) { m_Panel = UIElementsUtility.FindOrCreateEditorPanel(m_LastTree); var visualTree = m_Panel.visualTree; UIElementsEditorUtility.AddDefaultEditorStyleSheets(visualTree); dirty = true; } if (dirty) { m_Panel.visualTree.Clear(); m_Panel.visualTree.Add(m_Tree); RenderStaticPreview(width, height, ref m_preview_texture); } return(dirty); }
void SendInitializeIfNecessary(bool duringOnCreate = false) { if (editorWindowModel == null) { return; } var window = editorWindowModel.window; if (window != null) { var rootElement = window.rootVisualElement; if (EditorApplication.isUpdating || duringOnCreate) { rootElement.schedule.Execute(() => { SendInitializeIfNecessary(false); }); return; } if (rootElement.GetProperty(k_InitializedWindowPropertyName) != null) { return; } //we make sure styles have been applied UIElementsEditorUtility.AddDefaultEditorStyleSheets(rootElement); rootElement.SetProperty(k_InitializedWindowPropertyName, true); Invoke("CreateGUI"); } }
public void Render(VisualTreeAsset vta, Rect r, GUIStyle background) { if (Event.current.type != EventType.Repaint || r.width < 100 && r.height < 100) { return; } bool dirty = false; if (vta != m_LastTree || !m_LastTree) { m_LastTree = vta; m_Tree = (vta as UnityEngine.UIElements.VisualTreeAsset).CloneTree(); m_Tree.StretchToParentSize(); dirty = true; } if (m_Panel == null) { m_Panel = UIElementsUtility.FindOrCreatePanel(m_LastTree, ContextType.Editor); var visualTree = m_Panel.visualTree; visualTree.pseudoStates |= PseudoStates.Root; UIElementsEditorUtility.AddDefaultEditorStyleSheets(visualTree); m_Panel.allowPixelCaching = false; dirty = true; } if (dirty) { m_Panel.visualTree.Clear(); m_Panel.visualTree.Add(m_Tree); } EditorGUI.DrawRect(r, EditorGUIUtility.kViewBackgroundColor); Rect layoutRect = GUIClip.UnclipToWindow(r); m_Panel.visualTree.layout = new Rect(0, 0, layoutRect.width, layoutRect.height); // We will draw relative to a viewport covering the preview area, so draw at 0,0 m_Panel.visualTree.IncrementVersion(VersionChangeType.Repaint); var oldState = SavedGUIState.Create(); int clips = GUIClip.Internal_GetCount(); while (clips > 0) { GUIClip.Pop(); clips--; } // Establish preview area viewport var pixelsPerPoint = GUIUtility.pixelsPerPoint; var viewportRect = new Rect(layoutRect.x * pixelsPerPoint, (GUIClip.visibleRect.height - layoutRect.yMax) * pixelsPerPoint, layoutRect.width * pixelsPerPoint, layoutRect.height * pixelsPerPoint); GL.Viewport(viewportRect); m_Panel.Repaint(Event.current); oldState.ApplyAndForget(); }
private static VisualElement CreateRoot() { var name = "rootVisualContainer"; var root = new VisualElement() { name = VisualElementUtils.GetUniqueName(name), pickingMode = PickingMode.Ignore, // do not eat events so IMGUI gets them viewDataKey = name, renderHints = RenderHints.ClipWithScissors }; root.pseudoStates |= PseudoStates.Root; UIElementsEditorUtility.AddDefaultEditorStyleSheets(root); root.style.overflow = UnityEngine.UIElements.Overflow.Hidden; return(root); }
public void Render(VisualTreeAsset vta, Rect r, GUIStyle background) { if (Event.current.type != EventType.Repaint || r.width < 100 && r.height < 100) { return; } bool dirty = false; if (vta != m_LastTree || !m_LastTree) { m_LastTree = vta; m_Tree = (vta as UnityEngine.UIElements.VisualTreeAsset).Instantiate(); m_Tree.StretchToParentSize(); dirty = true; } if (m_Panel == null) { m_Panel = UIElementsUtility.FindOrCreateEditorPanel(m_LastTree); var visualTree = m_Panel.visualTree; visualTree.pseudoStates |= PseudoStates.Root; UIElementsEditorUtility.AddDefaultEditorStyleSheets(visualTree); dirty = true; } if (dirty) { m_Panel.visualTree.Clear(); m_Panel.visualTree.Add(m_Tree); } EditorGUI.DrawRect(r, EditorGUIUtility.kViewBackgroundColor); m_Panel.visualTree.SetSize(r.size); // We will draw relative to a viewport covering the preview area, so draw at 0,0 m_Panel.visualTree.IncrementVersion(VersionChangeType.Repaint); var oldState = SavedGUIState.Create(); int clips = GUIClip.Internal_GetCount(); while (clips > 0) { GUIClip.Pop(); clips--; } float pixelsPerPoint = EditorGUIUtility.pixelsPerPoint; var desc = new RenderTextureDescriptor((int)(r.width * pixelsPerPoint), (int)(r.height * pixelsPerPoint), RenderTextureFormat.ARGB32, 16); var rt = RenderTexture.GetTemporary(desc); var oldRt = RenderTexture.active; RenderTexture.active = rt; GL.LoadPixelMatrix(0, rt.width, rt.height, 0); m_Panel.Repaint(Event.current); RenderTexture.active = oldRt; oldState.ApplyAndForget(); GUI.DrawTexture(r, rt); RenderTexture.ReleaseTemporary(rt); }
public void Render(VisualTreeAsset vta, Rect r, GUIStyle background) { if (Event.current.type != EventType.Repaint || r.width < 100 && r.height < 100) { return; } bool dirty = false; if (vta != m_LastTree || !m_LastTree) { m_LastTree = vta; m_Tree = (vta as UnityEngine.UIElements.VisualTreeAsset).Instantiate(); m_Tree.StretchToParentSize(); dirty = true; } if (m_Panel == null) { m_Panel = UIElementsUtility.FindOrCreateEditorPanel(m_LastTree); var visualTree = m_Panel.visualTree; visualTree.pseudoStates |= PseudoStates.Root; UIElementsEditorUtility.AddDefaultEditorStyleSheets(visualTree); dirty = true; } if (dirty) { m_Panel.visualTree.Clear(); m_Panel.visualTree.Add(m_Tree); } EditorGUI.DrawRect(r, EditorGUIUtility.kViewBackgroundColor); var dpiScale = EditorGUIUtility.pixelsPerPoint; var viewportRect = GUIClip.UnclipToWindow(r); // Still in points, not pixels var viewportRectPx = new Rect(viewportRect.min * dpiScale, viewportRect.size * dpiScale); m_Panel.visualTree.SetSize(viewportRect.size); // We will draw relative to a viewport covering the preview area, so draw at 0,0 m_Panel.visualTree.IncrementVersion(VersionChangeType.Repaint); var oldState = SavedGUIState.Create(); int clips = GUIClip.Internal_GetCount(); while (clips > 0) { GUIClip.Pop(); clips--; } var oldClearFlags = m_Panel.clearFlags; m_Panel.clearFlags = PanelClearFlags.None; RectInt oldViewport = UnityEngine.UIElements.UIR.Utility.GetActiveViewport(); GL.Viewport(new Rect(new Vector2(viewportRectPx.xMin, Screen.height - viewportRectPx.yMin - viewportRectPx.height), viewportRectPx.size)); m_Panel.Repaint(Event.current); GL.Viewport(new Rect(oldViewport.xMin, oldViewport.yMin, oldViewport.width, oldViewport.height)); oldState.ApplyAndForget(); m_Panel.clearFlags = oldClearFlags; }