I am creating a map in Unity3D to be displayed in Hololens 2. I am using MRTK to code interactable functionality.
Since the map contains a lot of independent countries, whose respond onClick may vary with time, I decided to add the required components programmatically. For example:
public Transform worlmap;
public Microsoft.MixedReality.Toolkit.UI.Theme mTheme;
public Microsoft.MixedReality.Toolkit.UI.States mStates;
List<Microsoft.MixedReality.Toolkit.UI.Theme> themes;
void Start()
{
themes = new List<Microsoft.MixedReality.Toolkit.UI.Theme>();
if (mTheme)
{
themes.Add(mTheme);
}
// Looping through all the countries in worldMap
foreach (Transform country in worldMap)
{
// Adding the 4 components which make a GameObject interactable in Hololens
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>();
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.UI.PressableButton>();
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.Input.NearInteractionTouchable>();
country.gameObject.AddComponent<MeshCollider>();
// Assigning State
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().States = mStates;
// Assigning the Profiles (Will definine the colors based on the State)
Microsoft.MixedReality.Toolkit.UI.InteractableProfileItem mProfile = new Microsoft.MixedReality.Toolkit.UI.InteractableProfileItem();
mProfile.Themes = themes;
mProfile.Target = country.gameObject;
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().Profiles.Add(mProfile);
// Assigning Events
// TODO
}
}
The part below is working, but I am not able to assign Events to the different countries in the same loop.
I can define these events in the Editor, for example:
But since it is a repetitive activity which may slightly change over time, I was trying to achieve the same programmatically inside the loop of the script above.
This is what I tried, but it is not working:
// Creating an Event
Microsoft.MixedReality.Toolkit.UI.InteractableEvent mEvent = new Microsoft.MixedReality.Toolkit.UI.InteractableEvent();
// Assigning the method to be executed during onClick
mEvent.Event.AddListener(country.gameObject.GetComponent<CountrySelected>().printName)
// Assigning the Event to Interactable
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().InteractableEvents.Add(mEvent);
What is the correct way of adding programmatically Events to an Interactable in MRTK?
To assign OnClick Event for the Interactable component at the runtime, please try the following code:
this.GetComponent<Interactable>().OnClick.AddListener(MyFun1);
The Interactable component profile in the Unity Editor may not display the new method at the runtime, but it does work in the scene.
Related
I have an MRTK Slate which comes with Pressable Button to close the slate.
When the close button is pressed, the Slate is disabled but not destroyed. This is because in the Events section>Interactable script> GameObject.SetActive() is set by default as shown in fig:
I want to destroy the slate after clicking close button.
I know I can do this by making a script, attaching the slate prefab to the script(or taking the parent of the button till I get slate as parent game object), calling the function, and using GameObject.Destroy().
But I want to understand how the GameObject dropdown in the Interactable script is getting populated :
I understand that the other dropdowns like Transform, Follow me toggle, Solverhandler, etc are displayed because they are attached to the Slate.
But how does the GameObject option is available? This seems a basic thing, but I would like to be sure about how it is coded.
And if it is possible to add my new action in the Gameobject dropdown, if so, how?
I spent time looking at the scripts of Interactable and others, but I am a beginner in C# and was not able to see where is the code which does this.
Any input will help me.
No you can't because Object.Destroy is static and in UnityEvent (which Interactable.OnClick uses) via the Inspector you can only select instance methods.
You at lest need a minimal component like
public class ObjectDestroyer : MonoBehaviour
{
public void DestroyObject()
{
Destroy(this.gameObject);
}
}
or if you don't want to do it via the Inspector but automatically
public class ObjectDestroyer : MonoBehaviour
{
// store the itneractable reference
[Tootltip("If not provided uses the Interactable on this GameObject")]
[SerialzieField] private Interactable _interactable;
// Optionally provide a different target, otherwise destroys the Interactable
[Tooltip("If not provided destroys the GameObject of the Interactable")]
[SerializeField] private GameObject _targetToDestroy;
private void Awake()
{
// use get component as fallback if no Interactable was provided
if(!_interactable) _interactable = GetComponent<Interactable>();
// attach the callback on runtime (won't appear in the inspector)
_interactable.OnClick.AddListener(DestroyObject);
}
private void DestroyObject()
{
// destroy the object of the interactable if no target was provided
if(!_targetToDestroy) _targetToDestroy = _interactable.gameObject;
Destroy(_targetToDestroy);
}
}
I was checking different unity events for dragging by mouse. I found IDragHandler interface provides a method OnDrag which is called when dragging operation occurred for any UI component(please correct me if I'm wrong).
I found another method 'EventTrigger.OnDrag( PointerEventData data )'
In the documentation, it is written that -
"Called by the EventSystem every time the pointer is moved during dragging"
So, I used this in my unity project prescribed by the documentation below
private void Start()
{
//Fetch the Event Trigger component from your GameObject
trigger = GetComponent<EventTrigger>();
//Create a new entry for the Event Trigger
EventTrigger.Entry entry = new EventTrigger.Entry();
//Add a Drag type event to the Event Trigger
entry.eventID = EventTriggerType.Drag;
//call the OnDragDelegate function when the Event System detects dragging
entry.callback.AddListener((data) => OnDragDelegate((PointerEventData) data));
//Add the trigger entry
trigger.triggers.Add(entry);
}
void OnDragDelegate(PointerEventData data)
{
DragToMotion();
}
so, OnDragDelegate() is supposed to be called when any dragging operation happens. I have a cube with a box collider and I drag my mouse over it. I checked in the inspector but found no method being called while dragging, the operations in DragToMotion() are not executed as well.
Please correct me on what I'm doing wrong. What could be the solution for this?
You need some sort of collider on the object as well as a Physics Raycaster on your camera. I'm fairly confident that the EventTriggers are just the inspector version of the drag/pointer interfaces.
Using either should end in the same result. Instead of accessing the event trigger and adding it in code, you can add the component then set a callback on the object similar to an onclick of a button.
Never used listeners before so I might just be not understanding how the work properly? But what's happening, is I add a listener to a button every time a specific scene is loaded.
Here is a list of events that stop it working:
When I first run the game, the listener works as intended, the buttons are created in the script, and the listener is added.
I change the scene, not destroying the object with the script containing the listener.
When the scene changes back, the script will re-create the buttons, and re-add the listener to the buttons.
Using a simple log to check the method the listener should run, the log will no longer show.
Where the listener is added:
foreach (Sprite texture in spriteImages)
{
//Creates a button
GameObject button = Instantiate (shopButtonPrefab) as GameObject;
// gets the buttons image compnent for changing
Image buttonImage = button.GetComponent<Image> ();
Image[] images = button.GetComponentsInChildren<Image>();
int newIndex = spriteIndex;
button.GetComponent<Button> ().onClick.RemoveAllListeners ();
button.GetComponent<Button> ().onClick.AddListener (() => ChangePlayerSkin (newIndex));
spriteIndex++;
}
The method that should be called.
private void ChangePlayerSkin(int index)
{
selectedSkinIndex = index;
Debug.Log ("noo");
if (skinPurchased [index])
{
currentSkinIndex = index;
foreach (GameObject button in shopButtons)
{
button.transform.GetChild (2).gameObject.SetActive (false);
}
shopButtons [currentSkinIndex].transform.GetChild (2).gameObject.SetActive (true);
}
}
// Stops working after the scene is re-loaded
Only trying to show relevant code. The buttons are created in OnSceneLoaded method every time a certain scene is run. Then the listener is added. Why will the listener only work the first time the scene is loaded? And not every time the scene is reloaded. Does the dontdestroyonload mess up the listener?
Since asking the question, I have further looked around at the rest of the program am cannot find the error to be anywhere else. Also can't find anything in the documentation relating to this? Still can't find a fix.
I have now tried to add the listener in a separate scripts in the awake method of the buttons. Still the listener will not work.
I'm creating custom layout group and I want to control RectTransform on the child Objects. I want to lock some fields on child's RectTransform like when using canvas or Unity's Horizontal or Vertical group so that it cannot be modified.
I need the same effect. You can see this message on top of child's RectTransform : Some values driven by HorizontalLayoutGroup
I find a halfway :
Adding [ExecuteInEditMode] then:
public void Update()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
/* Todo => update child's positions here. */
}
#endif
}
Any other idea?
This is done with the DrivenRectTransformTracker API.
From the doc:
Driving a RectTransform means that the values of the driven
RectTransform are controlled by that component. These driven values
cannot be edited in the Inspector (they are shown as disabled). They
also won't be saved when saving a scene, which prevents undesired
scene file changes.
Whenever the component is changing values of driven RectTransforms, it
should first call the Clear method and then use the Add method to add
all RectTransforms it is driving. The Clear method should also be
called in the OnDisable callback of the component.
No example from the doc but below is how to use it:
public RectTransform targetRC;
UnityEngine.Object driver;
void Start()
{
DrivenRectTransformTracker dt = new DrivenRectTransformTracker();
dt.Clear();
//Object to drive the transform
driver = this;
dt.Add(driver, targetRC, DrivenTransformProperties.All);
}
The RectTransform linked to the targetRC variable will now be locked and cannot be modified from the Editor. It should now say something like "Some values are driven by another object". You can use DrivenTransformProperties to specify which variables to lock.
This is what it looks like after executing this code:
I've been struggling with what appears to be a simple task for the past few hours. I have been successfully able to turn a GameObject's guiTexture on an off using GameObject.guiTexture.enabled as I have read in other similar questions. I do this in a script that is attached to the GameObject I wish to toggle.
The problem arises when I try to preform this same command from another script that is attached to an empty game object in my scene. When I interact with the button, simply nothing happens.
To give this some context, I am trying to create functionality to switch menus on the screen. When a user taps a certain UI button, I want to call unloadMenu() so I can wipe everything off the screen and draw the textures that belong to the appropriate menu.
//When the user lifts their finger
void OnTouchEnded ()
{
switch (this.gameObject.name)
{
//If they have selected the quiz button
case "QuizButton":
//this.guiTexture.enabled = false;
//Call the unloadMenu function which will clear the GUITextures
menuMgr.unloadMenu();
break;
The commented out section is what works fine. But now, I wish to perform this code in a function that is located in another script - menuMgr.unloadMenu()
This is what the MenuManager script looks like:
public GameObject quizButton;
void unloadMenu()
{
quizButton.guiTexture.enabled = false;
}
I have ensured to drag the quizButton gameobject from my scene into the inspector and connect it properly with the variable I have created yet nothing happens when this function is called. Does anyone have any idea why? Thanks in advanced.
How you declare MenuManager creates a new script while the reference to quizButton is in your MenuManager GameObject. That is why your menuMgr.UnloadMenu didn't do anything.
I have created something similar to yours and I found it worked fine :
Quizbutton Script:
public GameObject menuMgr; // reference to the empty game object (menu manager)
void OnMouseDown() // In your case, this is OnTouchEnded
{
// get the script of menu manager here
MenuMgrScript menuscript = menuMgr.GetComponent<MenuMgrScript>();
// and execute the function you want
menuscript.UnloadMenu();
}
Menu Manager Script:
// this script is exactly the same as yours, nothing's wrong here
// oh except add public to function UnloadMenu, so it can be called
// in the quizbutton script
public GameObject quizButton;
public void UnloadMenu() {
quizButton.guiTexture.enabled = false;
}