GvrControllerTooltipsSimple.cs
2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//-----------------------------------------------------------------------
// <copyright file="GvrControllerTooltipsSimple.cs" company="Google Inc.">
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
// The controller is not available for versions of Unity without the
// GVR native integration.
using System.Collections;
using UnityEngine;
/// <summary>A lightweight tooltip designed to minimize draw calls.</summary>
[ExecuteInEditMode]
[HelpURL("https://developers.google.com/vr/reference/unity/class/GvrControllerTooltipsSimple")]
public class GvrControllerTooltipsSimple : MonoBehaviour, IGvrArmModelReceiver
{
private MeshRenderer tooltipRenderer;
private MaterialPropertyBlock materialPropertyBlock;
private int colorId;
/// <summary>Gets or sets the arm model used to position the controller.</summary>
/// <value>The arm model used to position the controller.</value>
public GvrBaseArmModel ArmModel { get; set; }
/// <summary>Updates the tooltip visualation based on the arm model.</summary>
protected void OnVisualUpdate()
{
float alpha = ArmModel != null ? ArmModel.TooltipAlphaValue : 1.0f;
materialPropertyBlock.SetColor(colorId, new Color(1, 1, 1, alpha));
tooltipRenderer.SetPropertyBlock(materialPropertyBlock);
}
private void Awake()
{
Initialize();
}
private void OnEnable()
{
GvrControllerInput.OnPostControllerInputUpdated += OnPostControllerInputUpdated;
}
private void OnDisable()
{
GvrControllerInput.OnPostControllerInputUpdated -= OnPostControllerInputUpdated;
}
private void OnValidate()
{
if (!Application.isPlaying)
{
Initialize();
OnVisualUpdate();
}
}
private void Initialize()
{
if (tooltipRenderer == null)
{
tooltipRenderer = GetComponent<MeshRenderer>();
}
if (materialPropertyBlock == null)
{
materialPropertyBlock = new MaterialPropertyBlock();
}
colorId = Shader.PropertyToID("_Color");
}
private void OnPostControllerInputUpdated()
{
OnVisualUpdate();
}
}