// Use this for initialization void Start() { mMyCollidableTex = GetComponent <CollidableTexture>(); mOtherCollidableTex = mLargeTexObject.GetComponent <CollidableTexture>(); if ((null == mMyCollidableTex) || (null == mOtherCollidableTex)) { Debug.Log("Both GameObjects must define CollidableTexture component!"); } if (mTarget == null) { GameObject g = Resources.Load("Prefabs/Target") as GameObject; mTarget = Instantiate(g) as GameObject; mTarget.GetComponent <Renderer>().enabled = false; } }
/// <summary> /// Assumes this and other both has SpriteRenderer, and both has CollidableTexture components /// </summary> public bool CollideTextures(CollidableTexture otherTex, Bounds otherBound, out Vector3 collidePoint) { collidePoint = Vector3.zero; #region details of per-pixel texture collision bool touches = mMyRenderer.bounds.Intersects(otherBound); if (touches) { bool pixelTouch = false; ComputeOrigin(); otherTex.ComputeOrigin(); int i = 0; while ((!pixelTouch) && (i < WidthInPixel)) { int j = 0; while ((!pixelTouch) && (j < HeightInPixel)) { collidePoint = IndexToCameraPosition(i, j); Color myColor = GetColor(i, j); if (myColor.a > 0) { Vector2 otherIndex = otherTex.CameraPositionToIndex(collidePoint); if (otherTex.IndexInBound(otherIndex)) { pixelTouch = (otherTex.GetColor((int)(otherIndex.x), (int)(otherIndex.y)).a > 0); } } j++; } i++; } touches = pixelTouch; } #endregion return(touches); /* * (105, 45): is the tip of the net-outline! * * Vector2 ind = otherTex.CameraPositionToIndex(IndexToCameraPosition(105, 45)); * collidePoint = otherTex.IndexToCameraPosition((int)ind.x, (int)ind.y); * // collidePoint = IndexToCameraPosition(105, 45); * return true; */ }