Advertisement
Mr_FJ

Vive controller texture override

May 30th, 2016
3,638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3.  
  4. /// <summary>
  5. /// Override the texture of the Vive controllers, with your own texture, after SteamVR has loaded and applied the original texture.
  6. /// </summary>
  7. public class OverrideControllerTexture : MonoBehaviour
  8. {
  9.     #region Public variables
  10.     [Header("Variables")]
  11.     public Texture2D newBodyTexture; //The new texture.
  12.     #endregion
  13.     void OnEnable ()
  14.     {
  15.         //Subscribe to the event that is called by SteamVR_RenderModel, when the controller mesh + texture, has been loaded completely.
  16.         SteamVR_Utils.Event.Listen("render_model_loaded", OnControllerLoaded);
  17.     }
  18.     void OnDisable ()
  19.     {
  20.         //Unsubscribe to the event if this object is disabled.
  21.         SteamVR_Utils.Event.Remove("render_model_loaded", OnControllerLoaded);
  22.     }
  23.  
  24.     /// <summary>
  25.     /// Override the texture of each of the parts, with your texture.
  26.     /// </summary>
  27.     /// <param name="newTexture">Override texture</param>
  28.     /// <param name="modelTransform">Transform of the gameobject, which has the SteamVR_RenderModel component.</param>
  29.     public void UpdateControllerTexture(Texture2D newTexture, Transform modelTransform)
  30.     {
  31.         modelTransform.Find("body").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  32.         modelTransform.Find("button").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  33.         modelTransform.Find("led").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  34.         modelTransform.Find("lgrip").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  35.         modelTransform.Find("rgrip").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  36.         modelTransform.Find("scroll_wheel").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  37.         modelTransform.Find("sys_button").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  38.         modelTransform.Find("trackpad").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  39.         modelTransform.Find("trackpad_scroll_cut").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  40.         modelTransform.Find("trackpad_touch").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  41.         modelTransform.Find("trigger").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
  42.     }
  43.  
  44.     /// <summary>
  45.     /// Call this method, when the "render_model_loaded" event is triggered.
  46.     /// </summary>
  47.     /// <param name="args">bool success, SteamVR_RenderModel model</param>
  48.     void OnControllerLoaded(params object[] args)
  49.     {
  50.         if ((bool)args[1])
  51.         {
  52.             SteamVR_RenderModel controllerRenderModel = (SteamVR_RenderModel)args[0];
  53.             UpdateControllerTexture(newBodyTexture, controllerRenderModel.gameObject.transform);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement