言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

Texture2D.GetPixels

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public function GetPixels(miplevel: int = 0): Color[];
public Color[] GetPixels(int miplevel = 0);
public def GetPixels(miplevel as int = 0) as Color[]

Description

ピクセルのカラー配列を取得します

この機能は、全体のピクセルカラーの配列を返す テクスチャのミップレベル 平面の配列を返す、ピクセルは右から左にレイアウトされている 下から上に(行の後の行)。配列サイズは使用されるミップレベルの高さ、幅である。 デフォルトのミップレベルはゼロ(ベーステクスチャ)です\nその場合、サイズはテクスチャのサイズにぴたりと一致します。 一般的にミップレベルのサイズは\nmipWidth=max(1,width>>miplevel) そして高さも同様です。 テクスチャはインポートセッティングのRead/Write Enabledフラグを設定しなくてはいけません。でなければこのファンクションは失敗するでしょう。 GetPixelsを使用すると、繰り返しGetPixelとを呼び出すよりも高速になります 特に大規模なテクスチャであれば高速になります。さらに、GetPixelsは個々のミップマップレベルにアクセスすることができます。

	// Get a rectangular area of a texture and place it into a new
	// texture the size of the rectangle.
	
	// Source texture and the rectangular area we want to extract.
	var sourceTex: Texture2D;
	var sourceRect: Rect;
	
	
	function Start () {
		// The values in the Rect structure are floats, so round them
		// down to the nearest integer.
		var x = Mathf.FloorToInt(sourceRect.x);
		var y = Mathf.FloorToInt(sourceRect.x);
		var width = Mathf.FloorToInt(sourceRect.width);
		var height = Mathf.FloorToInt(sourceRect.height);
		
		// Get the pixel block and place it into a new texture.
		var pix = sourceTex.GetPixels(x, y, width, height);
		var destTex = new Texture2D(width, height);
		destTex.SetPixels(pix);
		destTex.Apply();
		
		// Set the current object's texture to show the
		// extracted rectangle.
		renderer.material.mainTexture = destTex;
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Texture2D sourceTex;
    public Rect sourceRect;
    void Start() {
        int x = Mathf.FloorToInt(sourceRect.x);
        int y = Mathf.FloorToInt(sourceRect.x);
        int width = Mathf.FloorToInt(sourceRect.width);
        int height = Mathf.FloorToInt(sourceRect.height);
        Color[] pix = sourceTex.GetPixels(x, y, width, height);
        Texture2D destTex = new Texture2D(width, height);
        destTex.SetPixels(pix);
        destTex.Apply();
        renderer.material.mainTexture = destTex;
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public sourceTex as Texture2D

	public sourceRect as Rect

	def Start() as void:
		x as int = Mathf.FloorToInt(sourceRect.x)
		y as int = Mathf.FloorToInt(sourceRect.x)
		width as int = Mathf.FloorToInt(sourceRect.width)
		height as int = Mathf.FloorToInt(sourceRect.height)
		pix as (Color) = sourceTex.GetPixels(x, y, width, height)
		destTex as Texture2D = Texture2D(width, height)
		destTex.SetPixels(pix)
		destTex.Apply()
		renderer.material.mainTexture = destTex

See Also: SetPixels, mipmapCount.

public function GetPixels(x: int, y: int, blockWidth: int, blockHeight: int, miplevel: int = 0): Color[];
public Color[] GetPixels(int x, int y, int blockWidth, int blockHeight, int miplevel = 0);
public def GetPixels(x as int, y as int, blockWidth as int, blockHeight as int, miplevel as int = 0) as Color[]

Description

ピクセルのカラー配列を取得します

この関数は、上記のGetPixelsの拡張版です。\n 全体ミップレベルだけでなくyとxで始まるblockHeightとblockWidthを返も返します。\n ブロックは使用されるMIPレベルに適合しなければなりません。返される配列は blockWidth* blockHeight サイズです。