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.
i have two objects name ,one is 皮沙發 another is 布沙發,how to revise this HTML?
The HTML snippet doesn’t reference object names, so doesn’t need to be updated to the two above names. Instead, you need to replace <MY-EXTENSION> with the name of the Switch Objects extension configured in the scene editor. It’s the extension configuration (in the scene editor) where you select which scene objects are switchable. See this video tutorial https://www.youtube.com/watch?v=8EON-gErO0I to learn how to set up the Switch Objects extension.
You’ve misunderstood; what I mean is that I currently have two objects, and when using the "switch object" function, I need the object names to be displayed separately on the screen. How do I configure this?