Just trying to add a scrollview component by entirely creating it in code.
var DisableScroll = pc.createScript('disableScroll');
// initialize code called once per entity
DisableScroll.prototype.initialize = function() {
var scrollview = new pc.ScrollViewComponent();
var viewport = this.entity.findByName("Viewport");
var content = viewport.findByName("Content");
var horScroller = this.entity.findByName("HorizontalScrollbar");
var verScroller = this.entity.findByName("VerticalScrollbar");
scrollview.bounceAmount = 0.1;
scrollview.friction = 0.05;
scrollview.viewportEntity = viewport;
scrollview.contentEntity = content;
scrollview.horizontal = true;
scrollview.horizontalScrollbarEntity = horScroller;
scrollview.vertical = true;
scrollview.verticalScrollbarEntity = verScroller;
scrollview.scrollMode = 1;
scrollview.verticalScrollbarVisibility = 1;
scrollview.horizontalScrollbarVisibility = 1;
this.entity.addComponent(scrollview);
};
// update code called every frame
DisableScroll.prototype.update = function(dt) {
};
// swap method called for script hot-reloading
// inherit your script state here
// DisableScroll.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/
However, I get the error as follows.
Any way to resolve this?
EDITOR - https://playcanvas.com/editor/scene/939271
DevilZ
June 16, 2020, 7:08am
#2
I’d like to know this as well, all the parameters seem to be well defined, what is “schema”?
I just tried it out @yaustar . Same error. My new code is this.
this.entity.addComponent("ScrollViewComponent", {
bounceAmount: 0.1,
friction: 0.05,
viewportEntity: viewport,
contentEntity: content,
horizontal: true,
horizontalScrollbarEntity: horScroller,
vertical: true,
verticalScrollbarEntity: verScroller,
});
Added it just like the tutorial added the model component. Not so familiar with adding components via code, so help would be appreciated.
Recheck the documentation on the list of valid strings please. You are close.
Sorry @yaustar , didn’t quite get that. You mean the “ScrollViewComponent” string?
e.addComponent('scrollview', {});
The documentation covers what strings are used for which components to add.
Bear in mind that the scrollview is a complicated setup and you may be best off creating it in Editor:
Whats the reason for creating the scroll view in code?
1 Like
Thanks a lot @yaustar ! Really helped.
I’ll leave this to @DevilZ to explain.
DevilZ
June 16, 2020, 9:43am
#9
Essentially @yaustar we are developing a drag and drop system for which we need multiple attributes of the scrollbar to be enabled/disabled at certain times. Due to this, it was decided that we’d create it in code, because the subsequent steps become simpler. Also, thanks for your detecting double click project, that was helpful in our drag and drop system!
Maybe this helps:
Hi @Jasont and welcome,
Not sure what’s wrong with your code, but a good idea when doing runtime spawning of entities/objects is to prepare them in editor, keep them disabled. On runtime use that as a template/prefab to create a clone and spawn an exact copy of that editor prepared object.
var newEntity = prefabEntity.clone();
this.entity.addChild(newEntity);
newEntity.enabled = true;
Not sure if that will solve your problem, but it can help to easily test where is the issue. If it’s somethi…
2 Likes
DevilZ
June 16, 2020, 12:08pm
#11
Thanks @Albertos . Will consider it.