Tile with background

 

关键代码如下

   void TileBackgroundImages()
    {
        var currentpositon = cameraReference.position.x;
//        var currentpositon = testcube.position.x;
        // avoid call this method every frame.
        if (HasLeftNeighbourImage==false || HasRightNeighbourImgae==false)
        {
            for (int i = 0; i < BackgroundTransforms.Length; i++)
            {
                var backgroundpositon = BackgroundTransforms[i].position;

                //Judge whether camera move to the right threshold , if true, instantiate a new background image. set the two neightbour boolean variable.
                if ((backgroundpositon.x + backgroundRadius[i]) - (currentpositon + cameraRadius) < CameraEdgeThreshold && (backgroundpositon.x + backgroundRadius[i]) - (currentpositon + cameraRadius) >0&& !HasRightNeighbourImgae)
                {
                    HasRightNeighbourImgae = true;
                    //Move to right
                    var newposition = new Vector3(backgroundpositon.x + backgroundRadius[i]*2, backgroundpositon.y, backgroundpositon.z);
                    var newRightNeighbour= Instantiate(BackgroundTransforms[i], newposition, Quaternion.identity) as Transform;


                    newRightNeighbour.localScale = IsReverse
                                   ? new Vector3(
                                         newRightNeighbour.localScale.x * -1,
                                         newRightNeighbour.localScale.y,
                                         newRightNeighbour.localScale.z)
                                   : newRightNeighbour.localScale;
                    newRightNeighbour.GetComponent<TileBackground>().HasLeftNeighbourImage = true;
                    newRightNeighbour.GetComponent<TileBackground>().HasRightNeighbourImgae = false;
                }
                //Judge whether camera move to the left threshold , if true, instantiate a new background image. set the two neightbour boolean variable.
                else if (currentpositon - cameraRadius - (backgroundpositon.x - backgroundRadius[i]) < CameraEdgeThreshold && currentpositon - cameraRadius - (backgroundpositon.x - backgroundRadius[i]) > 0&& !HasLeftNeighbourImage)
                {
                    HasLeftNeighbourImage = true;
                    //Move to left
                    var newposition = new Vector3(backgroundpositon.x - backgroundRadius[i]*2, backgroundpositon.y, backgroundpositon.z);
                    var newLeftNeighbour= Instantiate(BackgroundTransforms[i], newposition, Quaternion.identity) as Transform;

                    newLeftNeighbour.localScale = IsReverse
                                                       ? new Vector3(
                                                             newLeftNeighbour.localScale.x * -1,
                                                             newLeftNeighbour.localScale.y,
                                                             newLeftNeighbour.localScale.z)
                                                       : newLeftNeighbour.localScale; 

//                    newLeftNeighbour.GetComponent<TileBackground>().IsReverse = true;
                    newLeftNeighbour.GetComponent<TileBackground>().HasRightNeighbourImgae = true;
                    newLeftNeighbour.GetComponent<TileBackground>().HasLeftNeighbourImage =false;

                }
                
//                var abscameraposition = Mathf.Abs(cameraReference.position.x);
//                var absbackimageposition = Mathf.Abs(backgroundpositon.x);
//                if (Mathf.Abs(abscameraposition + cameraRadius - absbackimageposition - backgroundRadius[i]) > maxcriticalValueX)
//                {
//                    Debug.Log("i exceed max critical value");
//                }
//                else
//                {
//                    Debug.Log("i exceed min critical value");
//                }

            }
        }
        

        
    }

  

posted @ 2014-07-06 17:03  penney  阅读(152)  评论(0)    收藏  举报