Created
December 3, 2013 07:50
-
-
Save nakamura001/7765503 to your computer and use it in GitHub Desktop.
Unityでマウスカーソルの位置のテクスチャのピクセル情報を取得するサンプル。ピクセル情報を取得したいオブジェクトはMesh Colliderが使われていなければ動作しません。Box Colliderなどを使っている場合には変更して下さい。また、使用しているテクスチャの画像にアクセスするのでInspectorでRead/Write Enabledにチェックを付けておいてして下さい。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class HitTest : MonoBehaviour { | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
RaycastHit hit; | |
if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) | |
return; | |
Renderer renderer = hit.collider.renderer; | |
Texture2D tex = renderer.material.mainTexture as Texture2D; | |
Vector2 pixelUV = hit.textureCoord; | |
pixelUV.x *= tex.width; | |
pixelUV.y *= tex.height; | |
int x = (int)Mathf.Floor(pixelUV.x); | |
int y = (int)Mathf.Floor(pixelUV.y); | |
Debug.Log( "Texture Name : " + hit.transform.gameObject.name + ", u=" + pixelUV.x + ", v=" + pixelUV.y + ", w=" + tex.width + ", h=" + tex.height + ", " + tex.GetPixel(x, y)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment