如何以编程方式延迟或加载A帧场景? [英] How to defer or load an A-Frame scene programmatically?
问题描述
我有一个A帧场景想要放置在页面上,但是我只希望在告诉时才加载它。这意味着,在我发出事件或调用方法之前,它不应呈现或运行。
I have an A-Frame scene that I want to place on a page, but I want it to load only when I tell it to. That means it should not be rendering or running until I emit an event or call a method.
<body>
<!-- Don't play yet. -->
<a-scene>
</a-scene>
</body>
推荐答案
当前没有内置+记录的方式,但稍后将有一个功能。但是,有两种方法可以手动执行此操作:
There is currently no built-in + documented way, but there will be a feature for this later. However, there are a couple ways to do this manually:
节点(每个< a-entity>
继承的节点)将阻止场景加载,直到调用 .load()
事件。
Nodes (which every <a-entity>
inherits from will block the scene from loading until it calls the .load()
event.
在场景中创建一个虚拟节点,然后调用 .load( )
。
Create a dummy node in the scene. and call .load()
when ready.
<a-node id="dummy"></a-node>
document.getElementById('dummy').load();
杠杆资产系统
您可以创建一种资产,直到您告知该资产后才加载它,并将超时设置为有效的无限期(稍后我们将添加一个功能来避免超时)。
Leverage asset system
You can create an asset that will never load until you tell it to, and set the timeout to effectively indefinite (later we will add a feature to not timeout).
<a-scene>
<a-assets timeout="999999999">
<a-asset-item id="trigger" src="NEVERLOADUNTILITELLITTO.whatever"></a-asset-item>
</a-assets>
</a-scene>
然后触发
document.querySelector('#trigger').load();
仅在准备就绪时注入场景
您可以将场景保存在单独的文件,模板中或作为字符串,或者使用具有View概念的框架。仅当准备好将场景渲染时,才将场景注入DOM。这是最有效的方法,但是目前是最不透气的方法。
Inject scene only when ready
You could keep your scene in a separate file, template, or as a string, or using a framework with concept of Views. Only inject the scene into the DOM when you are ready for it to render. This is the most work, but currently the most air-tight method.
sceneEl.appendChild(document.createRange()。createContextualFragment(str) );
此操作将暂停渲染场景。但是,场景可能已经初始化了一些组件并已经渲染了几帧。因此,它不是气密的。
This will pause the scene from rendering. However, the scene will probably have initialized some components and rendered a few frames already. So it is not air-tight.
document.querySelector('a-scene').pause();
这篇关于如何以编程方式延迟或加载A帧场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!