public class ToggleCustomHands : MonoBehaviour
{
public VRTK_ControllerEvents leftController;
public VRTK_ControllerEvents rightController;
public GameObject leftHandAvatar;
public GameObject rightHandAvatar;
protected bool state;
protected virtual void OnEnable()
{
state = false;
//注册手柄菜单按下事件
if (leftController != null)
{
leftController.ButtonTwoPressed += ToggleHands;
}
if (rightController != null)
{
rightController.ButtonTwoPressed += ToggleHands;
}
ToggleVisibility();
}
protected virtual void OnDisable()
{
if (leftController != null)
{
leftController.ButtonTwoPressed -= ToggleHands;
}
if (rightController != null)
{
rightController.ButtonTwoPressed -= ToggleHands;
}
}
protected virtual void ToggleHands(object sender, ControllerInteractionEventArgs e)
{
state = !state;
ToggleVisibility();
}
protected virtual void ToggleVisibility()
{
ToggleAvatarVisibility();//让左右手两个模型显示或隐藏
ToggleSDKVisibility();//自带的手柄显示或隐藏
ToggleScriptAlias();
}
protected virtual void ToggleAvatarVisibility()
{
if (leftHandAvatar != null)
{
leftHandAvatar.SetActive(state);
}
if (rightHandAvatar != null)
{
rightHandAvatar.SetActive(state);
}
}
protected virtual void ToggleSDKVisibility()
{
VRTK_SDKSetup sdkType = VRTK_SDKManager.GetLoadedSDKSetup();//获取当前vrtk使用的是哪种类型的sdk 例如当前使用的是steamvr
if (sdkType != null)
{
//获取的是当前手柄 bool参数 如果是true 返回的是当前手柄实际物体 为fasle的话返回的是leftcontrollerscript物体
VRTK_ControllerReference leftController = VRTK_ControllerReference.GetControllerReference(VRTK_DeviceFinder.GetControllerLeftHand(true));
VRTK_ControllerReference rightController = VRTK_ControllerReference.GetControllerReference(VRTK_DeviceFinder.GetControllerRightHand(true));
switch (sdkType.name)
{
case "SteamVR":
ToggleControllerRenderer(leftController.actual, "Model");
ToggleControllerRenderer(rightController.actual, "Model");
break;
case "Oculus":
ToggleControllerRenderer(leftController.model);
ToggleControllerRenderer(rightController.model);
break;
case "WindowsMR":
ToggleControllerRenderer(leftController.model, "glTFController");
ToggleControllerRenderer(rightController.model, "glTFController");
break;
}
}
}
protected virtual void ToggleControllerRenderer(GameObject controller, string findPath = "")
{
if (controller != null)
{
if (findPath == "")
{
controller.SetActive(!state);
}
else
{
Transform childModel = controller.transform.Find(findPath);
if (childModel != null)
{
childModel.gameObject.SetActive(!state);
}
}
}
}
protected virtual void ToggleScriptAlias()
{
GameObject scriptLeft = VRTK_DeviceFinder.GetControllerLeftHand(false);
GameObject scriptRight = VRTK_DeviceFinder.GetControllerRightHand(false);
CycleScriptAlias(scriptLeft, leftHandAvatar);
CycleScriptAlias(scriptRight, rightHandAvatar);
}
protected virtual void CycleScriptAlias(GameObject controller, GameObject avatar)
{
if (controller != null)
{
VRTK_InteractTouch touch = controller.GetComponentInChildren<VRTK_InteractTouch>();
VRTK_InteractGrab grab = controller.GetComponentInChildren<VRTK_InteractGrab>();
touch.enabled = false;
grab.enabled = false;
touch.customColliderContainer = null;//分别设置 碰撞集 和抓取组件需要的刚体组件 这里线设置为空 在后面进行赋值
grab.ForceControllerAttachPoint(null);
if (avatar != null && state)
{//分别设置 碰撞集 和抓取组件需要的刚体组件点
touch.customColliderContainer = avatar.transform.Find("HandColliders").gameObject;
grab.ForceControllerAttachPoint(avatar.transform.Find("GrabAttachPoint").GetComponent<Rigidbody>());
}
touch.enabled = true;
grab.enabled = true;
}
}
}
代码切换手柄模型和自定义模型
最新推荐文章于 2023-12-14 17:19:01 发布