Unity 2D将滚动条的值设置为0

时间:2017-07-24 01:20:57

标签: c# unity3d canvas

我只是想在每次创建新文本对象时将scrollview对象的滚动条的值设置为0。这样,最新的对象将始终保持在底部而无需向下滚动(例如,想想视频游戏的聊天)。我的问题是,由于某种原因,程序将值设置为0,然后创建文本对象,因此它将第二个最新对象保留在滚动视图的底部。

我的代码:

//Creates the Text objects
private GameObject GenerateTextObject(string content)
{
    //Creates the Text objects
    GameObject messagePrefab = Instantiate(MessagePrefab);
    Text textComponent = messagePrefab.GetComponent<Text>();

    //Sets the content of the text and parent
    textComponent.text = content;
    messagePrefab.transform.SetParent(ContentPanel.transform, false);

    ScrollbarVertical.value = 0;

    return messagePrefab;
}

在代码中,我在函数末尾设置了值,但在创建对象之前仍然将滚动条的值设置为0(正确地将滚动视图移动到底部)。

https://gyazo.com/897982521f13d7792ec26540490a40c0 在Gyazo图片中,您可以看到它不会一直向下滚动。

我尝试过使用coroutine和waitForEndFrame以及waitforseconds(1),但似乎都没有。

编辑:当加载Unity并向滚动视图发送新消息时,我看到滚动条一直向下,然后真的快速向上移动一点隐藏新文本对象。

2 个答案:

答案 0 :(得分:3)

您可以做的是创建一个新的from bokeh.layouts import column from bokeh.models import CustomJS, ColumnDataSource, Slider from bokeh.plotting import Figure, output_file, show output_file("js_on_change.html") x = [x*0.005 for x in range(0, 200)] y = x source = ColumnDataSource(data=dict(x=x, y=y)) plot = Figure(plot_width=400, plot_height=400) plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6) callback = CustomJS(args=dict(source=source), code=""" var data = source.data; var f = cb_obj.value x = data['x'] y = data['y'] for (i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], f) } source.change.emit(); """) slider = Slider(start=0.1, end=4, value=1, step=.1, title="power") slider.js_on_change('value', callback) layout = column(slider, plot) show(layout) 并按照以下步骤操作:

enter image description here

结果是每秒呼叫Scroll View

GenerateTextObject(Time.time.ToString());

答案 1 :(得分:1)

我还没有找到解决这个问题的正确方法(老实说我不知道​​问题的原因),但我已经为这种情况做了一些解决方法。

为此,我只创建了一个小函数:

public void Testing()
{
    if (testing == true)
    {
        ScrollbarVertical.value = 0;
        testing = false;
    }

}

这将附加到滚动条的内置功能&#34; On Value Changed(Single)&#34;。为了让这个工作,我只是改变了bool&#34;测试&#34;在GenerateTextObject(字符串内容)上创建文本对象后为true。

这个修复非常难看,并且可能会在不久的将来引起更多问题,但就目前而言,这是解决此问题的快速而肮脏的解决方案。