Skip to content

Commit bf7ff80

Browse files
committed
New utility methods: CreateSprite, SetLossyScale, GetPropertyValue, SetPropertyValue
1 parent 1686186 commit bf7ff80

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

Runtime/Extensions/UnityEngine/MaterialExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Extensions/UnityEngine/Texture2DExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,16 @@ public static Texture2D LoadFromBase64(this Texture2D texture, string base64Enco
3131
texture.LoadImage(decodedFromBase64);
3232
return texture;
3333
}
34+
35+
/// <summary>
36+
/// Create new sprite instance for the texture.
37+
/// </summary>
38+
/// <param name="texture">A texture to created sprite from.</param>
39+
/// <returns>New sprite instance.</returns>
40+
public static Sprite CreateSprite(this Texture2D texture)
41+
{
42+
return Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f));
43+
}
44+
3445
}
3546
}

Runtime/Extensions/UnityEngine/TransformExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class TransformExtensions
1212
/// </summary>
1313
/// <param name="transform">Transform component.</param>
1414
/// <param name="lossyScale">New lossyScale value.</param>
15-
public static void SetLossyScaleScale(this Transform transform, Vector3 lossyScale)
15+
public static void SetLossyScale(this Transform transform, Vector3 lossyScale)
1616
{
1717
transform.localScale = Vector3.one;
1818
var currentLossyScale = transform.lossyScale;

Runtime/Utilities/ReflectionUtility.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Reflection;
34

45
namespace StansAssets.Foundation
56
{
@@ -33,5 +34,29 @@ public static Type FindType(string typeFullName)
3334
.SelectMany(assembly => assembly.GetTypes())
3435
.FirstOrDefault(type => type.FullName == null || type.FullName.Equals(typeFullName));
3536
}
37+
38+
/// <summary>
39+
/// Get property value from an object by it's name.
40+
/// </summary>
41+
/// <param name="src">Source object.</param>
42+
/// <param name="propName">Property name.</param>
43+
/// <param name="bindingAttr">Property binding Attributes. ` BindingFlags.Instance | BindingFlags.Public` by default.</param>
44+
/// <returns>Property value.</returns>
45+
public static object GetPropertyValue(object src, string propName, BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public)
46+
{
47+
return src.GetType().GetProperty(propName, bindingAttr).GetValue(src, null);
48+
}
49+
50+
/// <summary>
51+
/// Get property value from an object by it's name.
52+
/// </summary>
53+
/// <param name="src">Source object.</param>
54+
/// <param name="propName">Property name.</param>
55+
/// <param name="propValue">New property value.</param>
56+
/// <param name="bindingAttr">Property binding Attributes. ` BindingFlags.Instance | BindingFlags.Public` by default.</param>
57+
public static void SetPropertyValue<T>(object src, string propName, T propValue, BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public)
58+
{
59+
src.GetType().GetProperty(propName, bindingAttr).SetValue(src, propValue);
60+
}
3661
}
3762
}

0 commit comments

Comments
 (0)