Skip to content

Instantly share code, notes, and snippets.

@capnslipp
Last active December 5, 2019 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capnslipp/87d944e474ca701eca8b to your computer and use it in GitHub Desktop.
Save capnslipp/87d944e474ca701eca8b to your computer and use it in GitHub Desktop.
RectOffsetPOD for Unity3D (since UnityEngine.RectOffset likes to throw “ArgumentException: set_left can only be called from the main thread.” if used as a field initialier)
/// @creator: Slipp Douglas Thompson
/// @license: Public Domain per The Unlicense. See <http://unlicense.org/>.
/// @why: Because UnityEngine.RectOffset fields should be able to be initialized without throwing ArgumentExceptions (last observed 4.5.2f1).
/// Also, UnityEngine.RectOffset should probably be a struct, not a class.  So a struct form like this is necessary for some software patterns.
/// @intended project path: Assets/Plugins/UnityEngine Extensions/RectOffsetPOD.cs
/// @intersource: https://gist.github.com/capnslipp/87d944e474ca701eca8b
using System;
using UnityEngine;
namespace UnityEngine
{
[Serializable]
public struct RectOffsetPOD
{
// Plain-Old-Data Fields
public int left;
public int right;
public int top;
public int bottom;
// Conversion Methods
public static implicit operator RectOffset(RectOffsetPOD pod)
{
return new RectOffset(
left: pod.left,
right: pod.right,
top: pod.top,
bottom: pod.bottom
);
}
}
}
public static class RectOffsetPODExtension
{
public static RectOffsetPOD ToPOD(this RectOffset @this)
{
return new RectOffsetPOD {
left = @this.left,
right = @this.right,
top = @this.top,
bottom = @this.bottom
};
}
public static void CopyFromPOD(this RectOffset @this, RectOffsetPOD rectOffsetPOD)
{
@this.left = rectOffsetPOD.left;
@this.right = rectOffsetPOD.right;
@this.top = rectOffsetPOD.top;
@this.bottom = rectOffsetPOD.bottom;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment