-
-
Save nomissbowling/eb1de312f7c1cb82a419f383e8888c57 to your computer and use it in GitHub Desktop.
Unity&VR 視点ポインターが当たったときオブジェクトの透明度を変える
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Opacity : MonoBehaviour | |
{ | |
bool sight; | |
Color color; | |
float red, green, blue, alpha; //RGBを操作するための変数 | |
// Use this for initialization | |
void Start() | |
{ //マテリアルの情報を取得 | |
alpha = GetComponent<MeshRenderer>().material.color.a; | |
red = GetComponent<MeshRenderer>().material.color.r; | |
green = GetComponent<MeshRenderer>().material.color.g; | |
blue = GetComponent<MeshRenderer>().material.color.b; | |
//最初の透明度値、0~1で設定、0になるほど透明になっていく | |
alpha = 0.5f; | |
//透明度を反映させる。色は変えない | |
GetComponent<MeshRenderer>().material.color = new Color(red, green, blue, alpha); | |
sight = false; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if (sight == true) | |
{ | |
alpha = alpha + Time.deltaTime * 0.5f; //徐々に | |
//今回は色を変更していないので、alpha以外は最初の値 | |
GetComponent<MeshRenderer>().material.color = new Color(red, green, blue, alpha); | |
} | |
} | |
//視点がオブジェクトに入ったら | |
public void OnEnterPointer() | |
{ | |
sight = true; | |
} | |
//視点がオブジェクトから外れたら | |
public void OnExitPonter() | |
{ | |
sight = false; | |
alpha = 0.5f; | |
GetComponent<MeshRenderer>().material.color = new Color(red, green, blue, alpha); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment