Switch object show text

how to do if switch object what to Show text with body-end.html

Could you explain in more detail what’ you’d like to achieve? Would you like to display the name of the selected object when cycling through the Switch Object extension?

YES That’s right!

It’s not a documented feature but you can track each object switch event with:

viewer.onApiUserStateChanged(function(key, value) {
});

For state changes triggered by the Switch Objects extension, the key in the callback is SwitchObjects:EXTENSION_NAME, and value is the index of the object in the list of switchable objects defined for your extension (0 for the first object, 1 for the second object, …).

Sorry, I don’t quite understand. Could you please describe the steps in more detail?

Sorry, I don’t quite understand. Could you please describe the steps in more detail?

The following HTML snippet allows you to display the index of the currently displayed object in the list of switchable objects defined for your extension <MY-EXTENSION>.


<div id="current-object">Object #<span id="current-object-index">1</span></div>

<style>
  #current-object {
    position: fixed;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 32px;
    color: #fff;
    text-shadow: 0 1px 4px rgba(0,0,0,0.8);
  }
</style>

<script>
  const viewer = WALK.getViewer();
  const index = document.getElementById('current-object-index');

  viewer.onApiUserStateChanged(function (key, value) {
    if (key !== 'SwitchObjects:<MY-EXTENSION>') return;
    index.textContent = value + 1;
  });
</script>

If you want to display the name you need to map the index to the object name using a hard-coded lookup table in your script - there’s no way to get the name through the API.