Initialer Upload neues Unity-Projekt
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class ButtonEffect : MonoBehaviour
|
||||
{
|
||||
public void OnButtonDown(Hand fromHand)
|
||||
{
|
||||
ColorSelf(Color.cyan);
|
||||
fromHand.TriggerHapticPulse(1000);
|
||||
}
|
||||
|
||||
public void OnButtonUp(Hand fromHand)
|
||||
{
|
||||
ColorSelf(Color.white);
|
||||
}
|
||||
|
||||
private void ColorSelf(Color newColor)
|
||||
{
|
||||
Renderer[] renderers = this.GetComponentsInChildren<Renderer>();
|
||||
for (int rendererIndex = 0; rendererIndex < renderers.Length; rendererIndex++)
|
||||
{
|
||||
renderers[rendererIndex].material.color = newColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fdc6e48706c13fa4a8f807ff0f16e9de
|
||||
timeCreated: 1530130146
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,53 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class ButtonExample : MonoBehaviour
|
||||
{
|
||||
public HoverButton hoverButton;
|
||||
|
||||
public GameObject prefab;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
hoverButton.onButtonDown.AddListener(OnButtonDown);
|
||||
}
|
||||
|
||||
private void OnButtonDown(Hand hand)
|
||||
{
|
||||
StartCoroutine(DoPlant());
|
||||
}
|
||||
|
||||
private IEnumerator DoPlant()
|
||||
{
|
||||
GameObject planting = GameObject.Instantiate<GameObject>(prefab);
|
||||
planting.transform.position = this.transform.position;
|
||||
planting.transform.rotation = Quaternion.Euler(0, Random.value * 360f, 0);
|
||||
|
||||
planting.GetComponentInChildren<MeshRenderer>().material.SetColor("_TintColor", Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f));
|
||||
|
||||
Rigidbody rigidbody = planting.GetComponent<Rigidbody>();
|
||||
if (rigidbody != null)
|
||||
rigidbody.isKinematic = true;
|
||||
|
||||
|
||||
Vector3 initialScale = Vector3.one * 0.01f;
|
||||
Vector3 targetScale = Vector3.one * (1 + (Random.value * 0.25f));
|
||||
|
||||
float startTime = Time.time;
|
||||
float overTime = 0.5f;
|
||||
float endTime = startTime + overTime;
|
||||
|
||||
while (Time.time < endTime)
|
||||
{
|
||||
planting.transform.localScale = Vector3.Slerp(initialScale, targetScale, (Time.time - startTime) / overTime);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
|
||||
if (rigidbody != null)
|
||||
rigidbody.isKinematic = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 80df3856ba408c944a3cf528cf7bad83
|
||||
timeCreated: 1534552033
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,119 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
//
|
||||
// Purpose: Demonstrates the use of the controller hint system
|
||||
//
|
||||
//=============================================================================
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using Valve.VR;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
//-------------------------------------------------------------------------
|
||||
public class ControllerHintsExample : MonoBehaviour
|
||||
{
|
||||
private Coroutine buttonHintCoroutine;
|
||||
private Coroutine textHintCoroutine;
|
||||
|
||||
//-------------------------------------------------
|
||||
public void ShowButtonHints( Hand hand )
|
||||
{
|
||||
if ( buttonHintCoroutine != null )
|
||||
{
|
||||
StopCoroutine( buttonHintCoroutine );
|
||||
}
|
||||
buttonHintCoroutine = StartCoroutine( TestButtonHints( hand ) );
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
public void ShowTextHints( Hand hand )
|
||||
{
|
||||
if ( textHintCoroutine != null )
|
||||
{
|
||||
StopCoroutine( textHintCoroutine );
|
||||
}
|
||||
textHintCoroutine = StartCoroutine( TestTextHints( hand ) );
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
public void DisableHints()
|
||||
{
|
||||
if ( buttonHintCoroutine != null )
|
||||
{
|
||||
StopCoroutine( buttonHintCoroutine );
|
||||
buttonHintCoroutine = null;
|
||||
}
|
||||
|
||||
if ( textHintCoroutine != null )
|
||||
{
|
||||
StopCoroutine( textHintCoroutine );
|
||||
textHintCoroutine = null;
|
||||
}
|
||||
|
||||
foreach ( Hand hand in Player.instance.hands )
|
||||
{
|
||||
ControllerButtonHints.HideAllButtonHints( hand );
|
||||
ControllerButtonHints.HideAllTextHints( hand );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Cycles through all the button hints on the controller
|
||||
//-------------------------------------------------
|
||||
private IEnumerator TestButtonHints( Hand hand )
|
||||
{
|
||||
ControllerButtonHints.HideAllButtonHints( hand );
|
||||
|
||||
while ( true )
|
||||
{
|
||||
for (int actionIndex = 0; actionIndex < SteamVR_Input.actionsIn.Length; actionIndex++)
|
||||
{
|
||||
ISteamVR_Action_In action = SteamVR_Input.actionsIn[actionIndex];
|
||||
if (action.GetActive(hand.handType))
|
||||
{
|
||||
ControllerButtonHints.ShowButtonHint(hand, action);
|
||||
yield return new WaitForSeconds(1.0f);
|
||||
ControllerButtonHints.HideButtonHint(hand, action);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
|
||||
ControllerButtonHints.HideAllButtonHints( hand );
|
||||
yield return new WaitForSeconds( 1.0f );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Cycles through all the text hints on the controller
|
||||
//-------------------------------------------------
|
||||
private IEnumerator TestTextHints( Hand hand )
|
||||
{
|
||||
ControllerButtonHints.HideAllTextHints( hand );
|
||||
|
||||
while ( true )
|
||||
{
|
||||
for (int actionIndex = 0; actionIndex < SteamVR_Input.actionsIn.Length; actionIndex++)
|
||||
{
|
||||
ISteamVR_Action_In action = SteamVR_Input.actionsIn[actionIndex];
|
||||
if (action.GetActive(hand.handType))
|
||||
{
|
||||
ControllerButtonHints.ShowTextHint(hand, action, action.GetShortName());
|
||||
yield return new WaitForSeconds(3.0f);
|
||||
ControllerButtonHints.HideTextHint(hand, action);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
|
||||
ControllerButtonHints.HideAllTextHints(hand);
|
||||
yield return new WaitForSeconds(3.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79e7ef0af37bbcb40ab89c4cc2481018
|
||||
timeCreated: 1544851960
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,97 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using Valve.VR;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class CustomSkeletonHelper : MonoBehaviour
|
||||
{
|
||||
public Retargetable wrist;
|
||||
|
||||
public Finger[] fingers;
|
||||
public Thumb[] thumbs;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
for (int fingerIndex = 0; fingerIndex < fingers.Length; fingerIndex++)
|
||||
{
|
||||
Finger finger = fingers[fingerIndex];
|
||||
finger.metacarpal.destination.rotation = finger.metacarpal.source.rotation;
|
||||
finger.proximal.destination.rotation = finger.proximal.source.rotation;
|
||||
finger.middle.destination.rotation = finger.middle.source.rotation;
|
||||
finger.distal.destination.rotation = finger.distal.source.rotation;
|
||||
}
|
||||
for (int thumbIndex = 0; thumbIndex < thumbs.Length; thumbIndex++)
|
||||
{
|
||||
Thumb thumb = thumbs[thumbIndex];
|
||||
thumb.metacarpal.destination.rotation = thumb.metacarpal.source.rotation;
|
||||
thumb.middle.destination.rotation = thumb.middle.source.rotation;
|
||||
thumb.distal.destination.rotation = thumb.distal.source.rotation;
|
||||
}
|
||||
|
||||
wrist.destination.position = wrist.source.position;
|
||||
wrist.destination.rotation = wrist.source.rotation;
|
||||
}
|
||||
|
||||
public enum MirrorType
|
||||
{
|
||||
None,
|
||||
LeftToRight,
|
||||
RightToLeft
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Retargetable
|
||||
{
|
||||
public Transform source;
|
||||
public Transform destination;
|
||||
|
||||
public Retargetable(Transform source, Transform destination)
|
||||
{
|
||||
this.source = source;
|
||||
this.destination = destination;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Thumb
|
||||
{
|
||||
public Retargetable metacarpal;
|
||||
public Retargetable middle;
|
||||
public Retargetable distal;
|
||||
|
||||
public Transform aux;
|
||||
|
||||
public Thumb(Retargetable metacarpal, Retargetable middle, Retargetable distal, Transform aux)
|
||||
{
|
||||
this.metacarpal = metacarpal;
|
||||
this.middle = middle;
|
||||
this.distal = distal;
|
||||
this.aux = aux;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Finger
|
||||
{
|
||||
public Retargetable metacarpal;
|
||||
public Retargetable proximal;
|
||||
public Retargetable middle;
|
||||
public Retargetable distal;
|
||||
|
||||
public Transform aux;
|
||||
|
||||
public Finger(Retargetable metacarpal, Retargetable proximal, Retargetable middle, Retargetable distal, Transform aux)
|
||||
{
|
||||
this.metacarpal = metacarpal;
|
||||
this.proximal = proximal;
|
||||
this.middle = middle;
|
||||
this.distal = distal;
|
||||
this.aux = aux;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 294fb0b20d456c749966ea34ca82355a
|
||||
timeCreated: 1531864824
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
233
Assets/SteamVR/InteractionSystem/Samples/Scripts/FloppyHand.cs
Normal file
233
Assets/SteamVR/InteractionSystem/Samples/Scripts/FloppyHand.cs
Normal file
@ -0,0 +1,233 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class FloppyHand : MonoBehaviour
|
||||
{
|
||||
protected float fingerFlexAngle = 140;
|
||||
|
||||
public SteamVR_Action_Single squeezyAction = SteamVR_Input.GetAction<SteamVR_Action_Single>("Squeeze");
|
||||
public SteamVR_Input_Sources inputSource;
|
||||
|
||||
[System.Serializable]
|
||||
public class Finger
|
||||
{
|
||||
public float mass;
|
||||
|
||||
[Range(0, 1)]
|
||||
public float pos;
|
||||
|
||||
public Vector3 forwardAxis;
|
||||
|
||||
public SkinnedMeshRenderer renderer;
|
||||
[HideInInspector]
|
||||
public SteamVR_Action_Single squeezyAction;
|
||||
public SteamVR_Input_Sources inputSource;
|
||||
|
||||
public Transform[] bones;
|
||||
public Transform referenceBone;
|
||||
public Vector2 referenceAngles;
|
||||
|
||||
public enum eulerAxis
|
||||
{
|
||||
X, Y, Z
|
||||
}
|
||||
public eulerAxis referenceAxis;
|
||||
|
||||
|
||||
[HideInInspector]
|
||||
public float flexAngle;
|
||||
|
||||
private Vector3[] rotation;
|
||||
private Vector3[] velocity;
|
||||
private Transform[] boneTips;
|
||||
private Vector3[] oldTipPosition;
|
||||
private Vector3[] oldTipDelta;
|
||||
private Vector3[,] inertiaSmoothing;
|
||||
|
||||
float squeezySmooth;
|
||||
|
||||
private int inertiaSteps = 10;
|
||||
private float k = 400;
|
||||
private float damping = 8;
|
||||
private Quaternion[] startRot;
|
||||
|
||||
public void ApplyForce(Vector3 worldForce)
|
||||
{
|
||||
for (int i = 0; i < startRot.Length; i++)
|
||||
{
|
||||
velocity[i] += worldForce / 50;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Init()
|
||||
{
|
||||
startRot = new Quaternion[bones.Length];
|
||||
rotation = new Vector3[bones.Length];
|
||||
velocity = new Vector3[bones.Length];
|
||||
oldTipPosition = new Vector3[bones.Length];
|
||||
oldTipDelta = new Vector3[bones.Length];
|
||||
boneTips = new Transform[bones.Length];
|
||||
inertiaSmoothing = new Vector3[bones.Length, inertiaSteps];
|
||||
for (int i = 0; i < bones.Length; i++)
|
||||
{
|
||||
startRot[i] = bones[i].localRotation;
|
||||
if (i < bones.Length - 1)
|
||||
{
|
||||
boneTips[i] = bones[i + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateFinger(float deltaTime)
|
||||
{
|
||||
if (deltaTime == 0)
|
||||
return;
|
||||
|
||||
float squeezeValue = 0;
|
||||
if (squeezyAction != null && squeezyAction.GetActive(inputSource))
|
||||
squeezeValue = squeezyAction.GetAxis(inputSource);
|
||||
|
||||
squeezySmooth = Mathf.Lerp(squeezySmooth, Mathf.Sqrt(squeezeValue), deltaTime * 10);
|
||||
|
||||
if (renderer.sharedMesh.blendShapeCount > 0)
|
||||
{
|
||||
renderer.SetBlendShapeWeight(0, squeezySmooth * 100);
|
||||
}
|
||||
|
||||
float boneRot = 0;
|
||||
if (referenceAxis == eulerAxis.X)
|
||||
boneRot = referenceBone.localEulerAngles.x;
|
||||
if (referenceAxis == eulerAxis.Y)
|
||||
boneRot = referenceBone.localEulerAngles.y;
|
||||
if (referenceAxis == eulerAxis.Z)
|
||||
boneRot = referenceBone.localEulerAngles.z;
|
||||
boneRot = FixAngle(boneRot);
|
||||
|
||||
pos = Mathf.InverseLerp(referenceAngles.x, referenceAngles.y, boneRot);
|
||||
|
||||
if (mass > 0)
|
||||
{
|
||||
for (int boneIndex = 0; boneIndex < bones.Length; boneIndex++)
|
||||
{
|
||||
bool useOffset = boneTips[boneIndex] != null;
|
||||
if (useOffset) // inertia sim
|
||||
{
|
||||
Vector3 offset = (boneTips[boneIndex].localPosition - bones[boneIndex].InverseTransformPoint(oldTipPosition[boneIndex])) / deltaTime;
|
||||
Vector3 inertia = (offset - oldTipDelta[boneIndex]) / deltaTime;
|
||||
oldTipDelta[boneIndex] = offset;
|
||||
|
||||
Vector3 drag = offset * -2;
|
||||
inertia *= -2f;
|
||||
|
||||
for (int offsetIndex = inertiaSteps - 1; offsetIndex > 0; offsetIndex--) // offset inertia steps
|
||||
{
|
||||
inertiaSmoothing[boneIndex, offsetIndex] = inertiaSmoothing[boneIndex, offsetIndex - 1];
|
||||
}
|
||||
inertiaSmoothing[boneIndex, 0] = inertia;
|
||||
|
||||
Vector3 smoothedInertia = Vector3.zero;
|
||||
for (int offsetIndex = 0; offsetIndex < inertiaSteps; offsetIndex++) // offset inertia steps
|
||||
{
|
||||
smoothedInertia += inertiaSmoothing[boneIndex, offsetIndex];
|
||||
}
|
||||
|
||||
smoothedInertia = smoothedInertia / inertiaSteps;
|
||||
//if (boneIndex == 0 && Input.GetKey(KeyCode.Space))
|
||||
// Debug.Log(smoothedInertia);
|
||||
smoothedInertia = PowVector(smoothedInertia / 20, 3) * 20;
|
||||
|
||||
Vector3 forward = forwardAxis;
|
||||
Vector3 forwardDrag = forwardAxis + drag;
|
||||
Vector3 forwardInertia = forwardAxis + smoothedInertia;
|
||||
Quaternion dragQuaternion = Quaternion.FromToRotation(forward, forwardDrag);
|
||||
Quaternion inertiaQuaternion = Quaternion.FromToRotation(forward, forwardInertia);
|
||||
velocity[boneIndex] += FixVector(dragQuaternion.eulerAngles) * 2 * deltaTime;
|
||||
velocity[boneIndex] += FixVector(inertiaQuaternion.eulerAngles) * 50 * deltaTime;
|
||||
velocity[boneIndex] = Vector3.ClampMagnitude(velocity[boneIndex], 1000);
|
||||
|
||||
}
|
||||
|
||||
Vector3 targetPos = pos * Vector3.right * (flexAngle / bones.Length);
|
||||
|
||||
Vector3 springForce = -k * (rotation[boneIndex] - targetPos);
|
||||
var dampingForce = damping * velocity[boneIndex];
|
||||
var force = springForce - dampingForce;
|
||||
var acceleration = force / mass;
|
||||
velocity[boneIndex] += acceleration * deltaTime;
|
||||
rotation[boneIndex] += velocity[boneIndex] * Time.deltaTime;
|
||||
rotation[boneIndex] = Vector3.ClampMagnitude(rotation[boneIndex], 180);
|
||||
if (useOffset)
|
||||
{
|
||||
oldTipPosition[boneIndex] = boneTips[boneIndex].position;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("<b>[SteamVR Interaction]</b> finger mass is zero");
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyTransforms()
|
||||
{
|
||||
for (int i = 0; i < bones.Length; i++)
|
||||
{
|
||||
bones[i].localRotation = startRot[i];
|
||||
bones[i].Rotate(rotation[i], Space.Self);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 FixVector(Vector3 ang)
|
||||
{
|
||||
return new Vector3(FixAngle(ang.x), FixAngle(ang.y), FixAngle(ang.z));
|
||||
}
|
||||
|
||||
private float FixAngle(float ang)
|
||||
{
|
||||
if (ang > 180)
|
||||
ang = -360 + ang;
|
||||
return ang;
|
||||
}
|
||||
|
||||
private Vector3 PowVector(Vector3 vector, float power)
|
||||
{
|
||||
Vector3 sign = new Vector3(Mathf.Sign(vector.x), Mathf.Sign(vector.y), Mathf.Sign(vector.z));
|
||||
vector.x = Mathf.Pow(Mathf.Abs(vector.x), power) * sign.x;
|
||||
vector.y = Mathf.Pow(Mathf.Abs(vector.y), power) * sign.y;
|
||||
vector.z = Mathf.Pow(Mathf.Abs(vector.z), power) * sign.z;
|
||||
return vector;
|
||||
}
|
||||
}
|
||||
|
||||
public Finger[] fingers;
|
||||
|
||||
public Vector3 constforce;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
for (int fingerIndex = 0; fingerIndex < fingers.Length; fingerIndex++)
|
||||
{
|
||||
fingers[fingerIndex].Init();
|
||||
fingers[fingerIndex].flexAngle = fingerFlexAngle;
|
||||
fingers[fingerIndex].squeezyAction = squeezyAction;
|
||||
fingers[fingerIndex].inputSource = inputSource;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
for (int fingerIndex = 0; fingerIndex < fingers.Length; fingerIndex++)
|
||||
{
|
||||
fingers[fingerIndex].ApplyForce(constforce);
|
||||
fingers[fingerIndex].UpdateFinger(Time.deltaTime);
|
||||
fingers[fingerIndex].ApplyTransforms();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 158d046322c150348bfed00c688d3142
|
||||
timeCreated: 1531871869
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,73 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Valve.VR.InteractionSystem;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class FlowerPlanted : MonoBehaviour
|
||||
{
|
||||
private void Start()
|
||||
{
|
||||
Plant();
|
||||
}
|
||||
|
||||
public void Plant()
|
||||
{
|
||||
StartCoroutine(DoPlant());
|
||||
}
|
||||
|
||||
private IEnumerator DoPlant()
|
||||
{
|
||||
Vector3 plantPosition;
|
||||
|
||||
RaycastHit hitInfo;
|
||||
bool hit = Physics.Raycast(this.transform.position, Vector3.down, out hitInfo);
|
||||
if (hit)
|
||||
{
|
||||
plantPosition = hitInfo.point + (Vector3.up * 0.05f);
|
||||
}
|
||||
else
|
||||
{
|
||||
plantPosition = this.transform.position;
|
||||
plantPosition.y = Player.instance.transform.position.y;
|
||||
}
|
||||
|
||||
GameObject planting = this.gameObject;
|
||||
planting.transform.position = plantPosition;
|
||||
planting.transform.rotation = Quaternion.Euler(0, Random.value * 360f, 0);
|
||||
|
||||
#if UNITY_URP
|
||||
Color newColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
|
||||
newColor.a = 0.75f;
|
||||
planting.GetComponentInChildren<MeshRenderer>().material.SetColor("_BaseColor", newColor);
|
||||
#else
|
||||
planting.GetComponentInChildren<MeshRenderer>().material.SetColor("_TintColor", Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f));
|
||||
#endif
|
||||
Rigidbody rigidbody = planting.GetComponent<Rigidbody>();
|
||||
if (rigidbody != null)
|
||||
rigidbody.isKinematic = true;
|
||||
|
||||
|
||||
|
||||
Vector3 initialScale = Vector3.one * 0.01f;
|
||||
Vector3 targetScale = Vector3.one * (1 + (Random.value * 0.25f));
|
||||
|
||||
float startTime = Time.time;
|
||||
float overTime = 0.5f;
|
||||
float endTime = startTime + overTime;
|
||||
|
||||
while (Time.time < endTime)
|
||||
{
|
||||
planting.transform.localScale = Vector3.Slerp(initialScale, targetScale, (Time.time - startTime) / overTime);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
|
||||
if (rigidbody != null)
|
||||
rigidbody.isKinematic = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a7c27a46b80a6445bffe44ea9e91cd0
|
||||
timeCreated: 1529518097
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- plantAction: {instanceID: 0}
|
||||
- hand: {instanceID: 0}
|
||||
- prefabToPlant: {fileID: 1000011210241306, guid: 8a2487c189a464148aadc3af2effa7c8,
|
||||
type: 2}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,149 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
//
|
||||
// Purpose: Demonstrates how to create a simple interactable object
|
||||
//
|
||||
//=============================================================================
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
//-------------------------------------------------------------------------
|
||||
[RequireComponent( typeof( Interactable ) )]
|
||||
public class InteractableExample : MonoBehaviour
|
||||
{
|
||||
private TextMesh generalText;
|
||||
private TextMesh hoveringText;
|
||||
private Vector3 oldPosition;
|
||||
private Quaternion oldRotation;
|
||||
|
||||
private float attachTime;
|
||||
|
||||
private Hand.AttachmentFlags attachmentFlags = Hand.defaultAttachmentFlags & ( ~Hand.AttachmentFlags.SnapOnAttach ) & (~Hand.AttachmentFlags.DetachOthers) & (~Hand.AttachmentFlags.VelocityMovement);
|
||||
|
||||
private Interactable interactable;
|
||||
|
||||
//-------------------------------------------------
|
||||
void Awake()
|
||||
{
|
||||
var textMeshs = GetComponentsInChildren<TextMesh>();
|
||||
generalText = textMeshs[0];
|
||||
hoveringText = textMeshs[1];
|
||||
|
||||
generalText.text = "No Hand Hovering";
|
||||
hoveringText.text = "Hovering: False";
|
||||
|
||||
interactable = this.GetComponent<Interactable>();
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Called when a Hand starts hovering over this object
|
||||
//-------------------------------------------------
|
||||
private void OnHandHoverBegin( Hand hand )
|
||||
{
|
||||
generalText.text = "Hovering hand: " + hand.name;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Called when a Hand stops hovering over this object
|
||||
//-------------------------------------------------
|
||||
private void OnHandHoverEnd( Hand hand )
|
||||
{
|
||||
generalText.text = "No Hand Hovering";
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Called every Update() while a Hand is hovering over this object
|
||||
//-------------------------------------------------
|
||||
private void HandHoverUpdate( Hand hand )
|
||||
{
|
||||
GrabTypes startingGrabType = hand.GetGrabStarting();
|
||||
bool isGrabEnding = hand.IsGrabEnding(this.gameObject);
|
||||
|
||||
if (interactable.attachedToHand == null && startingGrabType != GrabTypes.None)
|
||||
{
|
||||
// Save our position/rotation so that we can restore it when we detach
|
||||
oldPosition = transform.position;
|
||||
oldRotation = transform.rotation;
|
||||
|
||||
// Call this to continue receiving HandHoverUpdate messages,
|
||||
// and prevent the hand from hovering over anything else
|
||||
hand.HoverLock(interactable);
|
||||
|
||||
// Attach this object to the hand
|
||||
hand.AttachObject(gameObject, startingGrabType, attachmentFlags);
|
||||
}
|
||||
else if (isGrabEnding)
|
||||
{
|
||||
// Detach this object from the hand
|
||||
hand.DetachObject(gameObject);
|
||||
|
||||
// Call this to undo HoverLock
|
||||
hand.HoverUnlock(interactable);
|
||||
|
||||
// Restore position/rotation
|
||||
transform.position = oldPosition;
|
||||
transform.rotation = oldRotation;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Called when this GameObject becomes attached to the hand
|
||||
//-------------------------------------------------
|
||||
private void OnAttachedToHand( Hand hand )
|
||||
{
|
||||
generalText.text = string.Format("Attached: {0}", hand.name);
|
||||
attachTime = Time.time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Called when this GameObject is detached from the hand
|
||||
//-------------------------------------------------
|
||||
private void OnDetachedFromHand( Hand hand )
|
||||
{
|
||||
generalText.text = string.Format("Detached: {0}", hand.name);
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Called every Update() while this GameObject is attached to the hand
|
||||
//-------------------------------------------------
|
||||
private void HandAttachedUpdate( Hand hand )
|
||||
{
|
||||
generalText.text = string.Format("Attached: {0} :: Time: {1:F2}", hand.name, (Time.time - attachTime));
|
||||
}
|
||||
|
||||
private bool lastHovering = false;
|
||||
private void Update()
|
||||
{
|
||||
if (interactable.isHovering != lastHovering) //save on the .tostrings a bit
|
||||
{
|
||||
hoveringText.text = string.Format("Hovering: {0}", interactable.isHovering);
|
||||
lastHovering = interactable.isHovering;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Called when this attached GameObject becomes the primary attached object
|
||||
//-------------------------------------------------
|
||||
private void OnHandFocusAcquired( Hand hand )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// Called when another attached GameObject becomes the primary attached object
|
||||
//-------------------------------------------------
|
||||
private void OnHandFocusLost( Hand hand )
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8bb563c4e716d2c4181e187d9be248db
|
||||
timeCreated: 1544852179
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
98
Assets/SteamVR/InteractionSystem/Samples/Scripts/Planting.cs
Normal file
98
Assets/SteamVR/InteractionSystem/Samples/Scripts/Planting.cs
Normal file
@ -0,0 +1,98 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Valve.VR.InteractionSystem;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class Planting : MonoBehaviour
|
||||
{
|
||||
public SteamVR_Action_Boolean plantAction;
|
||||
|
||||
public Hand hand;
|
||||
|
||||
public GameObject prefabToPlant;
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (hand == null)
|
||||
hand = this.GetComponent<Hand>();
|
||||
|
||||
if (plantAction == null)
|
||||
{
|
||||
Debug.LogError("<b>[SteamVR Interaction]</b> No plant action assigned", this);
|
||||
return;
|
||||
}
|
||||
|
||||
plantAction.AddOnChangeListener(OnPlantActionChange, hand.handType);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (plantAction != null)
|
||||
plantAction.RemoveOnChangeListener(OnPlantActionChange, hand.handType);
|
||||
}
|
||||
|
||||
private void OnPlantActionChange(SteamVR_Action_Boolean actionIn, SteamVR_Input_Sources inputSource, bool newValue)
|
||||
{
|
||||
if (newValue)
|
||||
{
|
||||
Plant();
|
||||
}
|
||||
}
|
||||
|
||||
public void Plant()
|
||||
{
|
||||
StartCoroutine(DoPlant());
|
||||
}
|
||||
|
||||
private IEnumerator DoPlant()
|
||||
{
|
||||
Vector3 plantPosition;
|
||||
|
||||
RaycastHit hitInfo;
|
||||
bool hit = Physics.Raycast(hand.transform.position, Vector3.down, out hitInfo);
|
||||
if (hit)
|
||||
{
|
||||
plantPosition = hitInfo.point + (Vector3.up * 0.05f);
|
||||
}
|
||||
else
|
||||
{
|
||||
plantPosition = hand.transform.position;
|
||||
plantPosition.y = Player.instance.transform.position.y;
|
||||
}
|
||||
|
||||
GameObject planting = GameObject.Instantiate<GameObject>(prefabToPlant);
|
||||
planting.transform.position = plantPosition;
|
||||
planting.transform.rotation = Quaternion.Euler(0, Random.value * 360f, 0);
|
||||
|
||||
planting.GetComponentInChildren<MeshRenderer>().material.SetColor("_TintColor", Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f));
|
||||
|
||||
Rigidbody rigidbody = planting.GetComponent<Rigidbody>();
|
||||
if (rigidbody != null)
|
||||
rigidbody.isKinematic = true;
|
||||
|
||||
|
||||
|
||||
Vector3 initialScale = Vector3.one * 0.01f;
|
||||
Vector3 targetScale = Vector3.one * (1 + (Random.value * 0.25f));
|
||||
|
||||
float startTime = Time.time;
|
||||
float overTime = 0.5f;
|
||||
float endTime = startTime + overTime;
|
||||
|
||||
while (Time.time < endTime)
|
||||
{
|
||||
planting.transform.localScale = Vector3.Slerp(initialScale, targetScale, (Time.time - startTime) / overTime);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
|
||||
if (rigidbody != null)
|
||||
rigidbody.isKinematic = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4390f3b73c731c846a0cc863c72d7a1e
|
||||
timeCreated: 1529518097
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- plantAction: {instanceID: 0}
|
||||
- hand: {instanceID: 0}
|
||||
- prefabToPlant: {fileID: 1000011210241306, guid: 8a2487c189a464148aadc3af2effa7c8,
|
||||
type: 2}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,40 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
#if UNITY_UGUI_UI || !UNITY_2019_2_OR_NEWER
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class RenderModelChangerUI : UIElement
|
||||
{
|
||||
public GameObject leftPrefab;
|
||||
public GameObject rightPrefab;
|
||||
|
||||
protected SkeletonUIOptions ui;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
ui = this.GetComponentInParent<SkeletonUIOptions>();
|
||||
}
|
||||
|
||||
protected override void OnButtonClick()
|
||||
{
|
||||
base.OnButtonClick();
|
||||
|
||||
if (ui != null)
|
||||
{
|
||||
ui.SetRenderModel(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
using UnityEngine;
|
||||
namespace Valve.VR.InteractionSystem.Sample { public class RenderModelChangerUI : MonoBehaviour {
|
||||
public GameObject leftPrefab;
|
||||
public GameObject rightPrefab;
|
||||
} }
|
||||
#endif
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f05229f3db0d24a4e9df9f11919f341a
|
||||
timeCreated: 1548286473
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,75 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using Valve.VR.InteractionSystem;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class SkeletonUIOptions : MonoBehaviour
|
||||
{
|
||||
|
||||
public void AnimateHandWithController()
|
||||
{
|
||||
for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
|
||||
{
|
||||
Hand hand = Player.instance.hands[handIndex];
|
||||
if (hand != null)
|
||||
{
|
||||
hand.SetSkeletonRangeOfMotion(Valve.VR.EVRSkeletalMotionRange.WithController);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AnimateHandWithoutController()
|
||||
{
|
||||
for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
|
||||
{
|
||||
Hand hand = Player.instance.hands[handIndex];
|
||||
if (hand != null)
|
||||
{
|
||||
hand.SetSkeletonRangeOfMotion(Valve.VR.EVRSkeletalMotionRange.WithoutController);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowController()
|
||||
{
|
||||
for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
|
||||
{
|
||||
Hand hand = Player.instance.hands[handIndex];
|
||||
if (hand != null)
|
||||
{
|
||||
hand.ShowController(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetRenderModel(RenderModelChangerUI prefabs)
|
||||
{
|
||||
for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
|
||||
{
|
||||
Hand hand = Player.instance.hands[handIndex];
|
||||
if (hand != null)
|
||||
{
|
||||
if (hand.handType == SteamVR_Input_Sources.RightHand)
|
||||
hand.SetRenderModel(prefabs.rightPrefab);
|
||||
if (hand.handType == SteamVR_Input_Sources.LeftHand)
|
||||
hand.SetRenderModel(prefabs.leftPrefab);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void HideController()
|
||||
{
|
||||
for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
|
||||
{
|
||||
Hand hand = Player.instance.hands[handIndex];
|
||||
if (hand != null)
|
||||
{
|
||||
hand.HideController(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48c26627b0786bd489db7ad471de1a5e
|
||||
timeCreated: 1529438475
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,69 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class TargetHitEffect : MonoBehaviour
|
||||
{
|
||||
public Collider targetCollider;
|
||||
|
||||
public GameObject spawnObjectOnCollision;
|
||||
|
||||
public bool colorSpawnedObject = true;
|
||||
|
||||
public bool destroyOnTargetCollision = true;
|
||||
|
||||
private void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
if (collision.collider == targetCollider)
|
||||
{
|
||||
ContactPoint contact = collision.contacts[0];
|
||||
RaycastHit hit;
|
||||
|
||||
float backTrackLength = 1f;
|
||||
Ray ray = new Ray(contact.point - (-contact.normal * backTrackLength), -contact.normal);
|
||||
if (collision.collider.Raycast(ray, out hit, 2))
|
||||
{
|
||||
if (colorSpawnedObject)
|
||||
{
|
||||
Renderer renderer = collision.gameObject.GetComponent<Renderer>();
|
||||
Texture2D tex = (Texture2D)renderer.material.mainTexture;
|
||||
Color color = tex.GetPixelBilinear(hit.textureCoord.x, hit.textureCoord.y);
|
||||
|
||||
if (color.r > 0.7f && color.g > 0.7f && color.b < 0.7f)
|
||||
color = Color.yellow;
|
||||
else if (Mathf.Max(color.r, color.g, color.b) == color.r)
|
||||
color = Color.red;
|
||||
else if (Mathf.Max(color.r, color.g, color.b) == color.g)
|
||||
color = Color.green;
|
||||
else
|
||||
color = Color.yellow;
|
||||
|
||||
color *= 15f;
|
||||
|
||||
GameObject spawned = GameObject.Instantiate(spawnObjectOnCollision);
|
||||
spawned.transform.position = contact.point;
|
||||
spawned.transform.forward = ray.direction;
|
||||
|
||||
Renderer[] spawnedRenderers = spawned.GetComponentsInChildren<Renderer>();
|
||||
for (int rendererIndex = 0; rendererIndex < spawnedRenderers.Length; rendererIndex++)
|
||||
{
|
||||
Renderer spawnedRenderer = spawnedRenderers[rendererIndex];
|
||||
spawnedRenderer.material.color = color;
|
||||
if (spawnedRenderer.material.HasProperty("_EmissionColor"))
|
||||
{
|
||||
spawnedRenderer.material.SetColor("_EmissionColor", color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Debug.DrawRay(ray.origin, ray.direction, Color.cyan, 5, true);
|
||||
|
||||
if (destroyOnTargetCollision)
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 037de2161ddbc6f4e96de7dd85ea6686
|
||||
timeCreated: 1528762595
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,58 @@
|
||||
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
|
||||
|
||||
#if UNITY_UGUI_UI || !UNITY_2019_2_OR_NEWER
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Valve.VR.InteractionSystem.Sample
|
||||
{
|
||||
public class TargetMeasurement : MonoBehaviour
|
||||
{
|
||||
public GameObject visualWrapper;
|
||||
public Transform measurementTape;
|
||||
|
||||
public Transform endPoint;
|
||||
public Text measurementTextM;
|
||||
public Text measurementTextFT;
|
||||
|
||||
public float maxDistanceToDraw = 6f;
|
||||
|
||||
public bool drawTape = false;
|
||||
|
||||
private float lastDistance;
|
||||
private void Update()
|
||||
{
|
||||
if (Camera.main != null)
|
||||
{
|
||||
Vector3 fromPoint = Camera.main.transform.position;
|
||||
fromPoint.y = endPoint.position.y;
|
||||
|
||||
float distance = Vector3.Distance(fromPoint, endPoint.position);
|
||||
|
||||
Vector3 center = Vector3.Lerp(fromPoint, endPoint.position, 0.5f);
|
||||
|
||||
this.transform.position = center;
|
||||
this.transform.forward = endPoint.position - fromPoint;
|
||||
measurementTape.localScale = new Vector3(0.05f, distance, 0.05f);
|
||||
|
||||
if (Mathf.Abs(distance - lastDistance) > 0.01f)
|
||||
{
|
||||
measurementTextM.text = distance.ToString("00.0m");
|
||||
measurementTextFT.text = (distance * 3.28084).ToString("00.0ft");
|
||||
|
||||
lastDistance = distance;
|
||||
}
|
||||
|
||||
if (drawTape)
|
||||
visualWrapper.SetActive(distance < maxDistanceToDraw);
|
||||
else
|
||||
visualWrapper.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
using UnityEngine;
|
||||
namespace Valve.VR.InteractionSystem.Sample { public class TargetMeasurement : MonoBehaviour { } }
|
||||
#endif
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ec342cce029750947a79c83ea47c42b9
|
||||
timeCreated: 1528840561
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
#if UNITY_EDITOR && UNITY_URP
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
[ExecuteInEditMode]
|
||||
public class URPMaterialSwitcher : MonoBehaviour
|
||||
{
|
||||
public bool children = false;
|
||||
|
||||
#if UNITY_EDITOR && UNITY_URP
|
||||
|
||||
private const string searchTemplate = "URP{0} t:material";
|
||||
void Start()
|
||||
{
|
||||
Renderer renderer;
|
||||
|
||||
if (children)
|
||||
renderer = this.GetComponentInChildren<Renderer>();
|
||||
else
|
||||
renderer = this.GetComponent<Renderer>();
|
||||
|
||||
if (renderer.sharedMaterial.name.StartsWith("URP") == false)
|
||||
{
|
||||
string[] mats = UnityEditor.AssetDatabase.FindAssets(string.Format(searchTemplate, renderer.sharedMaterial.name));
|
||||
if (mats.Length > 0)
|
||||
{
|
||||
string path = UnityEditor.AssetDatabase.GUIDToAssetPath(mats[0]);
|
||||
|
||||
if (PrefabUtility.IsPartOfPrefabInstance(this))
|
||||
{
|
||||
string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(this);
|
||||
GameObject myPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
|
||||
URPMaterialSwitcher[] switchers = myPrefab.GetComponentsInChildren<URPMaterialSwitcher>(true);
|
||||
foreach (var switcher in switchers)
|
||||
{
|
||||
switcher.Execute();
|
||||
}
|
||||
EditorUtility.SetDirty(myPrefab);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
if (children)
|
||||
{
|
||||
Renderer[] renderers = this.GetComponentsInChildren<Renderer>();
|
||||
foreach (var renderer in renderers)
|
||||
SwitchRenderer(renderer);
|
||||
}
|
||||
else
|
||||
{
|
||||
SwitchRenderer(this.GetComponent<Renderer>());
|
||||
}
|
||||
}
|
||||
|
||||
private void SwitchRenderer(Renderer renderer)
|
||||
{
|
||||
if (renderer != null && renderer.sharedMaterial.name.StartsWith("URP") == false)
|
||||
{
|
||||
string[] foundMaterials = UnityEditor.AssetDatabase.FindAssets(string.Format(searchTemplate, renderer.sharedMaterial.name));
|
||||
if (foundMaterials.Length > 0)
|
||||
{
|
||||
string urpMaterialPath = UnityEditor.AssetDatabase.GUIDToAssetPath(foundMaterials[0]);
|
||||
Material urpMaterial = UnityEditor.AssetDatabase.LoadAssetAtPath<Material>(urpMaterialPath);
|
||||
|
||||
SerializedObject serializedRenderer = new SerializedObject(renderer);
|
||||
serializedRenderer.Update();
|
||||
|
||||
SerializedProperty materialProp = serializedRenderer.FindProperty("m_Materials");
|
||||
materialProp.ClearArray();
|
||||
materialProp.InsertArrayElementAtIndex(0);
|
||||
materialProp.GetArrayElementAtIndex(0).objectReferenceValue = urpMaterial;
|
||||
|
||||
serializedRenderer.ApplyModifiedProperties();
|
||||
if (PrefabUtility.IsPartOfPrefabInstance(renderer))
|
||||
{
|
||||
PrefabUtility.RecordPrefabInstancePropertyModifications(renderer);
|
||||
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(this.gameObject.scene);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 54a4af4cc6b48a9449034f15c9691985
|
||||
timeCreated: 1591404676
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user