2016-03-21 3 views
0

私は(TV上の平面のような)オブジェクトのテクスチャを変更するスクリプトを、持っている固定介して、キャンバス画像を変更する方法回:スクリプト

public Texture[] frames;    // array of textures 
public float framesPerSecond = 2.0f; // delay between frames 

void Update() 
{ 
    int index = (int)(Time.time * framesPerSecond) % frames.Length; 
    GetComponent<Renderer>().material.mainTexture = frames[index]; 
} 

このスクリプトでは、私は飛行機のテクスチャを変更することができます、クワッドなどですが、私はキャンバス画像のためにそれを変更する必要があります。私は "画像"スクリプトを変更することはできません。このスクリプトの値「ソースイメージ」にアクセスする必要がありますか?それは正しいでしょうか?

それで、どうすればImageコンポーネントにアクセスできますか?私はこれを書くことはできません。

Image image = GetComponent<Image>();

enter image description here

+1

私は以下のことをテストすることができないため、私はそれを答えにしていません。あなたは 'Unityengine.UIを使用して 'を追加する必要があり、' GetComponent ().setImage = someImage; 'の行に沿って何かを使うことができます。 – Rana

+0

@Rana setImageがどこから得られたのかわかりません... – Programmer

+0

it私の頭の中でXDだった。私はそれは "ラインに沿って"と言った...私はスプライトと呼ばれるプロパティを持っていることを確認した。だからあなたは 'GetComponent ().sprite = someSprite;'私は間違っているかもしれません... – Rana

答えて

3

あなたがImageを使用する場合は、変換に必要Textureimage.spriteプロパティ

方法1変更を行っSprite画像

public Texture[] frames;    // array of textures 
public float framesPerSecond = 2.0f; // delay between frames 
Image image = null; 

void Start() 
{ 
    //Get Image Reference 
    image = gameObject.GetComponent<Image>(); 
} 

void Update() 
{ 
     int index = (int)(Time.time * framesPerSecond) % frames.Length; 

     //Convert Texture to Sprite 
     Sprite s = Sprite.Create((Texture2D)frames[index], new Rect(0, 0, frames[index].width, frames[index].height), 
           Vector2.zero, 0); 
     image.sprite = s; //Change The Image 
} 

方法2RawImageimage.texture あなたがRawImageを使用する場合は、だけimage.textureプロパティを変更する必要があります。

public Texture[] frames;    // array of textures 
public float framesPerSecond = 2.0f; // delay between frames 
RawImage image = null; 

void Start() 
{ 
//Get Raw Image Reference 
image = gameObject.GetComponent<RawImage>(); 
} 

void Update() 
{ 
    int index = (int)(Time.time * framesPerSecond) % frames.Length; 
    image.texture = frames[index]; //Change The Image 
} 

私はあなたが方法2で行くお勧めします。これは、あなたのシーンの現在の画像削除する必要があることを意味します。 >UI - - その後ゲームオブジェクトに行く>RAW画像RawImageを作成します。あなたが常にイメージを変更しているので、Raw Imageは効率的なです。 方法1を使用すると、遅くであり、あなたのゲーム/アプリガベージコレクタを発揮します。

関連する問題