zoukankan      html  css  js  c++  java
  • UGUI Set Anchor And Pivot

    我的环境

    Unity 5.3.7p4

    在运行时动态的设置UI元素的锚点和中心点。

    设置坐标

    对于UI上的元素,使用anchorPosition,而不是localpostion,因为Recttransform可以设置锚点。

    设置Anchor

    修改offsetMax不生效

    使用下面这段代码设置Anchor并不会生效,尽管他和你在属性面板看到的值是一样的。

    retRoot.offsetMin = Vector2(0,0)
    retRoot.offsetMax = Vector2(0,0) 
    

    SetInsetAndSizeFromParentEdge

    使用SetInsetAndSizeFromParentEdge函数来进行设定。此函数不受锚点和中心的影响,其中第一个参数代表对齐方式,第二个参数为距离边界的距离,第三个参数为宽度或高度。

    示例:

    ---设置为左上角
    retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, width)
    retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, height)
    

    修改Anchor不会影响Pivot

    修改Anchor之后,并不会影响Pivot

    设置Pivot(中心点)

    pivot是一个0~1之间的值 示例:retRoot.pivot = Vector2(0, 0)

    其中0,0为左下角

    当你要做动画,设置父容器的Pivot就可以控制动画的出现方向

    查看Pivot

    在Editor的工具栏将Pivot的模式设置为这个模式(Pivot Local),才能查看到正确的Pivot。

    示例代码

    设置左上、左下、右上、右下四个锚点,并同时设置中心点。

    
    ---@param retRoot UnityEngine.RectTransform
    function TipsHelper.SetTipsAnchor(retRoot, anchor, width, height)
        if not retRoot then
            print("[error] SetAnchor失败,RectTransform不能为空")
            return
        end
        if anchor == Constants.Anchor.LeftTop then
            retRoot.pivot = Vector2(0, 1)
            retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, width)
            retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, height)
        elseif anchor == Constants.Anchor.LeftBottom then
            retRoot.pivot = Vector2(0, 0)
            retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, width)
            retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, height)
        elseif anchor == Constants.Anchor.RightTop then
            retRoot.pivot = Vector2(1, 1)
            retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, width)
            retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, height)
        elseif anchor == Constants.Anchor.RightBottom then
            retRoot.pivot = Vector2(1, 0)
            retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, width)
            retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, height)
        end
    end
    

    参考资料

    参考:http://www.arkaistudio.com/blog/334/unity/unity-ugui-原理篇三:recttransform

  • 相关阅读:
    readystatechange事件
    DOMContentLoaded事件
    beforeunload事件
    jieba
    模型评估
    机器学习术语
    决策树
    kafka
    即时通讯好文
    HTTP头的Expires与Cache-control
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/8884303.html
Copyright © 2011-2022 走看看