private static void DrawTexture(Rect screenRect, Rect uvs, Texture texture, bool texelAdjust) { // sanity check if (uvs.width == 0 || uvs.height == 0 || screenRect.width == 0 || screenRect.height == 0 || (texture == null)) { return; } Rect uvsAdjusted; if (texelAdjust) { // adjust the uvs by half a pixel - sometimes useful when textures are closely packed const float texelAdjustPixels = 0.5f; Vector2 uvHalfPixel = new Vector2(texelAdjustPixels / texture.width, texelAdjustPixels / texture.height); uvsAdjusted = Rect.MinMaxRect(uvs.xMin + uvHalfPixel.x, uvs.yMin + uvHalfPixel.y, uvs.xMax - uvHalfPixel.x, uvs.yMax - uvHalfPixel.y); } else { // do not adjust uvsAdjusted = uvs; } // create Vec2 sizes for the inputs Vector2 screenRectSize = new Vector2(screenRect.width, screenRect.height); Vector2 uvSize = new Vector2(uvsAdjusted.width, uvsAdjusted.height); Vector2 textureSize = new Vector2(texture.width, texture.height); // convert the UV size into pixels Vector2 uvSizeInPixels = Vector2.Scale(textureSize, uvSize); // divide the screenRect size (pixels) by the uv size (pixels) to get a scale ration Vector2 textureScale = new Vector2(screenRectSize.x / uvSizeInPixels.x, screenRectSize.y / uvSizeInPixels.y); // convert the UV offset into pixels Vector2 uvOffsetInPixels = new Vector2(uvsAdjusted.xMin * textureSize.x, uvsAdjusted.yMin * textureSize.y); // multiply the uv offset (pixels) by the texture scale to get the offset of the texture // this result will be positive but we need to move the texture left and up - which is the negative direction so *=-1 Vector2 textureOffset = new Vector2(uvOffsetInPixels.x * textureScale.x, uvOffsetInPixels.y * textureScale.y) * -1; // multiply the texture size by the texture scale to get the actual width and height the texture should be drawn at Vector2 textureRectSize = Vector2.Scale(textureScale, textureSize); // set up the rect used to draw the texture Rect textureRect = GUIx.Vector2Rect(textureOffset, textureRectSize); // set up a GUI clipping rect GUI.BeginGroup(screenRect); // rendering the texture directly always results in scaling - use a guiStyle GUIStyle guiStyle = new GUIStyle(); guiStyle.normal.background = (Texture2D)texture; // render the texture - use GUI.Box instead of GUI.DrawTexture as GUI.DrawTexture does not obey Group clipping GUI.Box(textureRect, (string)null, guiStyle); // end GUI clipping rect GUI.EndGroup(); }
public static string DelayedEditableLabel(Rect rect, string text, GUIStyle labelStyle = null, GUIStyle textStyle = null, int clickCount = GUIx.EditableLabelClickCount) { if (labelStyle == null) { labelStyle = "label"; } if (textStyle == null) { textStyle = "textfield"; } //DelayedTextField: 切换焦点时值错误 return(GUIx.DelayedEditableLabel(rect, text, clickCount: clickCount, labelStyle: labelStyle, textStyle: textStyle, textField: (o) => EditorGUI.TextField(rect, o, textStyle), onStart: () => { //EditorGUIUtility.editingTextField = false; EditorGUIUtility.editingTextField = true; }, onEnd: (b) => EditorGUIUtility.editingTextField = false)); ; }
public static string DelayedPlaceholderField(Rect rect, string text, out string current, GUIContent placeholder, GUIStyle textStyle = null, GUIStyle placeholderStyle = null) { return(GUIx.DelayedPlaceholderField(rect, text, out current, placeholder, textStyle: textStyle, placeholderStyle: placeholderStyle, textField: o => EditorGUI.TextField(rect, o)));; }
private static void DrawScaleTexture(Rect screenRect, Texture texture) { // sanity check if (screenRect.width == 0 || screenRect.height == 0 || texture == null || texture.width == 0 || texture.height == 0) { return; } // calculate sizes used when drawing Vector2 screenRectSize = new Vector2(screenRect.width, screenRect.height); Vector2 halfScreenRectSize = screenRectSize / 2.0f; Vector2 textureSize = new Vector2(1.0f, 1.0f); // use 1.0 instead of actual size as we are passing in UVs Vector2 textureThirdSize = textureSize / 3.0f; Vector2 textureCornerPixels = new Vector2(texture.width / 3.0f, texture.height / 3.0f); Vector2 cornerSize = new Vector2(Mathf.Min(halfScreenRectSize.x, textureCornerPixels.x), Mathf.Min(halfScreenRectSize.y, textureCornerPixels.y)); Vector2 edgeSize = new Vector2(screenRectSize.x - (textureCornerPixels.x * 2.0f), screenRectSize.y - (textureCornerPixels.y * 2.0f)); // use a GUI group to get correct offsetting GUI.BeginGroup(screenRect); // draw the corners // top left GUIx.DrawTexture(GUIx.Vector2Rect(Vector2.zero, cornerSize), GUIx.Vector2Rect(Vector2.zero, textureThirdSize), texture); // top right GUIx.DrawTexture(GUIx.Vector2Rect(new Vector2(screenRectSize.x - cornerSize.x, 0.0f), cornerSize), GUIx.Vector2Rect(new Vector2(textureSize.x - textureThirdSize.x, 0.0f), textureThirdSize), texture); // bottom left GUIx.DrawTexture(GUIx.Vector2Rect(new Vector2(0.0f, screenRectSize.y - cornerSize.y), cornerSize), GUIx.Vector2Rect(new Vector2(0.0f, textureSize.y - textureThirdSize.y), textureThirdSize), texture); // bottom right GUIx.DrawTexture(GUIx.Vector2Rect(screenRectSize - cornerSize, cornerSize), GUIx.Vector2Rect(textureSize - textureThirdSize, textureThirdSize), texture); // will be set to false if one or both of the edges does not draw bool drawCentre = true; // should the horizontal edges be drawn? if (edgeSize.x > 0.0) { // top GUIx.DrawTexture(GUIx.Vector2Rect(new Vector2(cornerSize.x, 0.0f), new Vector2(edgeSize.x, cornerSize.y)), GUIx.Vector2Rect(new Vector2(textureThirdSize.x, 0.0f), textureThirdSize), texture); // bottom GUIx.DrawTexture( GUIx.Vector2Rect(new Vector2(cornerSize.x, screenRectSize.y - cornerSize.y), new Vector2(edgeSize.x, cornerSize.y)), GUIx.Vector2Rect(new Vector2(textureThirdSize.x, textureSize.y - textureThirdSize.y), textureThirdSize), texture); } else { drawCentre = false; } // should the vertical edges be drawn? if (edgeSize.y > 0.0) { // left GUIx.DrawTexture(GUIx.Vector2Rect(new Vector2(0.0f, cornerSize.y), new Vector2(cornerSize.x, edgeSize.y)), GUIx.Vector2Rect(new Vector2(0.0f, textureThirdSize.y), textureThirdSize), texture); // right GUIx.DrawTexture( GUIx.Vector2Rect(new Vector2(screenRectSize.x - cornerSize.x, cornerSize.y), new Vector2(cornerSize.x, edgeSize.y)), GUIx.Vector2Rect(new Vector2(textureSize.x - textureThirdSize.x, textureThirdSize.y), textureThirdSize), texture); } else { drawCentre = false; } // if both edges were drawn, draw the centre texture if (drawCentre) { GUIx.DrawTexture(GUIx.Vector2Rect(cornerSize, edgeSize), GUIx.Vector2Rect(textureThirdSize, textureThirdSize), texture); } // end the GUI group GUI.EndGroup(); }
public static void DrawTexture(Rect screenRect, Vector2 uvMin, Vector2 uvMax, Texture texture, bool texelAdjust) { DrawTexture(screenRect, GUIx.MinMaxRect(uvMin, uvMax), texture, texelAdjust); }