Skip to content

Instantly share code, notes, and snippets.

@nabesi777
Created September 29, 2018 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nabesi777/e9227a9bc8fbf2697c75da0f0e973ef1 to your computer and use it in GitHub Desktop.
Save nabesi777/e9227a9bc8fbf2697c75da0f0e973ef1 to your computer and use it in GitHub Desktop.
Unity&VR 視点ポインターが当たったときオブジェクトの透明度を変える
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