public static void DrawSpriteElement(Sprite sprite) { if (sprite == null) { return; } Vector2 fixeSize = new Vector2(64, 64); Rect rect = GUILayoutUtility.GetRect(200, 64, GUI.skin.box); Rect rectImage = new Rect(rect.x, rect.y, fixeSize.x, fixeSize.y); Rect rectLB = new Rect(rect.x + fixeSize.x, rect.y, 150, fixeSize.y); Vector4 outerUV = ((sprite == null) ? Vector4.zero : DataUtility.GetOuterUV(sprite)); //转化一层,outerUV为(左下角点 + 右上角点) rectUV 为 (位置起始点 + 尺寸大小) Rect rectUV = new Rect(outerUV.x, outerUV.y, outerUV.z - outerUV.x, outerUV.w - outerUV.y); float aspect = sprite.rect.width / sprite.rect.height; if (aspect > 0) { rectImage.height = rectImage.width / aspect; } else { rectImage.height = rectImage.width * aspect; } if (rectImage.height > fixeSize.y) { rectImage.width = fixeSize.x * fixeSize.y / rectImage.height; rectImage.height = fixeSize.y; } GUI.DrawTextureWithTexCoords(rectImage, sprite.texture, rectUV); if (sprite.border.Equals(default(Vector4))) { GUI.Label(rectLB, string.Format(" {0}\n {1} x {2}", sprite.name, sprite.rect.width, sprite.rect.height)); } else { GUI.Label(rectLB, string.Format(" {0}\n {1} x {2}\n [九宫格]", sprite.name, sprite.rect.width, sprite.rect.height)); } EditorTools.ClickAndPingObject(rect, sprite); }
public static void DrawSpriteElement(Texture2D texture) { if (texture == null) { return; } Vector2 fixeSize = new Vector2(100, 100); Rect rect = GUILayoutUtility.GetRect(200, fixeSize.y, GUI.skin.box); Rect rectImage = new Rect(rect.x, rect.y, fixeSize.x, fixeSize.y); Rect rectLB = new Rect(rect.x + fixeSize.x + 20, rect.y, 150, fixeSize.y); float aspect = (float)texture.width / (float)texture.height; if (aspect > 0) { rectImage.height = rectImage.width / aspect; } else { rectImage.height = rectImage.width * aspect; } GUI.DrawTexture(rectImage, texture); GUI.Label(rectLB, string.Format("{0} x {1}\n{2}\n{3}\n{4}", texture.width, texture.height, texture.format.ToString(), texture.wrapMode.ToString(), texture.filterMode.ToString() )); EditorTools.ClickAndPingObject(rect, texture); }
protected virtual void DrawFields() { UIGrid grid = target as UIGrid; int constraint = EditorGUILayout.IntField("Constraint", grid.constraint); Vector2 padding = EditorGUILayout.Vector2Field("Padding", grid.padding); Vector2 space = EditorGUILayout.Vector2Field("Space", grid.space); Vector2 cellSize = EditorGUILayout.Vector2Field("CellSize", grid.cellSize); LayoutCorner alignement = (LayoutCorner)EditorGUILayout.EnumPopup("Alignement", grid.alignement); SizeFitterType fitterType = (SizeFitterType)EditorGUILayout.EnumPopup("SizeFitter", grid.fitterType); bool fixedCellSize = EditorGUILayout.Toggle("FixedCellSize", grid.fixedCellSize); GUILayout.BeginHorizontal(); string prefabUIName = EditorGUILayout.TextField("Prefab UI", grid.prefabUI, GUILayout.Height(20)); if (m_PrefabUI == null || m_PrefabUI.name != prefabUIName) { m_PrefabUI = GetTargetUIPrefab(prefabUIName); } Rect rectbox = GUILayoutUtility.GetRect(16, 16, GUI.skin.box); if (m_PrefabUI != null) { GUI.Box(rectbox, EditorGUIUtility.IconContent("lightMeter/greenLight")); EditorTools.ClickAndPingObject(rectbox, m_PrefabUI); GUI.backgroundColor = Color.blue; if (GUILayout.Button("G", GUILayout.Width(18), GUILayout.Height(18))) { var temp = AssetDatabase.LoadAssetAtPath <GameObject>(string.Format("Assets/AssetBases/PrefabAssets/ui/{0}.prefab", prefabUIName)); if (temp != null) { var item = Object.Instantiate(temp); item.name = temp.name; item.transform.parent = grid.transform; item.transform.localScale = Vector3.one; } } GUI.backgroundColor = Color.red; if (GUILayout.Button("C", GUILayout.Width(18), GUILayout.Height(18))) { EditorTools.DestroyChild(grid.gameObject); } GUI.backgroundColor = Color.white; } GUILayout.EndHorizontal(); if (GUI.changed) { EditorTools.RegisterUndo("UIGrid", grid); grid.prefabUI = prefabUIName; grid.constraint = constraint; grid.padding = padding; grid.space = space; grid.cellSize = cellSize; grid.alignement = alignement; grid.fixedCellSize = fixedCellSize; grid.fitterType = fitterType; EditorTools.SetDirty(grid); } }