Singularity/Library/PackageCache/com.unity.ugui@1.0.0/Editor/UI/RawImageEditor.cs

111 lines
3.4 KiB
C#
Raw Permalink Normal View History

2024-05-06 14:45:45 -04:00
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;
}
}
}