File size: 3,488 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | using UnityEngine;
using UnityEngine.UI;
namespace UnityEditor.UI
{
[CustomEditor(typeof(RawImage), true)]
[CanEditMultipleObjects]
/// <summary>
/// Custom editor for RawImage.
/// Extend this class to write a custom editor for a component derived from RawImage.
/// </summary>
public class RawImageEditor : GraphicEditor
{
SerializedProperty m_Texture;
SerializedProperty m_UVRect;
GUIContent m_UVRectContent;
protected override void OnEnable()
{
base.OnEnable();
// Note we have precedence for calling rectangle for just rect, even in the Inspector.
// For example in the Camera component's Viewport Rect.
// Hence sticking with Rect here to be consistent with corresponding property in the API.
m_UVRectContent = EditorGUIUtility.TrTextContent("UV Rect");
m_Texture = serializedObject.FindProperty("m_Texture");
m_UVRect = serializedObject.FindProperty("m_UVRect");
SetShowNativeSize(true);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(m_Texture);
AppearanceControlsGUI();
RaycastControlsGUI();
MaskableControlsGUI();
EditorGUILayout.PropertyField(m_UVRect, m_UVRectContent);
SetShowNativeSize(false);
NativeSizeButtonGUI();
serializedObject.ApplyModifiedProperties();
}
void SetShowNativeSize(bool instant)
{
base.SetShowNativeSize(m_Texture.objectReferenceValue != null, instant);
}
private static Rect Outer(RawImage rawImage)
{
Rect outer = rawImage.uvRect;
outer.xMin *= rawImage.rectTransform.rect.width;
outer.xMax *= rawImage.rectTransform.rect.width;
outer.yMin *= rawImage.rectTransform.rect.height;
outer.yMax *= rawImage.rectTransform.rect.height;
return outer;
}
/// <summary>
/// Allow the texture to be previewed.
/// </summary>
public override bool HasPreviewGUI()
{
RawImage rawImage = target as RawImage;
if (rawImage == null)
return false;
var outer = Outer(rawImage);
return outer.width > 0 && outer.height > 0;
}
/// <summary>
/// Draw the Image preview.
/// </summary>
public override void OnPreviewGUI(Rect rect, GUIStyle background)
{
RawImage rawImage = target as RawImage;
Texture tex = rawImage.mainTexture;
if (tex == null)
return;
var outer = Outer(rawImage);
SpriteDrawUtility.DrawSprite(tex, rect, outer, rawImage.uvRect, rawImage.canvasRenderer.GetColor());
}
/// <summary>
/// Info String drawn at the bottom of the Preview
/// </summary>
public override string GetInfoString()
{
RawImage rawImage = target as RawImage;
// Image size Text
string text = string.Format("RawImage Size: {0}x{1}",
Mathf.RoundToInt(Mathf.Abs(rawImage.rectTransform.rect.width)),
Mathf.RoundToInt(Mathf.Abs(rawImage.rectTransform.rect.height)));
return text;
}
}
}
|