2016-10-20 10 views
0

Unity2Dでは、カメラが最後を見る直前に背景スプライトを繰り返すスクリプトを作成しました。グラフィック表示を繰り返す

は、ここに私のコードです:

using UnityEngine; 
using System.Collections; 

[RequireComponent (typeof(SpriteRenderer))] 

public class Tiling : MonoBehaviour { 

    public int offsetX = 2;   // the offset so that we don't get any weird errors 

    // these are used for checking if we need to instantiate stuff 
    public bool hasARightBuddy = false; 
    public bool hasALeftBuddy = false; 

    public bool reverseScale = false; // used if the object is not tilable 

    private float spriteWidth = 0f;  // the width of our element 
    private Camera cam; 
    private Transform myTransform; 
    private float localSc; 

    void Awake() { 
     cam = Camera.main; 
     myTransform = transform; 
     localSc = transform.localScale.x; 
    } 

    // Use this for initialization 
    void Start() { 
     SpriteRenderer sRenderer = GetComponent<SpriteRenderer>(); 
     spriteWidth = sRenderer.sprite.bounds.size.x * transform.localScale.x; 
    } 

    // Update is called once per frame 
    void Update() { 
     // does it still need buddies? If not do nothing 
     if (hasALeftBuddy == false || hasARightBuddy == false) { 
      // calculate the cameras extend (half the width) of what the camera can see in world coordinates 
      float camHorizontalExtend = cam.orthographicSize * Screen.width/Screen.height; 

      // calculate the x position where the camera can see the edge of the sprite (element) 
      float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend; 
      float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizontalExtend; 

      // checking if we can see the edge of the element and then calling MakeNewBuddy if we can 
      if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false) 
      { 
       MakeNewBuddy (1); 
       hasARightBuddy = true; 
      } 
      else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false) 
      { 
       MakeNewBuddy (-1); 
       hasALeftBuddy = true; 
      } 
     } 
    } 

    // a function that creates a buddy on the side required 
    void MakeNewBuddy (int rightOrLeft) { 
     // calculating the new position for our new buddy 
     Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z); 
     // instantating our new body and storing him in a variable 
     Transform newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform; 

     newBuddy.parent = myTransform.parent; 

     // if not tilable let's reverse the x size on our object to get rid of missmatches 
     if (reverseScale == true) { 
      newBuddy.localScale = new Vector3 (localSc*-1 , 1, 1); 
     } 



     if (rightOrLeft == 1) { //if this function was called to make a right buddy (1) 
      newBuddy.GetComponent<Tiling>().hasALeftBuddy = true; 
     } 
     else { //else we just made a left buddy, so it has a right copy 
      newBuddy.GetComponent<Tiling>().hasARightBuddy = true; 
     } 
    } 

さて、スクリプトが背景スプライトに添付され、それが正常に動作します。 bool reverseScaleがあり、画像が反転していることがわかります。 画像が反復可能でない場合(ピクセルレベルで終わりと始まりが一致しない場合)、xを元に戻して(* -1)ミラーリングできます。

私が言ったように、reverseScaleを無効にして起動すると、奇妙なことにeveythingが動作します。 reverseScaleを有効にすると、混乱します。オーバーラップし、ひどくスケールされたスプライトの無限ループ。ゲームがクラッシュする

私には何が欠けていますか?最悪の場合(しかし、それでも起こるはずはありません)、コードスニペットは一致しないイメージを作成する必要があります。なぜそれがプログラムを壊していますか?

EDIT:

私は答えをEnfyveするためのソリューションのおかげで見つけました。私は、理由のために単一のものではなく、全体のグラフィックコンポーネントのスケールを反転していました。代わりにflipXフィールドを使用する必要があります。また、ミスマッチを避けるために、2つのタイルごとに1つだけフリップする必要があります。

+0

クラスには必ず暴走ループがないため、問題の原因となっている別の場所にある必要があります。また、 'newBuddy'の' Tiling'コンポーネントを取得していますが、最初にコンポーネントを追加しましたか? (私はここにそれを見ない) – Enfyve

答えて

1

SpriteRendererにスプライトを描画するためのプロパティが既に含まれていますが、代わりにflipXを使用してください。

関連する問題