public virtual void DrawBoundingBox(GameObject obj, Graphics gfx) { if (IsVoxel) { gfx.DrawRectangle(BoundsRectPenVoxel, obj.GetBounds()); } else { gfx.DrawRectangle(BoundsRectPenSHP, obj.GetBounds()); } var top = obj.TopTile; var left = obj.Tile.Layer.GetTileR(obj.TopTile.Rx, obj.TopTile.Ry + obj.Drawable.Foundation.Height); var bottom = obj.Tile.Layer.GetTileR(obj.TopTile.Rx + obj.Drawable.Foundation.Width, obj.TopTile.Ry + obj.Drawable.Foundation.Height); var right = obj.Tile.Layer.GetTileR(obj.TopTile.Rx + obj.Drawable.Foundation.Width, obj.TopTile.Ry); List <Point> verts = new List <Point>(); verts.Add(new Point(top.Dx * TileWidth / 2, top.Dy * TileHeight / 2)); verts.Add(new Point(left.Dx * TileWidth / 2 - TileWidth / 4, left.Dy * TileHeight / 2 + TileHeight / 4)); verts.Add(new Point(bottom.Dx * TileWidth / 2, bottom.Dy * TileHeight / 2 + TileHeight / 2)); verts.Add(new Point(right.Dx * TileWidth / 2 + TileHeight / 2, right.Dy * TileHeight / 2 + TileHeight / 4)); verts.Add(new Point(top.Dx * TileWidth / 2, top.Dy * TileHeight / 2)); List <Point> verts2 = new List <Point>(); foreach (var p in verts) { p.Offset(30, 0); verts2.Add(p); } gfx.DrawLines(BoundsRectPenISO, verts2.ToArray()); }
public virtual void DrawBoundingBox(GameObject obj, Graphics gfx) { if (IsVoxel) { gfx.DrawRectangle(BoundsRectPenVoxel, obj.GetBounds()); } else { gfx.DrawRectangle(BoundsRectPenSHP, obj.GetBounds()); } var top = obj.TopTile; var left = obj.Tile.Layer.GetTileR(obj.TopTile.Rx, obj.TopTile.Ry + obj.Drawable.Foundation.Height); var bottom = obj.Tile.Layer.GetTileR(obj.TopTile.Rx + obj.Drawable.Foundation.Width, obj.TopTile.Ry + obj.Drawable.Foundation.Height); var right = obj.Tile.Layer.GetTileR(obj.TopTile.Rx + obj.Drawable.Foundation.Width, obj.TopTile.Ry); List <Point> verts = new List <Point>(); // Starkku: Failsafe because these don't always seem to get initialized properly with buildings places near edges of the map for some reason. if (top != null) { verts.Add(new Point(top.Dx * TileWidth / 2, top.Dy * TileHeight / 2)); } if (left != null) { verts.Add(new Point(left.Dx * TileWidth / 2 - TileWidth / 4, left.Dy * TileHeight / 2 + TileHeight / 4)); } if (bottom != null) { verts.Add(new Point(bottom.Dx * TileWidth / 2, bottom.Dy * TileHeight / 2 + TileHeight / 2)); } if (right != null) { verts.Add(new Point(right.Dx * TileWidth / 2 + TileHeight / 2, right.Dy * TileHeight / 2 + TileHeight / 4)); } if (top != null) { verts.Add(new Point(top.Dx * TileWidth / 2, top.Dy * TileHeight / 2)); } List <Point> verts2 = new List <Point>(); foreach (var p in verts) { p.Offset(30, -15); verts2.Add(p); } gfx.DrawLines(BoundsRectPenISO, verts2.ToArray()); }
public static void RebuildPrefabPanel(PrefabPanel panel) { panel.transform.ClearImmediate(); Vector3 position = Vector3.zero; int i = 0; foreach (GameObject prefab in panel.prefabs) { GameObject go = (GameObject)PrefabUtility.InstantiatePrefab(prefab); GameObject item = new GameObject("Item{0} ({1})".format(i, go.name)); if (panel.isUIPrefabs) { Canvas canvas = item.AddComponent <Canvas>(); CanvasScaler canvasScaler = item.AddComponent <CanvasScaler>(); GraphicRaycaster graphicRaycaster = item.AddComponent <GraphicRaycaster>(); canvas.renderMode = RenderMode.WorldSpace; RectTransform rectTransform = canvas.GetComponent <RectTransform>(); rectTransform.sizeDelta = panel.uiCanvasSize; } else { if (go.GetComponentInChildren <Renderer>() == null) { item.AddComponent <MeshRenderer>(); TextMesh text = item.AddComponent <TextMesh>(); text.characterSize = 0.1f; text.fontSize = 30; text.anchor = TextAnchor.MiddleCenter; text.alignment = TextAlignment.Center; text.text = go.name; } } go.transform.SetParent(item.transform, false); panel.Add(item); Bounds itemb = item.GetBounds(); if (panel.isUIPrefabs) { itemb = new Bounds(Vector3.zero, panel.uiCanvasSize.xyz(0)); } float shift = itemb.IsEmpty() ? 0.0f : (Vector3.right.Mul(itemb.size).magnitude / 2.0f); if (i > 0) { position += Vector3.right * shift; position += Vector3.right; } item.transform.localPosition = position; position += Vector3.right * shift; i++; } }
public void TestGetBounds() { GameObject a = new GameObject("a"); Bounds bounds = a.GetBounds(); Assert.AreEqual(0, a.transform.position.x); Assert.AreEqual(0, a.transform.position.y); Assert.AreEqual(0, bounds.size.x); Assert.AreEqual(0, bounds.size.y); GameObject b = new GameObject("b"); b.transform.parent = a.transform; Vector2 size = AddSpriteRenderer(b); b.transform.position = new Vector3(2 * size.x, 7 * size.y, 0); bounds = a.GetBounds(); Assert.AreEqual(2 * size.x, b.transform.position.x, 0.01f); Assert.AreEqual(7 * size.y, b.transform.position.y, 0.01f); Assert.AreEqual(size.x, bounds.size.x); Assert.AreEqual(size.y, bounds.size.y); GameObject c = new GameObject("c"); c.transform.parent = a.transform; c.transform.position = new Vector3(1 * size.x, 3 * size.y, 0); AddSpriteRenderer(c); bounds = a.GetBounds(); Assert.AreEqual(2 * size.x, bounds.size.x, 0.01f); Assert.AreEqual(5 * size.y, bounds.size.y, 0.01f); // Now let's add a sprite to the parent object AddSpriteRenderer(a); bounds = a.GetBounds(); Assert.AreEqual(0, a.transform.position.x); Assert.AreEqual(0, a.transform.position.y); Assert.AreEqual(2 * size.x, b.transform.position.x, 0.01f); // ensure position assumptions still correct Assert.AreEqual(7 * size.y, b.transform.position.y, 0.01f); Assert.AreEqual(3 * size.x, bounds.size.x, 0.01f); Assert.AreEqual(8 * size.y, bounds.size.y, 0.01f); }
public void SetParent(GameObject parent) { var targetHealth = parent.GetComponent <Health>(); targetHealth.Should().NotBeNull("EnemyHealthBar passed a gameobject without a Health component"); targetHealth.Subscribe(UpdateHealthBar); var parentObjectHeight = parent.GetBounds().max.y; var myHeight = gameObject.GetComponent <Canvas>().GetComponent <RectTransform>().rect.height; gameObject.transform.SetParent(parent.transform); gameObject.transform.localPosition = new Vector3(0, parentObjectHeight + myHeight + 1, 0); }
public static void DoGrowthEffect(GameObject go, float time) { Shader stencilMask = Shader.Find("Custom/StencilMask"); Shader stencilStandard = Shader.Find("Custom/Stencil_Standard"); if (!stencilMask || !stencilStandard) { Debug.LogError("没有找到模板测试shader!"); return; } MeshRenderer[] renderers = go.GetComponentsInChildren <MeshRenderer>(); renderers.ForEach((x) => { x.materials.ForEach((y) => { if (!y.shader.name.Contains("Standard")) { Debug.LogWarning(y.name + "不是standard材质,可能产生无法预期的效果上"); } y.shader = stencilStandard; }); }); Bounds bounds = go.GetBounds(); GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); Object.Destroy(cube.GetComponent <Collider>()); cube.GetComponent <MeshRenderer>().material.shader = stencilMask; cube.transform.position = new Vector3(bounds.center.x, bounds.center.y - bounds.size.y, bounds.center.z); cube.transform.localScale = bounds.size; UnityAction ua = () => { cube.transform.Translate(Vector3.up * Time.deltaTime / time); if (cube.transform.position.y > bounds.center.y) { Shader standard = Shader.Find("Standard"); cube.transform.SetPosY(bounds.center.y); renderers.ForEach((x) => { x.materials.ForEach((y) => { y.shader = standard; }); }); } }; MonoBehaviorTool.Instance.RegisterUpdate(ua); }
private void MoveToPosition() { if (Target == null) { return; } var parentObjectHeight = Target.GetBounds().max.y; var myBounds = this.GetBounds(); var targetPosition = Target.transform.position; gameObject.transform.position = new Vector3(targetPosition.x, parentObjectHeight + myBounds.extents.y, targetPosition.z); }
/** * Position the camera according to the size of the grid object */ public void PositionCamera(GameObject objectToView) { Bounds targetBounds = objectToView.GetBounds(); float screenRatio = (float)Screen.width / (float)Screen.height; float targetRatio = targetBounds.size.x / targetBounds.size.y; if (screenRatio >= targetRatio) { Camera.orthographicSize = targetBounds.size.y / 2 + yOffset; } else { float differenceInSize = targetRatio / screenRatio; Camera.orthographicSize = targetBounds.size.y / 2 * differenceInSize + xOffset; } transform.position = new Vector3(targetBounds.center.x, targetBounds.center.y + 0.5f, -1f); }
Sprite RenderObjectIcon(GameObject go, string iconName) { Bounds bounds = go.GetBounds(); MainCamera.Instance.transform.position = bounds.center + Vector3.back * camDistance; MainCamera.Instance.camera.orthographicSize = Mathf.Max(bounds.extents.y, bounds.extents.x); MainCamera.Instance.camera.Render(); Sprite sprite = SaveIcon(iconName); if (selectOnRender) { Selection.activeObject = sprite; } return(sprite); }
private bool PixelCollisionHFlip(GameObject other, Color[] dataA, Color[] dataB) { int top = Math.Max(bounds.Top, other.GetBounds().Top); int bottom = Math.Min(bounds.Bottom, other.GetBounds().Bottom); int left = Math.Max(bounds.Left, other.GetBounds().Left); int right = Math.Min(bounds.Right, other.GetBounds().Right); for (int y = top; y < bottom; y++) { for (int x = left; x < right; x++) { Color colorA = dataA[(int)(bounds.Width / sprite.GetRealScale().X) - 1 - (int)((x - bounds.Left) / sprite.GetRealScale().X) + (int)((int)((y - bounds.Top) / sprite.GetRealScale().Y) * (bounds.Width / sprite.GetRealScale().X))]; Color colorB = dataB[(int)((x - other.GetBounds().Left) / other.sprite.GetRealScale().X) + (int)((int)((y - other.GetBounds().Top) / other.sprite.GetRealScale().Y) * (other.GetBounds().Width / other.sprite.GetRealScale().X))]; if (colorA.A != 0 && colorB.A != 0) // Collision return true; } } return false; }
public bool Collision(GameObject check) { return(check.GetBounds().Contains(GetBounds())); }
public static void GizmosDrawCollider( GameObject go, Color color, Vector3 offset, bool recursive = false ) { if ( recursive ) for ( int i = 0 ; i < go.transform.childCount; i++ ) GizmosDrawCollider ( go.transform.GetChild ( i ).gameObject, color, offset, recursive ); PolygonCollider2D poly = go.GetComponent<PolygonCollider2D>(); if ( poly != null ) { Vector3 position = go.transform.position + (Vector3)poly.offset + offset; Gizmos.color = color; for ( int i = 0; i < poly.points.Length - 1; i++ ) Gizmos.DrawLine ( position + (Vector3)poly.points[i], position + (Vector3)poly.points[i + 1] ); Gizmos.DrawLine ( position + (Vector3)poly.points[0], position + (Vector3)poly.points[poly.points.Length - 1] ); Gizmos.color = Color.white; return; } CircleCollider2D circ = go.GetComponent<CircleCollider2D> (); if ( circ != null ) { Vector3 position = go.transform.position + (Vector3)circ.offset + offset; // Gizmos.color = Color.clear; // float r2 = circ.radius * 1.5f; // Gizmos.DrawCube ( position, new Vector3 ( r2, r2, r2 ) ); // Gizmos.color = Color.white; Handles.color = color; Handles.DrawWireDisc ( position, Vector3.back, circ.radius ); Handles.color = Color.white; return; } BoxCollider2D box2d = go.GetComponent<BoxCollider2D> (); if ( box2d != null ) { Vector3 position = go.transform.position + (Vector3)box2d.offset + offset; // Gizmos.color = Color.clear; // Gizmos.DrawCube ( position, box.size ); // Gizmos.color = Color.white; Gizmos.color = color; Gizmos.DrawWireCube ( position, box2d.size ); Gizmos.color = Color.white; return; } BoxCollider box = go.GetComponent<BoxCollider> (); if ( box != null ) { Vector3 position = go.transform.position + (Vector3)box.center + offset; // Gizmos.color = Color.clear; // Gizmos.DrawCube ( position, box.size ); // Gizmos.color = Color.white; Matrix4x4 matrixCopy = Gizmos.matrix; Gizmos.matrix = Matrix4x4.TRS ( position, go.transform.rotation, go.transform.lossyScale ); Gizmos.color = color; Gizmos.DrawWireCube ( /*position*/Vector3.zero, box.size ); Gizmos.color = Color.white; Gizmos.matrix = matrixCopy; return; } Bounds bounds = go.GetBounds ( GetBoundsOption.ChildOnly ); if ( bounds.size != Vector3.zero ) { Vector3 position = bounds.center + offset; // Gizmos.color = Color.clear; // Gizmos.DrawCube ( offset, bounds.size ); // Gizmos.color = Color.white; Gizmos.color = color; Gizmos.DrawWireCube ( position, bounds.size ); Gizmos.color = Color.white; return; } }
public static void HandlesDrawCollider( GameObject go, Color color, Vector3 offset ) { PolygonCollider2D poly = go.GetComponent<PolygonCollider2D> (); if ( poly != null ) { Vector3 position = go.transform.position + (Vector3)poly.offset + offset; Handles.color = color; for ( int i = 0; i < poly.points.Length - 1; i++ ) Handles.DrawLine ( position + (Vector3)poly.points[i], position + (Vector3)poly.points[i + 1] ); Handles.DrawLine ( position + (Vector3)poly.points[0], position + (Vector3)poly.points[poly.points.Length - 1] ); Gizmos.color = Color.white; return; } CircleCollider2D circ = go.GetComponent<CircleCollider2D> (); if ( circ != null ) { Vector3 position = go.transform.position + (Vector3)circ.offset + offset; Handles.color = Color.clear; float r2 = circ.radius * 1.5f; HandlesDrawWireCube ( position, new Vector3 ( r2, r2, r2 ) ); Handles.color = Color.white; return; } BoxCollider2D box = go.GetComponent<BoxCollider2D> (); if ( box != null ) { Vector3 position = go.transform.position + (Vector3)box.offset + offset; Handles.color = color; HandlesDrawWireCube ( position, box.size ); Handles.color = Color.white; return; } Bounds bounds = go.GetBounds ( GetBoundsOption.ChildOnly ); if ( bounds.size != Vector3.zero ) { Vector3 position = bounds.center + offset; Handles.color = color; HandlesDrawWireCube ( position, bounds.size ); Handles.color = Color.white; return; } }