Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Runtime.InteropServices;
- /// <summary>
- /// Override the texture of the Vive controllers, with your own texture, after SteamVR has loaded and applied the original texture.
- /// </summary>
- public class OverrideControllerTexture : MonoBehaviour
- {
- #region Public variables
- [Header("Variables")]
- public Texture2D newBodyTexture; //The new texture.
- #endregion
- void OnEnable ()
- {
- //Subscribe to the event that is called by SteamVR_RenderModel, when the controller mesh + texture, has been loaded completely.
- SteamVR_Utils.Event.Listen("render_model_loaded", OnControllerLoaded);
- }
- void OnDisable ()
- {
- //Unsubscribe to the event if this object is disabled.
- SteamVR_Utils.Event.Remove("render_model_loaded", OnControllerLoaded);
- }
- /// <summary>
- /// Override the texture of each of the parts, with your texture.
- /// </summary>
- /// <param name="newTexture">Override texture</param>
- /// <param name="modelTransform">Transform of the gameobject, which has the SteamVR_RenderModel component.</param>
- public void UpdateControllerTexture(Texture2D newTexture, Transform modelTransform)
- {
- modelTransform.Find("body").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("button").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("led").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("lgrip").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("rgrip").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("scroll_wheel").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("sys_button").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("trackpad").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("trackpad_scroll_cut").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("trackpad_touch").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- modelTransform.Find("trigger").GetComponent<MeshRenderer>().material.mainTexture = newTexture;
- }
- /// <summary>
- /// Call this method, when the "render_model_loaded" event is triggered.
- /// </summary>
- /// <param name="args">bool success, SteamVR_RenderModel model</param>
- void OnControllerLoaded(params object[] args)
- {
- if ((bool)args[1])
- {
- SteamVR_RenderModel controllerRenderModel = (SteamVR_RenderModel)args[0];
- UpdateControllerTexture(newBodyTexture, controllerRenderModel.gameObject.transform);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement