如何将 PolygonCollider2D 中的点复制到 Vector2[] 中?
How to copy points from a PolygonCollider2D into a Vector2[]?
我正在制作一个 ScriptableObject 来存储预制件的设置,这样我就可以生成它,并设置诸如 PolygonCollider2D 的点之类的东西。这是一种创建许多相似对象的方法,而不必将每个对象都创建为预制件或预制件变体。
这些点似乎位于一个名为 Paths 的数组中,然后在其中的每个元素中都有一个名为 Element 0 的数组,其中包含 Vector2。我的对撞机将始终只有一条路径,因此我想复制该列表并将其粘贴到具有 Vector2 数组的脚本化对象资产中:
public Vector2[] points;
我可以通过右键单击点轻松地在 PolygonCollider2D 之间复制,然后选择复制然后粘贴到另一个碰撞器中。如果我将其粘贴到文本编辑器中,我将得到:
GenericPropertyJSON:{"name":"m_Points","type":-1,"children":[{"name":"m_Paths","type":-1,"arraySize":1,"arrayType":"vector","children":[{"name":"Array","type":-1,"arraySize":1,"arrayType":"vector","children":[{"name":"size","type":12,"val":1},{"name":"data","type":-1,"arraySize":8,"arrayType":"Vector2","children":[{"name":"Array","type":-1,"arraySize":8,"arrayType":"Vector2","children":[{"name":"size","type":12,"val":8},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":0.067855835},{"name":"y","type":2,"val":-1.4540863}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":1.38775635},{"name":"y","type":2,"val":-0.6495056}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":1.77092743},{"name":"y","type":2,"val":1.06118774}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":1.6979599},{"name":"y","type":2,"val":1.639801}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":0.271110535},{"name":"y","type":2,"val":1.63804626}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":-1.778122},{"name":"y","type":2,"val":0.559341431}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":-1.65858459},{"name":"y","type":2,"val":-1.3278656}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":-0.930610657},{"name":"y","type":2,"val":-1.62702942}]}]}]}]}]}]}
但我不知道如何在 SO 中创建一个可以将这些数据粘贴到其中的字段。
当我查看 PolygonCollider2D 的源代码时,注释中提到了角点,我不知道注释是否有误,但我也没有在源代码中的任何地方看到 GetPoints_Binding
。
/// <summary>
/// <para>Corner points that define the collider's shape in local space.</para>
/// </summary>
/// <footer><a href="https://docs.unity3d.com/2020.3/Documentation/ScriptReference/30_search.html?q=PolygonCollider2D-points">`PolygonCollider2D.points` on docs.unity3d.com</a></footer>
public extern Vector2[] points { [NativeMethod("GetPoints_Binding"), MethodImpl(MethodImplOptions.InternalCall)] get; [NativeMethod("SetPoints_Binding"), MethodImpl(MethodImplOptions.InternalCall)] set; }
任何想法如何解决如何copy/paste这些?
我通过将其添加到 ScriptableObject 来解决它,因此我可以通过分配碰撞器来复制点。它使用 Odin 作为按钮。
希望它能帮助遇到同样异常问题的人:)
[ShowInInspector]
private PolygonCollider2D colliderToCopyFrom;
[Button]
private void CopyPoints() {
points = new Vector2[colliderToCopyFrom.points.Length];
Array.Copy(colliderToCopyFrom.points, points, colliderToCopyFrom.points.Length);
}
JSON 你得到的是 Unity 提供的一个特殊实用程序,用于通过 Unity 编辑器中的检查器复制值。它特定于此组件类型,是您无法通过 API 直接访问但只能通过检查器访问的私有序列化字段的表示。
最简单的狐狸是有一个同名的字段,例如
public float[] m_points;
但是,由于无论如何您都是按代码进行的,因此根本没有必要做您所做的事情。
Vector2
是一个 struct 所以一个值类型,根本不需要复制它们。
此外(作为 Unity 中的大多数 public 成员)PolygonCollider2D.points
is a property 在内部处理和复制 getter 和 setter 中的点,因此稍后会更改array 不影响它 => 也根本不需要复制 array,反正它已经 returns 只是一个副本。
所以使用
points = colliderToCopyFrom.points;
这就是您需要做的一切。
和分配一样
colliderToCopyFrom.points = someVector2Array;
请记住,尽管这些点位于对撞机的 local space 中。如果你在世界 space 中需要它们,你可以像
那样转换它们
using System.Linq;
...
worldPoints = colliderToCopyFrom.points.Select(p => colliderToCopyFrom.TransformPoint(p)).ToArray();
反之
colliderToCopyFrom.points = worldPoints.Select(p => colliderToCopyFrom.InverseTransformPoint (p)).ToArray();
我正在制作一个 ScriptableObject 来存储预制件的设置,这样我就可以生成它,并设置诸如 PolygonCollider2D 的点之类的东西。这是一种创建许多相似对象的方法,而不必将每个对象都创建为预制件或预制件变体。
这些点似乎位于一个名为 Paths 的数组中,然后在其中的每个元素中都有一个名为 Element 0 的数组,其中包含 Vector2。我的对撞机将始终只有一条路径,因此我想复制该列表并将其粘贴到具有 Vector2 数组的脚本化对象资产中:
public Vector2[] points;
我可以通过右键单击点轻松地在 PolygonCollider2D 之间复制,然后选择复制然后粘贴到另一个碰撞器中。如果我将其粘贴到文本编辑器中,我将得到:
GenericPropertyJSON:{"name":"m_Points","type":-1,"children":[{"name":"m_Paths","type":-1,"arraySize":1,"arrayType":"vector","children":[{"name":"Array","type":-1,"arraySize":1,"arrayType":"vector","children":[{"name":"size","type":12,"val":1},{"name":"data","type":-1,"arraySize":8,"arrayType":"Vector2","children":[{"name":"Array","type":-1,"arraySize":8,"arrayType":"Vector2","children":[{"name":"size","type":12,"val":8},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":0.067855835},{"name":"y","type":2,"val":-1.4540863}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":1.38775635},{"name":"y","type":2,"val":-0.6495056}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":1.77092743},{"name":"y","type":2,"val":1.06118774}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":1.6979599},{"name":"y","type":2,"val":1.639801}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":0.271110535},{"name":"y","type":2,"val":1.63804626}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":-1.778122},{"name":"y","type":2,"val":0.559341431}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":-1.65858459},{"name":"y","type":2,"val":-1.3278656}]},{"name":"data","type":8,"children":[{"name":"x","type":2,"val":-0.930610657},{"name":"y","type":2,"val":-1.62702942}]}]}]}]}]}]}
但我不知道如何在 SO 中创建一个可以将这些数据粘贴到其中的字段。
当我查看 PolygonCollider2D 的源代码时,注释中提到了角点,我不知道注释是否有误,但我也没有在源代码中的任何地方看到 GetPoints_Binding
。
/// <summary>
/// <para>Corner points that define the collider's shape in local space.</para>
/// </summary>
/// <footer><a href="https://docs.unity3d.com/2020.3/Documentation/ScriptReference/30_search.html?q=PolygonCollider2D-points">`PolygonCollider2D.points` on docs.unity3d.com</a></footer>
public extern Vector2[] points { [NativeMethod("GetPoints_Binding"), MethodImpl(MethodImplOptions.InternalCall)] get; [NativeMethod("SetPoints_Binding"), MethodImpl(MethodImplOptions.InternalCall)] set; }
任何想法如何解决如何copy/paste这些?
我通过将其添加到 ScriptableObject 来解决它,因此我可以通过分配碰撞器来复制点。它使用 Odin 作为按钮。
希望它能帮助遇到同样异常问题的人:)
[ShowInInspector]
private PolygonCollider2D colliderToCopyFrom;
[Button]
private void CopyPoints() {
points = new Vector2[colliderToCopyFrom.points.Length];
Array.Copy(colliderToCopyFrom.points, points, colliderToCopyFrom.points.Length);
}
JSON 你得到的是 Unity 提供的一个特殊实用程序,用于通过 Unity 编辑器中的检查器复制值。它特定于此组件类型,是您无法通过 API 直接访问但只能通过检查器访问的私有序列化字段的表示。
最简单的狐狸是有一个同名的字段,例如
public float[] m_points;
但是,由于无论如何您都是按代码进行的,因此根本没有必要做您所做的事情。
Vector2
是一个 struct 所以一个值类型,根本不需要复制它们。
此外(作为 Unity 中的大多数 public 成员)PolygonCollider2D.points
is a property 在内部处理和复制 getter 和 setter 中的点,因此稍后会更改array 不影响它 => 也根本不需要复制 array,反正它已经 returns 只是一个副本。
所以使用
points = colliderToCopyFrom.points;
这就是您需要做的一切。
和分配一样
colliderToCopyFrom.points = someVector2Array;
请记住,尽管这些点位于对撞机的 local space 中。如果你在世界 space 中需要它们,你可以像
那样转换它们using System.Linq;
...
worldPoints = colliderToCopyFrom.points.Select(p => colliderToCopyFrom.TransformPoint(p)).ToArray();
反之
colliderToCopyFrom.points = worldPoints.Select(p => colliderToCopyFrom.InverseTransformPoint (p)).ToArray();