From 015829f2e27af8e4c724d7b7883933898e1fa1f2 Mon Sep 17 00:00:00 2001 From: wfowler Date: Tue, 31 Jul 2018 17:55:06 -0600 Subject: [PATCH] UIVertexExtensions: Make extension methods work like they do for reference types. Add vanilla static methods that do the same things, for compilers that don't support extension methods on "ref" parameters. --- .../Source/Extensions/UIVertexExtensions.cs | 70 +++++++++++++------ 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/LibBSP/Source/Extensions/UIVertexExtensions.cs b/LibBSP/Source/Extensions/UIVertexExtensions.cs index c4cb507..ccead1d 100644 --- a/LibBSP/Source/Extensions/UIVertexExtensions.cs +++ b/LibBSP/Source/Extensions/UIVertexExtensions.cs @@ -21,15 +21,14 @@ namespace LibBSP { /// public static class UIVertexExtensions { +#if !UNITY /// /// Scales the position of this by a number. /// /// This . /// Scalar value. - /// The scaled . - public static UIVertex Scale(this UIVertex v1, float scalar) { + public static void Scale(this ref UIVertex v1, float scalar) { v1.position *= scalar; - return v1; } /// @@ -37,16 +36,10 @@ namespace LibBSP { /// /// This . /// The other . - /// The resulting . - public static UIVertex Add(this UIVertex v1, UIVertex v2) { - return new UIVertex { - color = v1.color, - normal = v1.normal, - position = v1.position + v2.position, - tangent = v1.tangent, - uv0 = v1.uv0 + v2.uv0, - uv1 = v1.uv1 + v2.uv1 - }; + public static void Add(this ref UIVertex v1, UIVertex v2) { + v1.position += v2.position; + v1.uv0 += v2.uv0; + v1.uv1 += v2.uv1; } /// @@ -54,16 +47,47 @@ namespace LibBSP { /// /// This . /// The . - /// The resulting . - public static UIVertex Translate(this UIVertex v1, Vector3 v2) { - return new UIVertex { - color = v1.color, - normal = v1.normal, - position = v1.position + v2, - tangent = v1.tangent, - uv0 = v1.uv0, - uv1 = v1.uv1 - }; + public static void Translate(this ref UIVertex v1, Vector3 v2) { + v1.position += v2; + } +#endif + + // Extension methods using "ref" are features of C# 7.2. They will not work in Unity, no matter what. + // Instead use these vanilla static methods in any code intended to be used with Unity. + + /// + /// Scales the position of a by a number and returns the result. + /// + /// The to scale. + /// Scalar value. + /// The resulting of this scaling operation. + public static UIVertex Scale(UIVertex v1, float scalar) { + v1.position *= scalar; + return v1; + } + + /// + /// Adds the position of a to another and returns the result. + /// + /// A to be added to another. + /// A to be added to another. + /// The resulting of this addition. + public static UIVertex Add(UIVertex v1, UIVertex v2) { + v1.position += v2.position; + v1.uv0 += v2.uv0; + v1.uv1 += v2.uv1; + return v1; + } + + /// + /// Adds the position of a Vector3 to a and returns the result. + /// + /// The to translate. + /// The to translate by. + /// The resulting translated by . + public static UIVertex Translate(UIVertex v1, Vector3 v2) { + v1.position += v2; + return v1; } ///