如果 VersionControlObject 支持,则为T 扩展对象。否则为null。
获取可选扩展对象。
您可以在 VersionControlObject 中实施可选扩展以改善用户体验。例如,IIconOverlayExtension 使您能够在资产图标上绘制 VCS 状态叠加。有关扩展的完整列表,请参阅文档。Unity 的未来版本可能会添加新的扩展或停止调用现有扩展。
默认的 GetExtension 实现尝试将 VersionControlObject 转换为扩展类型。这使您能够直接在您派生的 VersionControlObject 类中实现扩展。
using UnityEditor.VersionControl; using UnityEngine;
[VersionControl("Custom")] public class CustomVersionControlObject : VersionControlObject, IIconOverlayExtension { static Texture2D s_Icon;
static Texture2D GetIcon() { if (s_Icon == null) { s_Icon = new Texture2D(8, 8); for (var y = 0; y < s_Icon.height; ++y) { for (var x = 0; x < s_Icon.width; ++x) { var border = y == 0 || y == s_Icon.height - 1 || x == 0 || x == s_Icon.width - 1; var color = border ? Color.white : Color.red; s_Icon.SetPixel(x, y, color); } } s_Icon.Apply(); } return s_Icon; }
public void DrawOverlay(string assetPath, IconOverlayType type, Rect rect) { var topLeft = new Rect(rect.x, rect.y, 8, 8); var icon = GetIcon(); GUI.DrawTexture(topLeft, icon); } }
要支持在单独的类中实现的扩展,您必须重写此方法并返回适当的对象。
using UnityEditor.VersionControl; using UnityEngine;
public class CustomIconOverlayExtension : IIconOverlayExtension { public void DrawOverlay(string assetPath, IconOverlayType type, Rect rect) { var topLeft = new Rect(rect.x, rect.y, 8, 8); GUI.DrawTexture(topLeft, Texture2D.whiteTexture); } }
[VersionControl("Custom")] public class CustomVersionControlObject : VersionControlObject { readonly IIconOverlayExtension m_IconOverlayExtension = new CustomIconOverlayExtension();
public override T GetExtension<T>() { return m_IconOverlayExtension as T; } }