Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

MRTK Add ManipulationHandler in C#

Discussion in 'Scripting' started by epratt, Apr 3, 2020.

  1. epratt

    epratt

    Joined:
    Oct 15, 2019
    Posts:
    16
    I'm attempting to dynamically add ManipulationHandler to child objects of a parent. The parent will be the the object the user is inspecting, but the user will be able to grap parts off of the parent and inspect them more closely. (i.e. you can look at an engine, but if you want to inspect the pistons you can grab them and look at them)

    Instead of having to go into every child object and manually add it in Unity I'd like to be able to add the parent object and just procedurally add the ManipulationHandler on start or awake.

    So far I have the following code, but I'm not sure how to set up the pointers so I use the script and function I want from the source:

    Code (CSharp):
    1. item.AddComponent<ManipulationHandler>();
    2. ManipulationHandler handler = item.GetComponent<ManipulationHandler>();
    3. ManipulationEvent newevent = new ManipulationEvent();
    4. ManipulationEventData eventdata = new ManipulationEventData();
    5. eventdata.ManipulationSource = item;
    6.  
    I see there's a getter and setter for Pointer in ManipulationEventData, but I'm not sure how to instantiate IMixedRealityPointer and how to get it to work.

    I apologize in advance if I've missed something obvious. I'm new to MRTK.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    I've not used any of the MRTK stuff, but if you have the top of a hierarchy of objects, you can find all sub-objects with:

    Code (csharp):
    1. var AllTransforms = topObject.GetComponentsInChildren<Transform>();
    From there you can iterate all the transforms and decide if you want to
    AddComponent<>()
    to them. Note: you have to reach from the Transform to the GameObject to get at AddComponent<>();

    Code (csharp):
    1. foreach( var tr in AllTransforms)
    2. {
    3.   tr.gameObject.AddComponent<MyNewScriptToAdd>();
    4. }
     
  3. epratt

    epratt

    Joined:
    Oct 15, 2019
    Posts:
    16
    Yes, looping through child objects is not what I'm struggling with. I can loop through just fine. The MRTK script provided has a public variable of type "IMixedRealityPointer" called "Pointer", but it's an interface. So I'm not sure what to call to get the instance of it and how I would point to the script of the source it's pointing to.
     
  4. epratt

    epratt

    Joined:
    Oct 15, 2019
    Posts:
    16
    I realize my question is more geared toward specifics of the MRTK API and don't have much to do with Unity itself.
    I've re-posted my question on Stack Overflow here.

    Thanks!
     
    Kurt-Dekker likes this.