C# 如何根据变换对象比较子对象?
我通过这个函数生成了很多对象:C# 如何根据变换对象比较子对象?,c#,unity3d,C#,Unity3d,我通过这个函数生成了很多对象: private GameObject CreateGhost(Transform p, float r, float g, float b) { GameObject result = Instantiate( gameObject, Vector3.zero, Quaternion.identity ); Transform ghost_t = result.transform; RectTransform gho
private GameObject CreateGhost(Transform p, float r, float g, float b)
{
GameObject result = Instantiate(
gameObject, Vector3.zero, Quaternion.identity
);
Transform ghost_t = result.transform;
RectTransform ghost_rt = result.GetComponent<RectTransform>();
ghost_t.SetParent(p, false);
ghost_rt.anchorMin = new Vector2(0f, 0f);
ghost_rt.anchorMax = new Vector2(0f, 0f);
ghost_rt.pivot = new Vector2(0f, 0f);
ghost_rt.localScale = new Vector3(1f, 1f, 1f);
Image m = result.GetComponent<Image>();
m.color = new Color(r, g, b, 0.7f);
return result;
}
这个比较不起作用:!GameObject.ReferenceEqualschild\u t,ghost\u rt
我也试过了,但没有成功:!GameObject.ReferenceEqualschild\u t.parent、ghost\u rt.parent
有什么方法可以知道我是否可以与其他子对象进行比较=与当前对象不同?您可以使用该方法来知道1个矩形是否与其他矩形重叠:
child_t.Overlapsghost_rt.rect您是否尝试过比较child_t.gameObject和ghost_t.gameObject?ReferenceEquals适用于C对象,而不是Unity3d游戏对象。您可以进行比较。你需要知道两个矩形的位置是否相同,对吗?@Dunno是的,相同:它会发现它们都是相等的。rect@joreldraw实际上我就是这么做的:不可能把同一个矩形放在同一个位置,所以如果我试着比较一个相同的矩形,那么我认为它是相同的。但这不是很好的编程。我正在努力使事情比我被迫在网络上做的更好,我希望Unity不会像我们在网络上做的那样成为糟糕的DIY。
RectTransform ghost_rt = Ghost.GetComponent<RectTransform>();
Rect ghost_r = new Rect(ghost_rt.anchoredPosition,
new Vector2(ghost_rt.rect.width, ghost_rt.rect.height));
bool overlaps = false;
foreach (Transform child_t in Dst.transform) {
if (!GameObject.ReferenceEquals(child_t, ghost_rt)) {
RectTransform rt = child_t.GetComponent<RectTransform>();
Rect cmp_r = new Rect(rt.anchoredPosition,
new Vector2(rt.rect.width, rt.rect.height));
DebugText.text += "cmp_r:" + cmp_r + "\n";
if (cmp_r.Overlaps(ghost_r)) {
overlaps = true;
break;
}
}
}