UpdateGeometry() public method

Update the widget and fill its geometry if necessary. Returns whether something was changed.
public UpdateGeometry ( Matrix4x4 &worldToPanel, bool parentMoved, bool generateNormals ) : bool
worldToPanel Matrix4x4
parentMoved bool
generateNormals bool
return bool
示例#1
0
    private void UpdateWidgets()
    {
        bool flag  = false;
        bool flag2 = false;
        bool hasCumulativeClipping = this.hasCumulativeClipping;

        if (!this.cullWhileDragging)
        {
            for (int i = 0; i < UIScrollView.list.size; i++)
            {
                UIScrollView uIScrollView = UIScrollView.list[i];
                if (uIScrollView.panel == this && uIScrollView.isDragging)
                {
                    flag2 = true;
                }
            }
        }
        if (this.mForced != flag2)
        {
            this.mForced  = flag2;
            this.mResized = true;
        }
        int frameCount = Time.frameCount;
        int j          = 0;
        int count      = this.widgets.Count;

        while (j < count)
        {
            UIWidget uIWidget = this.widgets[j];
            if (uIWidget.panel == this && uIWidget.enabled)
            {
                if (uIWidget.UpdateTransform(frameCount) || this.mResized)
                {
                    bool visibleByAlpha = flag2 || uIWidget.CalculateCumulativeAlpha(frameCount) > 0.001f;
                    uIWidget.UpdateVisibility(visibleByAlpha, flag2 || (!hasCumulativeClipping && !uIWidget.hideIfOffScreen) || this.IsVisible(uIWidget));
                }
                if (uIWidget.UpdateGeometry(frameCount))
                {
                    flag = true;
                    if (!this.mRebuild)
                    {
                        if (uIWidget.drawCall != null)
                        {
                            uIWidget.drawCall.isDirty = true;
                        }
                        else
                        {
                            this.FindDrawCall(uIWidget);
                        }
                    }
                }
            }
            j++;
        }
        if (flag && this.onGeometryUpdated != null)
        {
            this.onGeometryUpdated();
        }
        this.mResized = false;
    }
示例#2
0
    // update all widgets
    void UpdateWidgets()
    {
        bool changed = false;

        for (int i = 0, imax = widgets.size; i < imax; ++i)
        {
            UIWidget w = widgets.buffer[i];

            if (w.panel == this && w.enabled)
            {
                int frame = Time.frameCount;

                if (w.UpdateTransform(frame))
                {
                }

                if (w.UpdateGeometry(frame))
                {
                    changed = true;

                    if (!mRebuild)
                    {
                        if (w.drawCall != null)
                        {
                            w.drawCall.isDirty = true;
                        }
                        else
                        {
                            FindDrawCall(w);
                        }
                    }
                }
            }
        }
    }
示例#3
0
    private void LateUpdate()
    {
        this.mUpdateTime = Time.realtimeSinceStartup;
        this.UpdateTransformMatrix();
        if (this.mLayer != this.cachedGameObject.layer)
        {
            this.mLayer = this.mGo.layer;
            UICamera uicamera = UICamera.FindCameraForLayer(this.mLayer);
            this.mCam = ((!(uicamera != null)) ? NGUITools.FindCameraForLayer(this.mLayer) : uicamera.cachedCamera);
            UIPanel.SetChildLayer(this.cachedTransform, this.mLayer);
            int i    = 0;
            int size = this.drawCalls.size;
            while (i < size)
            {
                this.mDrawCalls.buffer[i].gameObject.layer = this.mLayer;
                i++;
            }
        }
        bool forceVisible = !this.cullWhileDragging && (this.clipping == UIDrawCall.Clipping.None || this.mCullTime > this.mUpdateTime);
        int  j            = 0;
        int  size2        = this.mWidgets.size;

        while (j < size2)
        {
            UIWidget uiwidget = this.mWidgets[j];
            if (uiwidget.UpdateGeometry(this, forceVisible) && !this.mChanged.Contains(uiwidget.material))
            {
                this.mChanged.Add(uiwidget.material);
            }
            j++;
        }
        if (this.mChanged.size != 0 && this.onChange != null)
        {
            this.onChange();
        }
        if (this.mDepthChanged)
        {
            this.mDepthChanged = false;
            this.mWidgets.Sort(new Comparison <UIWidget>(UIWidget.CompareFunc));
        }
        int k     = 0;
        int size3 = this.mChanged.size;

        while (k < size3)
        {
            this.Fill(this.mChanged.buffer[k]);
            k++;
        }
        this.UpdateDrawcalls();
        this.mChanged.Clear();
    }
示例#4
0
    private void LateUpdate()
    {
        this.mUpdateTime = Time.realtimeSinceStartup;
        this.UpdateTransformMatrix();
        if (this.mLayer != this.cachedGameObject.layer)
        {
            this.mLayer = this.mGo.layer;
            UICamera camera = UICamera.FindCameraForLayer(this.mLayer);
            this.mCam = (camera == null) ? NGUITools.FindCameraForLayer(this.mLayer) : camera.cachedCamera;
            SetChildLayer(this.cachedTransform, this.mLayer);
            int num  = 0;
            int num2 = this.drawCalls.size;
            while (num < num2)
            {
                this.mDrawCalls.buffer[num].gameObject.layer = this.mLayer;
                num++;
            }
        }
        bool forceVisible = !this.cullWhileDragging ? ((this.clipping == UIDrawCall.Clipping.None) || (this.mCullTime > this.mUpdateTime)) : false;
        int  num3         = 0;
        int  size         = this.mWidgets.size;

        while (num3 < size)
        {
            UIWidget widget = this.mWidgets[num3];
            if (widget.UpdateGeometry(this, forceVisible) && !this.mChanged.Contains(widget.material))
            {
                this.mChanged.Add(widget.material);
            }
            num3++;
        }
        if ((this.mChanged.size != 0) && (this.onChange != null))
        {
            this.onChange();
        }
        if (this.mDepthChanged)
        {
            this.mDepthChanged = false;
            this.mWidgets.Sort(new Comparison <UIWidget>(UIWidget.CompareFunc));
        }
        int index = 0;
        int num6  = this.mChanged.size;

        while (index < num6)
        {
            this.Fill(this.mChanged.buffer[index]);
            index++;
        }
        this.UpdateDrawcalls();
        this.mChanged.Clear();
    }
示例#5
0
    private void UpdateWidgets()
    {
        bool flag  = !this.cullWhileDragging && this.mCullTime > this.mUpdateTime;
        bool flag2 = false;

        if (this.mForced != flag)
        {
            this.mForced  = flag;
            this.mResized = true;
        }
        bool clipsChildren = this.clipsChildren;
        int  i             = 0;
        int  size          = this.widgets.size;

        while (i < size)
        {
            UIWidget uIWidget = this.widgets.buffer[i];
            if (uIWidget.panel == this && uIWidget.enabled)
            {
                int frameCount = Time.frameCount;
                if (uIWidget.UpdateTransform(frameCount) || this.mResized)
                {
                    bool visibleByAlpha = flag || uIWidget.CalculateCumulativeAlpha(frameCount) > 0.001f;
                    uIWidget.UpdateVisibility(visibleByAlpha, flag || (!clipsChildren && !uIWidget.hideIfOffScreen) || this.IsVisible(uIWidget));
                }
                if (uIWidget.UpdateGeometry(frameCount))
                {
                    flag2 = true;
                    if (!this.mRebuild)
                    {
                        if (uIWidget.drawCall != null)
                        {
                            uIWidget.drawCall.isDirty = true;
                        }
                        else
                        {
                            this.FindDrawCall(uIWidget);
                        }
                    }
                }
            }
            i++;
        }
        if (flag2 && this.onGeometryUpdated != null)
        {
            this.onGeometryUpdated();
        }
        this.mResized = false;
    }
示例#6
0
    private void UpdateWidgets()
    {
        bool flag  = !cullWhileDragging && mCullTime > mUpdateTime;
        bool flag2 = false;

        if (mForced != flag)
        {
            mForced  = flag;
            mResized = true;
        }
        bool hasCumulativeClipping = this.hasCumulativeClipping;
        int  i = 0;

        for (int count = widgets.Count; i < count; i++)
        {
            UIWidget uIWidget = widgets[i];
            if (!(uIWidget.panel == this) || !uIWidget.enabled)
            {
                continue;
            }
            int frameCount = Time.frameCount;
            if (uIWidget.UpdateTransform(frameCount) || mResized)
            {
                bool visibleByAlpha = flag || uIWidget.CalculateCumulativeAlpha(frameCount) > 0.001f;
                uIWidget.UpdateVisibility(visibleByAlpha, flag || (!hasCumulativeClipping && !uIWidget.hideIfOffScreen) || IsVisible(uIWidget));
            }
            if (!uIWidget.UpdateGeometry(frameCount))
            {
                continue;
            }
            flag2 = true;
            if (!mRebuild)
            {
                if (uIWidget.drawCall != null)
                {
                    uIWidget.drawCall.isDirty = true;
                }
                else
                {
                    FindDrawCall(uIWidget);
                }
            }
        }
        if (flag2 && onGeometryUpdated != null)
        {
            onGeometryUpdated();
        }
        mResized = false;
    }
示例#7
0
    /// <summary>
    /// Update all widgets and rebuild their geometry if necessary.
    /// </summary>

    public void UpdateWidgets()
    {
        var child = mWidgetDicts.Values.GetEnumerator();

        while (child.MoveNext() == true)
        {
            UIWidget w = child.Current;

            // If the widget is visible, update it
            if (w.visibleFlag == 1 && w != null && w.UpdateGeometry(ref mWorldToLocal, (w.changeFlag == 1)))
            {
                mtlChanged = true;
            }
        }
    }
示例#8
0
 static public int UpdateGeometry(IntPtr l)
 {
     try {
         UIWidget     self = (UIWidget)checkSelf(l);
         System.Int32 a1;
         checkType(l, 2, out a1);
         var ret = self.UpdateGeometry(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
示例#9
0
	static int UpdateGeometry(IntPtr L)
	{
		try
		{
			ToLua.CheckArgsCount(L, 2);
			UIWidget obj = (UIWidget)ToLua.CheckObject<UIWidget>(L, 1);
			int arg0 = (int)LuaDLL.luaL_checknumber(L, 2);
			bool o = obj.UpdateGeometry(arg0);
			LuaDLL.lua_pushboolean(L, o);
			return 1;
		}
		catch (Exception e)
		{
			return LuaDLL.toluaL_exception(L, e);
		}
	}
示例#10
0
    private void LateUpdate()
    {
        mUpdateTime = Time.realtimeSinceStartup;
        UpdateTransformMatrix();
        if (mLayer != cachedGameObject.layer)
        {
            mLayer = mGo.layer;
            UICamera uICamera = UICamera.FindCameraForLayer(mLayer);
            mCam = uICamera == null?NGUITools.FindCameraForLayer(mLayer) : uICamera.cachedCamera;

            SetChildLayer(cachedTransform, mLayer);
            int i = 0;
            for (int size = drawCalls.size; i < size; i++)
            {
                mDrawCalls.buffer[i].gameObject.layer = mLayer;
            }
        }
        bool forceVisible = !cullWhileDragging && (clipping == UIDrawCall.Clipping.None || mCullTime > mUpdateTime);
        int  j            = 0;

        for (int size2 = mWidgets.size; j < size2; j++)
        {
            UIWidget uIWidget = mWidgets[j];
            if (uIWidget.UpdateGeometry(this, forceVisible) && !mChanged.Contains(uIWidget.material))
            {
                mChanged.Add(uIWidget.material);
            }
        }
        if (mChanged.size != 0 && onChange != null)
        {
            onChange();
        }
        if (mDepthChanged)
        {
            mDepthChanged = false;
            mWidgets.Sort(UIWidget.CompareFunc);
        }
        int k = 0;

        for (int size3 = mChanged.size; k < size3; k++)
        {
            Fill(mChanged.buffer[k]);
        }
        UpdateDrawcalls();
        mChanged.Clear();
    }
示例#11
0
    private void UpdateWidgets()
    {
        int num   = 0;
        int count = this.mChildren.Count;

        while (num < count)
        {
            UINode   item     = (UINode)this.mChildren[num];
            UIWidget uIWidget = item.widget;
            if (item.visibleFlag == 1 && uIWidget != null && uIWidget.UpdateGeometry(ref this.mWorldToLocal, item.changeFlag == 1, this.generateNormals) && !this.mChanged.Contains(uIWidget.material))
            {
                this.mChanged.Add(uIWidget.material);
                this.mChangedLastFrame = true;
            }
            item.changeFlag = 0;
            num++;
        }
    }
示例#12
0
    /// <summary>
    /// Update all widgets and rebuild their geometry if necessary.
    /// </summary>

    void UpdateWidgets()
    {
        foreach (KeyValuePair <Transform, UINode> c in mChildren)
        {
            UINode   pc = c.Value;
            UIWidget w  = pc.widget;

            // If the widget is visible, update it
            if (pc.visibleFlag == 1 && w != null && w.UpdateGeometry(ref mWorldToLocal, (pc.changeFlag == 1), generateNormals))
            {
                // We will need to refill this buffer
                if (!mChanged.Contains(w.material))
                {
                    mChanged.Add(w.material);
                }
            }
        }
    }
示例#13
0
    private void UpdateWidgets()
    {
        int num   = 0;
        int count = this.mChildren.Count;

        while (num < count)
        {
            UINode   node   = (UINode)this.mChildren[num];
            UIWidget widget = node.widget;
            if (((node.visibleFlag == 1) && (widget != null)) && (widget.UpdateGeometry(ref this.mWorldToLocal, node.changeFlag == 1, this.generateNormals) && !this.mChanged.Contains(widget.material)))
            {
                this.mChanged.Add(widget.material);
                this.mChangedLastFrame = true;
            }
            node.changeFlag = 0;
            num++;
        }
    }
示例#14
0
    /// <summary>
    /// Update all of the widgets belonging to this panel.
    /// </summary>

    void UpdateWidgets()
    {
#if UNITY_EDITOR
        bool forceVisible = cullWhileDragging ? false : (clipping == UIDrawCall.Clipping.None) || (Application.isPlaying && mCullTime > mUpdateTime);
#else
        bool forceVisible = cullWhileDragging ? false : (clipping == UIDrawCall.Clipping.None) || (mCullTime > mUpdateTime);
#endif
        bool changed = false;

        // Update all widgets
        for (int i = 0, imax = UIWidget.list.size; i < imax; ++i)
        {
            UIWidget w = UIWidget.list[i];

            // If the widget is visible, update it
            if (w.enabled && w.panel == this && w.UpdateGeometry(this, forceVisible))
            {
                changed = true;
                if (mFullRebuild)
                {
                    continue;
                }
                int index = w.renderQueue;

                if (index < 0)
                {
                    mFullRebuild = true;
                }
                else if (index < UIDrawCall.list.size && UIDrawCall.list[index] != null)
                {
                    UIDrawCall.list[index].isDirty = true;
                }
            }
        }

        // Inform the changed event listeners
        if (changed && onChange != null)
        {
            onChange();
        }
    }
示例#15
0
    private void UpdateWidgets()
    {
        Boolean flag  = false;
        Boolean flag2 = false;
        Boolean hasCumulativeClipping = this.hasCumulativeClipping;

        if (!this.cullWhileDragging)
        {
            for (Int32 i = 0; i < UIScrollView.list.size; i++)
            {
                UIScrollView uiscrollView = UIScrollView.list[i];
                if (uiscrollView.panel == this && uiscrollView.isDragging)
                {
                    flag2 = true;
                }
            }
        }
        if (this.mForced != flag2)
        {
            this.mForced  = flag2;
            this.mResized = true;
        }
        Int32 frameCount = Time.frameCount;
        Int32 j          = 0;
        Int32 count      = this.widgets.Count;

        while (j < count)
        {
            UIWidget uiwidget = (UIWidget)null;
            try
            {
                uiwidget = this.widgets[j];
            }
            catch
            {
                goto IL_183;
            }
            goto IL_B6;
IL_183:
            j++;
            continue;
IL_B6:
            if (!(uiwidget.panel == this) || !uiwidget.enabled)
            {
                goto IL_183;
            }
            if (uiwidget.UpdateTransform(frameCount) || this.mResized)
            {
                Boolean visibleByAlpha = flag2 || uiwidget.CalculateCumulativeAlpha(frameCount) > 0.001f;
                uiwidget.UpdateVisibility(visibleByAlpha, flag2 || (!hasCumulativeClipping && !uiwidget.hideIfOffScreen) || this.IsVisible(uiwidget));
            }
            if (!uiwidget.UpdateGeometry(frameCount))
            {
                goto IL_183;
            }
            flag = true;
            if (this.mRebuild)
            {
                goto IL_183;
            }
            if (uiwidget.drawCall != (UnityEngine.Object)null)
            {
                uiwidget.drawCall.isDirty = true;
                goto IL_183;
            }
            this.FindDrawCall(uiwidget);
            goto IL_183;
        }
        if (flag && this.onGeometryUpdated != null)
        {
            this.onGeometryUpdated();
        }
        this.mResized = false;
    }
示例#16
0
    /// <summary>
    /// Convenience function that figures out the panel's correct change flag by searching the parents.
    /// </summary>

    int GetChangeFlag(UINode start)
    {
        int flag = start.changeFlag;

        if (flag == -1)
        {
            Transform trans = start.trans.parent;
            UINode    sub;

            // Keep going until we find a set flag
            for (;;)
            {
                // Check the parent's flag
#if USE_SIMPLE_DICTIONARY
                if (trans != null && mChildren.TryGetValue(trans, out sub))
                {
#else
                if (trans != null && mChildren.Contains(trans))
                {
                    sub = (UINode)mChildren[trans];
#endif
                    flag  = sub.changeFlag;
                    trans = trans.parent;

                    // If the flag hasn't been set either, add this child to the hierarchy
                    if (flag == -1)
                    {
                        mHierarchy.Add(sub);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    flag = 0;
                    break;
                }
            }

            // Update the parent flags
            for (int i = 0, imax = mHierarchy.size; i < imax; ++i)
            {
                UINode pc = mHierarchy.buffer[i];
                pc.changeFlag = flag;
            }
            mHierarchy.Clear();
        }
        return(flag);
    }

    /// <summary>
    /// Update the world-to-local transform matrix as well as clipping bounds.
    /// </summary>

    void UpdateTransformMatrix()
    {
        if (mUpdateTime == 0f || mMatrixTime != mUpdateTime)
        {
            mMatrixTime   = mUpdateTime;
            mWorldToLocal = cachedTransform.worldToLocalMatrix;

            if (mClipping != UIDrawCall.Clipping.None)
            {
                Vector2 size = new Vector2(mClipRange.z, mClipRange.w);

                if (size.x == 0f)
                {
                    size.x = (mCam == null) ? Screen.width  : mCam.pixelWidth;
                }
                if (size.y == 0f)
                {
                    size.y = (mCam == null) ? Screen.height : mCam.pixelHeight;
                }

                size *= 0.5f;

                mMin.x = mClipRange.x - size.x;
                mMin.y = mClipRange.y - size.y;
                mMax.x = mClipRange.x + size.x;
                mMax.y = mClipRange.y + size.y;
            }
        }
    }

    /// <summary>
    /// Run through all managed transforms and see if they've changed.
    /// </summary>

    void UpdateTransforms()
    {
        mChangedLastFrame = false;
        bool transformsChanged = false;
        bool shouldCull        = false;

#if UNITY_EDITOR
        shouldCull = (clipping != UIDrawCall.Clipping.None) && (!Application.isPlaying || mUpdateTime > mCullTime);
        if (!Application.isPlaying || !widgetsAreStatic || mWidgetsAdded || shouldCull != mCulled)
#else
        shouldCull = (clipping != UIDrawCall.Clipping.None) && (mUpdateTime > mCullTime);
        if (!widgetsAreStatic || mWidgetsAdded || shouldCull != mCulled)
#endif
        {
#if USE_SIMPLE_DICTIONARY
            foreach (KeyValuePair <Transform, UINode> child in mChildren)
            {
                UINode node = child.Value;
#else
            for (int i = 0, imax = mChildren.Count; i < imax; ++i)
            {
                UINode node = (UINode)mChildren[i];
#endif
                if (node.trans == null)
                {
                    mRemoved.Add(node.trans);
                    continue;
                }

                if (node.HasChanged())
                {
                    node.changeFlag   = 1;
                    transformsChanged = true;
#if UNITY_EDITOR
                    Vector3 s   = node.trans.lossyScale;
                    float   min = Mathf.Abs(Mathf.Min(s.x, s.y));

                    if (min == 0f)
                    {
                        Debug.LogError("Scale of 0 is invalid! Zero cannot be divided by, which causes problems. Use a small value instead, such as 0.01\n" +
                                       node.trans.lossyScale, node.trans);
                    }
#endif
                }
                else
                {
                    node.changeFlag = -1;
                }
            }

            // Clean up the deleted transforms
            for (int i = 0, imax = mRemoved.Count; i < imax; ++i)
            {
                mChildren.Remove(mRemoved[i]);
            }
            mRemoved.Clear();
        }

        // If the children weren't culled but should be, check their visibility
        if (!mCulled && shouldCull)
        {
            mCheckVisibility = true;
        }

        // If something has changed, propagate the changes *down* the tree hierarchy (to children).
        // An alternative (but slower) approach would be to do a pc.trans.GetComponentsInChildren<UIWidget>()
        // in the loop above, and mark each one as dirty.

        if (mCheckVisibility || transformsChanged || mRebuildAll)
        {
#if USE_SIMPLE_DICTIONARY
            foreach (KeyValuePair <Transform, UINode> child in mChildren)
            {
                UINode pc = child.Value;
#else
            for (int i = 0, imax = mChildren.Count; i < imax; ++i)
            {
                UINode pc = (UINode)mChildren[i];
#endif
                if (pc.widget != null)
                {
                    int visibleFlag = 1;

                    // No sense in checking the visibility if we're not culling anything (as the visibility is always 'true')
                    if (shouldCull || transformsChanged)
                    {
                        // If the change flag has not yet been determined...
                        if (pc.changeFlag == -1)
                        {
                            pc.changeFlag = GetChangeFlag(pc);
                        }

                        // Is the widget visible?
                        if (shouldCull)
                        {
                            visibleFlag = (mCheckVisibility || pc.changeFlag == 1) ? (IsVisible(pc.widget) ? 1 : 0) : pc.visibleFlag;
                        }
                    }

                    // If visibility changed, mark the node as changed as well
                    if (pc.visibleFlag != visibleFlag)
                    {
                        pc.changeFlag = 1;
                    }

                    // If the node has changed and the widget is visible (or was visible before)
                    if (pc.changeFlag == 1 && (visibleFlag == 1 || pc.visibleFlag != 0))
                    {
                        // Update the visibility flag
                        pc.visibleFlag = visibleFlag;
                        Material mat = pc.widget.material;

                        // Add this material to the list of changed materials
                        if (!mChanged.Contains(mat))
                        {
                            mChanged.Add(mat);
                            mChangedLastFrame = true;
                        }
                    }
                }
            }
        }
        mCulled          = shouldCull;
        mCheckVisibility = false;
        mWidgetsAdded    = false;
    }

    /// <summary>
    /// Update all widgets and rebuild their geometry if necessary.
    /// </summary>

    void UpdateWidgets()
    {
#if USE_SIMPLE_DICTIONARY
        foreach (KeyValuePair <Transform, UINode> c in mChildren)
        {
            UINode pc = c.Value;
#else
        for (int i = 0, imax = mChildren.Count; i < imax; ++i)
        {
            UINode pc = (UINode)mChildren[i];
#endif
            UIWidget w = pc.widget;

            // If the widget is visible, update it
            if (pc.visibleFlag == 1 && w != null && w.UpdateGeometry(ref mWorldToLocal, (pc.changeFlag == 1), generateNormals))
            {
                // We will need to refill this buffer
                if (!mChanged.Contains(w.material))
                {
                    mChanged.Add(w.material);
                    mChangedLastFrame = true;
                }
            }
            pc.changeFlag = 0;
        }
    }

    /// <summary>
    /// Update the clipping rect in the shaders and draw calls' positions.
    /// </summary>

    bool nestingClip     = false;
    Vector4 newClipRange = Vector4.zero;
    public void UpdateDrawcalls()
    {
        Vector4 range = Vector4.zero;

        if (mClipping != UIDrawCall.Clipping.None)
        {
            UIDraggablePanel dragPanel = gameObject.GetComponent <UIDraggablePanel>();
            if (dragPanel != null && gameObject.transform.parent != null)
            {
                UIPanel parentPanel = NGUITools.FindInParents <UIPanel>(gameObject.transform.parent.gameObject);
                if (parentPanel != null && parentPanel.transform.parent != null && parentPanel.clipping != UIDrawCall.Clipping.None)
                {
                    Vector3 worldPos = transform.TransformPoint(Vector3.zero);
                    Vector3 localPos = parentPanel.transform.parent.InverseTransformPoint(worldPos);
                    newClipRange = mClipRange;

                    Vector4 parentRect = new Vector4(-localPos.x - parentPanel.clipRange.z / 2
                                                     , -localPos.y - parentPanel.clipRange.w / 2
                                                     , -localPos.x + parentPanel.clipRange.z / 2
                                                     , -localPos.y + parentPanel.clipRange.w / 2);

                    Vector4 selfRect = new Vector4(newClipRange.x - newClipRange.z / 2
                                                   , newClipRange.y - newClipRange.w / 2
                                                   , newClipRange.x + newClipRange.z / 2
                                                   , newClipRange.y + newClipRange.w / 2
                                                   );

                    if (selfRect.x < parentRect.x)
                    {
                        selfRect.x  = parentRect.x;
                        nestingClip = true;
                    }

                    if (selfRect.y < parentRect.y)
                    {
                        selfRect.y  = parentRect.y;
                        nestingClip = true;
                    }

                    if (selfRect.z > parentRect.z)
                    {
                        selfRect.z  = parentRect.z;
                        nestingClip = true;
                    }

                    if (selfRect.w > parentRect.w)
                    {
                        selfRect.w  = parentRect.w;
                        nestingClip = true;
                    }


                    float xLength = selfRect.z - selfRect.x;
                    if (xLength < 0)
                    {
                        xLength = 0.0001f;
                    }

                    float yLength = selfRect.w - selfRect.y;
                    if (yLength < 0)
                    {
                        yLength = 0.0001f;
                    }

                    newClipRange = new Vector4(selfRect.x + xLength / 2
                                               , selfRect.y + yLength / 2
                                               , xLength
                                               , yLength);

                    //if (newClipRange.x < -localPos.x - Mathf.Abs(parentPanel.clipRange.z - newClipRange.z)/2)
                    //{
                    //    float offset = -localPos.x - Mathf.Abs(parentPanel.clipRange.z - newClipRange.z)/2;
                    //    newClipRange.x = offset / 2;
                    //    newClipRange.z -= offset;

                    //    nestingClip = true;
                    //}
                    //else if (newClipRange.x > -localPos.x + Mathf.Abs(parentPanel.clipRange.z - newClipRange.z)/2)
                    //{
                    //    float offset = localPos.x + Mathf.Abs(parentPanel.clipRange.z - newClipRange.z)/2;
                    //    newClipRange.x -= offset / 2;
                    //    newClipRange.z = offset;

                    //    nestingClip = true;
                    //}
                    //else
                    //{
                    //    nestingClip = false;
                    //}

                    //if (newClipRange.y < -localPos.y)
                    //{
                    //    newClipRange.y = -localPos.y;
                    //    newClipRange.w = parentPanel.clipRange.w;
                    //}
                }
            }

            if (nestingClip)
            {
                range = new Vector4(newClipRange.x, newClipRange.y, newClipRange.z * 0.5f, newClipRange.w * 0.5f);
            }
            else
            {
                range = new Vector4(mClipRange.x, mClipRange.y, mClipRange.z * 0.5f, mClipRange.w * 0.5f);
            }
        }

        if (range.z == 0f)
        {
            range.z = Screen.width * 0.5f;
        }
        if (range.w == 0f)
        {
            range.w = Screen.height * 0.5f;
        }

        RuntimePlatform platform = Application.platform;

        if (platform == RuntimePlatform.WindowsPlayer ||
            platform == RuntimePlatform.WindowsWebPlayer ||
            platform == RuntimePlatform.WindowsEditor)
        {
            range.x -= 0.5f;
            range.y += 0.5f;
        }

        Transform t = cachedTransform;

        for (int i = 0, imax = mDrawCalls.size; i < imax; ++i)
        {
            UIDrawCall dc = mDrawCalls.buffer[i];
            dc.clipping     = mClipping;
            dc.clipRange    = range;
            dc.clipSoftness = mClipSoftness;
            dc.depthPass    = depthPass;

            // Set the draw call's transform to match the panel's.
            // Note that parenting directly to the panel causes unity to crash as soon as you hit Play.
            Transform dt = dc.transform;
            dt.position   = t.position;
            dt.rotation   = t.rotation;
            dt.localScale = t.lossyScale;
        }
    }

    /// <summary>
    /// Set the draw call's geometry responsible for the specified material.
    /// </summary>

    void Fill(Material mat)
    {
        // Cleanup deleted widgets
        for (int i = mWidgets.size; i > 0;)
        {
            if (mWidgets[--i] == null)
            {
                mWidgets.RemoveAt(i);
            }
        }

        // Fill the buffers for the specified material
        for (int i = 0, imax = mWidgets.size; i < imax; ++i)
        {
            UIWidget w = mWidgets.buffer[i];

            if (w.visibleFlag == 1 && w.material == mat)
            {
                UINode node = GetNode(w.cachedTransform);

                if (node != null)
                {
                    if (generateNormals)
                    {
                        w.WriteToBuffers(mVerts, mUvs, mCols, mNorms, mTans);
                    }
                    else
                    {
                        w.WriteToBuffers(mVerts, mUvs, mCols, null, null);
                    }
                }
                else
                {
                    Debug.LogError("No transform found for " + NGUITools.GetHierarchy(w.gameObject), this);
                }
            }
        }

        if (mVerts.size > 0)
        {
            // Rebuild the draw call's mesh
            UIDrawCall dc = GetDrawCall(mat, true);
            dc.depthPass = depthPass;
            dc.Set(mVerts, generateNormals ? mNorms : null, generateNormals ? mTans : null, mUvs, mCols);
        }
        else
        {
            // There is nothing to draw for this material -- eliminate the draw call
            UIDrawCall dc = GetDrawCall(mat, false);

            if (dc != null)
            {
                mDrawCalls.Remove(dc);
                NGUITools.DestroyImmediate(dc.gameObject);
            }
        }

        // Cleanup
        mVerts.Clear();
        mNorms.Clear();
        mTans.Clear();
        mUvs.Clear();
        mCols.Clear();
    }
示例#17
0
    /// <summary>
    /// Update all of the widgets belonging to this panel.
    /// </summary>

    void UpdateWidgets()
    {
#if UNITY_EDITOR
        bool forceVisible = cullWhileDragging ? false : (Application.isPlaying && mCullTime > mUpdateTime);
#else
        bool forceVisible = cullWhileDragging ? false : (mCullTime > mUpdateTime);
#endif
        bool changed = false;

        // Update all widgets
        for (int i = 0, imax = UIWidget.list.size; i < imax; ++i)
        {
            UIWidget w = UIWidget.list[i];

            // If the widget is visible, update it
            if (w.panel == this && w.enabled)
            {
#if UNITY_EDITOR
                // When an object is dragged from Project view to Scene view, its Z is...
                // odd, to say the least. Force it if possible.
                if (!Application.isPlaying)
                {
                    Transform t = w.cachedTransform;

                    if (t.hideFlags != HideFlags.HideInHierarchy)
                    {
                        t = (t.parent != null && t.parent.hideFlags == HideFlags.HideInHierarchy) ?
                            t.parent : null;
                    }

                    if (t != null)
                    {
                        for (; ;)
                        {
                            if (t.parent == null)
                            {
                                break;
                            }
                            if (t.parent.hideFlags == HideFlags.HideInHierarchy)
                            {
                                t = t.parent;
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (t != null)
                        {
                            Vector3 pos = t.localPosition;
                            pos.x = Mathf.Round(pos.x);
                            pos.y = Mathf.Round(pos.y);
                            pos.z = 0f;

                            if (Vector3.SqrMagnitude(t.localPosition - pos) > 0.0001f)
                            {
                                t.localPosition = pos;
                            }
                        }
                    }
                }
#endif
                bool vis = forceVisible ||
                           (mClipping == UIDrawCall.Clipping.None && !w.hideIfOffScreen) ||
                           (w.cumulativeAlpha > 0.001f && IsVisible(w));

                if (!w.UpdateGeometry(vis))
                {
                    continue;
                }

                changed = true;

                if (!mRebuild)
                {
                    if (w.drawCall != null)
                    {
                        w.drawCall.isDirty = true;
                    }
                    else
                    {
                        // Find an existing draw call, if possible
                        w.drawCall = InsertWidget(w);
                        if (w.drawCall == null)
                        {
                            mRebuild = true;
                        }
                    }
                }
            }
        }

        // Inform the changed event listeners
        if (changed && onChange != null)
        {
            onChange();
        }
    }
示例#18
0
    /// <summary>
    /// Convenience function that figures out the panel's correct change flag by searching the parents.
    /// </summary>

    int GetChangeFlag(UINode start)
    {
        int flag = start.changeFlag;

        if (flag == -1)
        {
            Transform trans = start.trans.parent;
            UINode    sub;

            // Keep going until we find a set flag
            for (;;)
            {
                // Check the parent's flag
#if USE_SIMPLE_DICTIONARY
                if (trans != null && mChildren.TryGetValue(trans, out sub))
                {
#else
                if (trans != null && mChildren.Contains(trans))
                {
                    sub = (UINode)mChildren[trans];
#endif
                    flag  = sub.changeFlag;
                    trans = trans.parent;

                    // If the flag hasn't been set either, add this child to the hierarchy
                    if (flag == -1)
                    {
                        mHierarchy.Add(sub);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    flag = 0;
                    break;
                }
            }

            // Update the parent flags
            for (int i = 0, imax = mHierarchy.size; i < imax; ++i)
            {
                UINode pc = mHierarchy.buffer[i];
                pc.changeFlag = flag;
            }
            mHierarchy.Clear();
        }
        return(flag);
    }

    /// <summary>
    /// Run through all managed transforms and see if they've changed.
    /// </summary>

    void UpdateTransforms()
    {
        bool transformsChanged = false;
        bool shouldCull        = false;

#if UNITY_EDITOR
        shouldCull = (clipping != UIDrawCall.Clipping.None) && (!Application.isPlaying || (cullWhileDragging || mUpdateTime > mCullTime));
        if (!Application.isPlaying || !widgetsAreStatic || mWidgetsAdded || shouldCull != mCulled)
#else
        shouldCull = (clipping != UIDrawCall.Clipping.None) && (mUpdateTime > mCullTime);
        if (!widgetsAreStatic || mWidgetsAdded || shouldCull != mCulled)
#endif
        {
#if USE_SIMPLE_DICTIONARY
            foreach (KeyValuePair <Transform, UINode> child in mChildren)
            {
                UINode node = child.Value;
#else
            for (int i = 0, imax = mChildren.Count; i < imax; ++i)
            {
                UINode node = (UINode)mChildren[i];
#endif
                if (node.trans == null)
                {
                    mRemoved.Add(node.trans);
                    continue;
                }

                if (node.HasChanged())
                {
                    node.changeFlag   = 1;
                    transformsChanged = true;
#if UNITY_EDITOR
                    Vector3 s   = node.trans.lossyScale;
                    float   min = Mathf.Abs(Mathf.Min(s.x, s.y));

                    if (min == 0f)
                    {
                        Debug.LogError("Scale of 0 is invalid! Zero cannot be divided by, which causes problems. Use a small value instead, such as 0.01\n" +
                                       node.trans.lossyScale, node.trans);
                    }
#endif
                }
                else
                {
                    node.changeFlag = -1;
                }
            }

            // Clean up the deleted transforms
            for (int i = 0, imax = mRemoved.Count; i < imax; ++i)
            {
                mChildren.Remove(mRemoved[i]);
            }
            mRemoved.Clear();
        }

        // If the children weren't culled but should be, check their visibility
        if (!mCulled && shouldCull)
        {
            mCheckVisibility = true;
        }

        // If something has changed, propagate the changes *down* the tree hierarchy (to children).
        // An alternative (but slower) approach would be to do a pc.trans.GetComponentsInChildren<UIWidget>()
        // in the loop above, and mark each one as dirty.

        if (mCheckVisibility || transformsChanged || mRebuildAll)
        {
#if USE_SIMPLE_DICTIONARY
            foreach (KeyValuePair <Transform, UINode> child in mChildren)
            {
                UINode pc = child.Value;
#else
            for (int i = 0, imax = mChildren.Count; i < imax; ++i)
            {
                UINode pc = (UINode)mChildren[i];
#endif
                if (pc.widget != null)
                {
                    int visibleFlag = 1;

                    // No sense in checking the visibility if we're not culling anything (as the visibility is always 'true')
                    if (shouldCull || transformsChanged)
                    {
                        // If the change flag has not yet been determined...
                        if (pc.changeFlag == -1)
                        {
                            pc.changeFlag = GetChangeFlag(pc);
                        }

                        // Is the widget visible?
                        if (shouldCull)
                        {
                            visibleFlag = (mCheckVisibility || pc.changeFlag == 1) ? (IsVisible(pc.widget) ? 1 : 0) : pc.visibleFlag;
                        }
                    }

                    // If visibility changed, mark the node as changed as well
                    if (pc.visibleFlag != visibleFlag)
                    {
                        pc.changeFlag = 1;
                    }

                    // If the node has changed and the widget is visible (or was visible before)
                    if (pc.changeFlag == 1 && (visibleFlag == 1 || pc.visibleFlag != 0))
                    {
                        // Update the visibility flag
                        pc.visibleFlag = visibleFlag;
                        Material mat = pc.widget.material;

                        // Add this material to the list of changed materials
                        if (!mChanged.Contains(mat))
                        {
                            mChanged.Add(mat);
                        }
                    }
                }
            }
        }
        mCulled          = shouldCull;
        mCheckVisibility = false;
        mWidgetsAdded    = false;
    }

    /// <summary>
    /// Update all widgets and rebuild their geometry if necessary.
    /// </summary>

    void UpdateWidgets()
    {
#if USE_SIMPLE_DICTIONARY
        foreach (KeyValuePair <Transform, UINode> c in mChildren)
        {
            UINode pc = c.Value;
#else
        for (int i = 0, imax = mChildren.Count; i < imax; ++i)
        {
            UINode pc = (UINode)mChildren[i];
#endif
            UIWidget w = pc.widget;

            // If the widget is visible, update it
            if (pc.visibleFlag == 1 && w != null && w.UpdateGeometry(this, ref worldToLocal, (pc.changeFlag == 1), generateNormals))
            {
                // We will need to refill this buffer
                if (!mChanged.Contains(w.material))
                {
                    mChanged.Add(w.material);
                }
            }
            pc.changeFlag = 0;
        }
    }
#endif

    /// <summary>
    /// Update the clipping rect in the shaders and draw calls' positions.
    /// </summary>

    public void UpdateDrawcalls()
    {
        Vector4 range = Vector4.zero;

        if (mClipping != UIDrawCall.Clipping.None)
        {
            range = new Vector4(mClipRange.x, mClipRange.y, mClipRange.z * 0.5f, mClipRange.w * 0.5f);
        }

        if (range.z == 0f)
        {
            range.z = Screen.width * 0.5f;
        }
        if (range.w == 0f)
        {
            range.w = Screen.height * 0.5f;
        }

        RuntimePlatform platform = Application.platform;

        if (platform == RuntimePlatform.WindowsPlayer ||
            platform == RuntimePlatform.WindowsWebPlayer ||
            platform == RuntimePlatform.WindowsEditor)
        {
            range.x -= 0.5f;
            range.y += 0.5f;
        }

        Transform  t = cachedTransform;
        UIDrawCall dc;
        Transform  dt;

        for (int i = 0, imax = mDrawCalls.size; i < imax; ++i)
        {
            dc              = mDrawCalls.buffer[i];
            dc.clipping     = mClipping;
            dc.clipRange    = range;
            dc.clipSoftness = mClipSoftness;
            dc.depthPass    = depthPass && mClipping == UIDrawCall.Clipping.None;

            // Set the draw call's transform to match the panel's.
            // Note that parenting directly to the panel causes unity to crash as soon as you hit Play.
            dt            = dc.transform;
            dt.position   = t.position;
            dt.rotation   = t.rotation;
            dt.localScale = t.lossyScale;
        }
    }

    /// <summary>
    /// Set the draw call's geometry responsible for the specified material.
    /// </summary>

    void Fill(Material mat)
    {
        int highest = -100;

        // Fill the buffers for the specified material
        for (int i = 0; i < mWidgets.size;)
        {
            UIWidget w = mWidgets.buffer[i];

            if (w == null)
            {
                mWidgets.RemoveAt(i);
                continue;
            }
#if OLD_UNITY
            else if (w.visibleFlag == 1 && w.material == mat)
#else
            else if (w.material == mat && w.isVisible)
#endif
            {
                if (w.panel == this)
                {
                    int depth = w.depth;
                    if (depth > highest)
                    {
                        highest = depth;
                    }
                    if (generateNormals)
                    {
                        w.WriteToBuffers(mVerts, mUvs, mCols, mNorms, mTans);
                    }
                    else
                    {
                        w.WriteToBuffers(mVerts, mUvs, mCols, null, null);
                    }
                }
                else
                {
                    mWidgets.RemoveAt(i);
                    continue;
                }
            }
            ++i;
        }

        if (mVerts.size > 0)
        {
            // Rebuild the draw call's mesh
            UIDrawCall dc = GetDrawCall(mat, true);
            dc.depthPass = depthPass && mClipping == UIDrawCall.Clipping.None;
            dc.depth     = sortByDepth ? highest : 0;
            dc.Set(mVerts, generateNormals ? mNorms : null, generateNormals ? mTans : null, mUvs, mCols);
            dc.mainTexture = mat.mainTexture;
        }
        else
        {
            // There is nothing to draw for this material -- eliminate the draw call
            UIDrawCall dc = GetDrawCall(mat, false);

            if (dc != null)
            {
                mDrawCalls.Remove(dc);
                NGUITools.DestroyImmediate(dc.gameObject);
            }
        }

        // Cleanup
        mVerts.Clear();
        mNorms.Clear();
        mTans.Clear();
        mUvs.Clear();
        mCols.Clear();
    }

    /// <summary>
    /// Main update function
    /// </summary>

    void LateUpdate()
    {
        mUpdateTime = Time.realtimeSinceStartup;
        UpdateTransformMatrix();
#if OLD_UNITY
        UpdateTransforms();
#endif
        // Always move widgets to the panel's layer
        if (mLayer != cachedGameObject.layer)
        {
            mLayer = mGo.layer;
            UICamera uic = UICamera.FindCameraForLayer(mLayer);
            mCam = (uic != null) ? uic.cachedCamera : NGUITools.FindCameraForLayer(mLayer);
            SetChildLayer(cachedTransform, mLayer);
            for (int i = 0, imax = drawCalls.size; i < imax; ++i)
            {
                mDrawCalls.buffer[i].gameObject.layer = mLayer;
            }
        }

#if OLD_UNITY
        UpdateWidgets();
#else
#if UNITY_EDITOR
        bool forceVisible = cullWhileDragging ? false : (clipping == UIDrawCall.Clipping.None) || (Application.isPlaying && mCullTime > mUpdateTime);
#else
        bool forceVisible = cullWhileDragging ? false : (clipping == UIDrawCall.Clipping.None) || (mCullTime > mUpdateTime);
#endif
        // Update all widgets
        for (int i = 0, imax = mWidgets.size; i < imax; ++i)
        {
            UIWidget w = mWidgets[i];

            // If the widget is visible, update it
            if (w.UpdateGeometry(this, forceVisible))
            {
                // We will need to refill this buffer
                if (!mChanged.Contains(w.material))
                {
                    mChanged.Add(w.material);
                }
            }
        }
#endif
        // Inform the changed event listeners
        if (mChanged.size != 0 && onChange != null)
        {
            onChange();
        }

        // If the depth has changed, we need to re-sort the widgets
        if (mDepthChanged)
        {
            mDepthChanged = false;
            mWidgets.Sort(UIWidget.CompareFunc);
        }

        // Fill the draw calls for all of the changed materials
        for (int i = 0, imax = mChanged.size; i < imax; ++i)
        {
            Fill(mChanged.buffer[i]);
        }

        // Update the clipping rects
        UpdateDrawcalls();
        mChanged.Clear();
#if OLD_UNITY
        mRebuildAll = false;
#endif
#if UNITY_EDITOR
        mScreenSize = new Vector2(Screen.width, Screen.height);
#endif
    }
示例#19
0
	/// <summary>
	/// Update all of the widgets belonging to this panel.
	/// </summary>

	void UpdateWidgets()
	{
#if UNITY_EDITOR
		bool forceVisible = cullWhileDragging ? false : (Application.isPlaying && mCullTime > mUpdateTime);
#else
		bool forceVisible = cullWhileDragging ? false : (mCullTime > mUpdateTime);
#endif
		bool changed = false;

		if (mForced != forceVisible)
		{
			mForced = forceVisible;
			mResized = true;
		}

		bool clipped = hasCumulativeClipping;

		// Update all widgets
		for (int i = 0, imax = widgets.Count; i < imax; ++i)
		{
			UIWidget w = widgets[i];

			// If the widget is visible, update it
			if (w.panel == this && w.enabled)
			{
#if UNITY_EDITOR
				// When an object is dragged from Project view to Scene view, its Z is...
				// odd, to say the least. Force it if possible.
				if (!Application.isPlaying)
				{
					Transform t = w.cachedTransform;

					if (t.hideFlags != HideFlags.HideInHierarchy)
					{
						t = (t.parent != null && t.parent.hideFlags == HideFlags.HideInHierarchy) ?
							t.parent : null;
					}

					if (t != null)
					{
						for (; ; )
						{
							if (t.parent == null) break;
							if (t.parent.hideFlags == HideFlags.HideInHierarchy) t = t.parent;
							else break;
						}

						if (t != null)
						{
							Vector3 pos = t.localPosition;
							pos.x = Mathf.Round(pos.x);
							pos.y = Mathf.Round(pos.y);
							pos.z = 0f;

							if (Vector3.SqrMagnitude(t.localPosition - pos) > 0.0001f)
								t.localPosition = pos;
						}
					}
				}
#endif
				int frame = Time.frameCount;

				// First update the widget's transform
				if (w.UpdateTransform(frame) || mResized)
				{
					// Only proceed to checking the widget's visibility if it actually moved
					bool vis = forceVisible || (w.CalculateCumulativeAlpha(frame) > 0.001f);
					w.UpdateVisibility(vis, forceVisible || ((clipped || w.hideIfOffScreen) ? IsVisible(w) : true));
				}
				
				// Update the widget's geometry if necessary
				if (w.UpdateGeometry(frame))
				{
					changed = true;

					if (!mRebuild)
					{
						if (w.drawCall != null)
						{
							w.drawCall.isDirty = true;
						}
						else
						{
							// Find an existing draw call, if possible
							FindDrawCall(w);
						}
					}
				}
			}
		}

		// Inform the changed event listeners
		if (changed && onGeometryUpdated != null) onGeometryUpdated();
		mResized = false;
	}
示例#20
0
    /// <summary>
    /// Convenience function that figures out the panel's correct change flag by searching the parents.
    /// </summary>

    int GetChangeFlag(UINode start)
    {
        int flag = start.changeFlag;

        if (flag == -1)
        {
            Transform trans = start.trans.parent;
            UINode    sub;

            // Keep going until we find a set flag
            for (;;)
            {
                // Check the parent's flag
#if UNITY_FLASH
                if (trans != null && mChildren.TryGetValue(trans, out sub))
                {
#else
                if (trans != null && mChildren.Contains(trans))
                {
                    sub = (UINode)mChildren[trans];
#endif
                    flag  = sub.changeFlag;
                    trans = trans.parent;

                    // If the flag hasn't been set either, add this child to the hierarchy
                    if (flag == -1)
                    {
                        mHierarchy.Add(sub);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    flag = 0;
                    break;
                }
            }

            // Update the parent flags
            for (int i = 0, imax = mHierarchy.size; i < imax; ++i)
            {
                UINode pc = mHierarchy.buffer[i];
                pc.changeFlag = flag;
            }
            mHierarchy.Clear();
        }
        return(flag);
    }

    /// <summary>
    /// Update the world-to-local transform matrix as well as clipping bounds.
    /// </summary>

    void UpdateTransformMatrix()
    {
        float time = Time.realtimeSinceStartup;

        if (time == 0f || mMatrixTime != time)
        {
            mMatrixTime   = time;
            mWorldToLocal = cachedTransform.worldToLocalMatrix;

            if (mClipping != UIDrawCall.Clipping.None)
            {
                Vector2 size = new Vector2(mClipRange.z, mClipRange.w);

                if (size.x == 0f)
                {
                    size.x = mScreenSize.x;
                }
                if (size.y == 0f)
                {
                    size.y = mScreenSize.y;
                }

                size *= 0.5f;

                mMin.x = mClipRange.x - size.x;
                mMin.y = mClipRange.y - size.y;
                mMax.x = mClipRange.x + size.x;
                mMax.y = mClipRange.y + size.y;
            }
        }
    }

    /// <summary>
    /// Run through all managed transforms and see if they've changed.
    /// </summary>

    void UpdateTransforms()
    {
        mChangedLastFrame = false;
        bool transformsChanged = false;

#if UNITY_EDITOR
        bool shouldCull = !Application.isPlaying || Time.realtimeSinceStartup > mCullTime;
        if (!Application.isPlaying || !widgetsAreStatic || mWidgetsAdded || shouldCull != mCulled)
#else
        bool shouldCull = Time.realtimeSinceStartup > mCullTime;
        if (!widgetsAreStatic || mWidgetsAdded || shouldCull != mCulled)
#endif
        {
#if UNITY_FLASH
            foreach (KeyValuePair <Transform, UINode> child in mChildren)
            {
                UINode node = child.Value;
#else
            for (int i = 0, imax = mChildren.Count; i < imax; ++i)
            {
                UINode node = (UINode)mChildren[i];
#endif
                if (node.trans == null)
                {
                    mRemoved.Add(node.trans);
                    continue;
                }

                if (node.HasChanged())
                {
                    node.changeFlag   = 1;
                    transformsChanged = true;
                }
                else
                {
                    node.changeFlag = -1;
                }
            }

            // Clean up the deleted transforms
            for (int i = 0, imax = mRemoved.Count; i < imax; ++i)
            {
                mChildren.Remove(mRemoved[i]);
            }
            mRemoved.Clear();
        }

        // If the children weren't culled but should be, check their visibility
        if (!mCulled && shouldCull)
        {
            mCheckVisibility = true;
        }

        // If something has changed, propagate the changes *down* the tree hierarchy (to children).
        // An alternative (but slower) approach would be to do a pc.trans.GetComponentsInChildren<UIWidget>()
        // in the loop above, and mark each one as dirty.

        if (mCheckVisibility || transformsChanged || mRebuildAll)
        {
#if UNITY_FLASH
            foreach (KeyValuePair <Transform, UINode> child in mChildren)
            {
                UINode pc = child.Value;
#else
            for (int i = 0, imax = mChildren.Count; i < imax; ++i)
            {
                UINode pc = (UINode)mChildren[i];
#endif
                if (pc.widget != null)
                {
                    int visibleFlag = 1;

                    // No sense in checking the visibility if we're not culling anything (as the visibility is always 'true')
                    if (shouldCull || transformsChanged)
                    {
                        // If the change flag has not yet been determined...
                        if (pc.changeFlag == -1)
                        {
                            pc.changeFlag = GetChangeFlag(pc);
                        }

                        // Is the widget visible?
                        if (shouldCull)
                        {
                            visibleFlag = (mCheckVisibility || pc.changeFlag == 1) ? (IsVisible(pc.widget) ? 1 : 0) : pc.visibleFlag;
                        }
                    }

                    // If visibility changed, mark the node as changed as well
                    if (pc.visibleFlag != visibleFlag)
                    {
                        pc.changeFlag = 1;
                    }

                    // If the node has changed and the widget is visible (or was visible before)
                    if (pc.changeFlag == 1 && (visibleFlag == 1 || pc.visibleFlag != 0))
                    {
                        // Update the visibility flag
                        pc.visibleFlag = visibleFlag;
                        Material mat = pc.widget.material;

                        // Add this material to the list of changed materials
                        if (!mChanged.Contains(mat))
                        {
                            mChanged.Add(mat);
                            mChangedLastFrame = true;
                        }
                    }
                }
            }
        }
        mCulled          = shouldCull;
        mCheckVisibility = false;
        mWidgetsAdded    = false;
    }

    /// <summary>
    /// Update all widgets and rebuild their geometry if necessary.
    /// </summary>

    void UpdateWidgets()
    {
#if UNITY_FLASH
        foreach (KeyValuePair <Transform, UINode> c in mChildren)
        {
            UINode pc = c.Value;
#else
        for (int i = 0, imax = mChildren.Count; i < imax; ++i)
        {
            UINode pc = (UINode)mChildren[i];
#endif
            UIWidget w = pc.widget;

            // If the widget is visible, update it
            if (pc.visibleFlag == 1 && w != null && w.UpdateGeometry(ref mWorldToLocal, (pc.changeFlag == 1), generateNormals))
            {
                // We will need to refill this buffer
                if (!mChanged.Contains(w.material))
                {
                    mChanged.Add(w.material);
                    mChangedLastFrame = true;
                }
            }
            pc.changeFlag = 0;
        }
    }

    /// <summary>
    /// Update the clipping rect in the shaders and draw calls' positions.
    /// </summary>

    public void UpdateDrawcalls()
    {
        Vector4 range = Vector4.zero;

        if (mClipping != UIDrawCall.Clipping.None)
        {
            range = new Vector4(mClipRange.x, mClipRange.y, mClipRange.z * 0.5f, mClipRange.w * 0.5f);
        }

        if (range.z == 0f)
        {
            range.z = mScreenSize.x * 0.5f;
        }
        if (range.w == 0f)
        {
            range.w = mScreenSize.y * 0.5f;
        }

        RuntimePlatform platform = Application.platform;

        if (platform == RuntimePlatform.WindowsPlayer ||
            platform == RuntimePlatform.WindowsWebPlayer ||
            platform == RuntimePlatform.WindowsEditor)
        {
            range.x -= 0.5f;
            range.y += 0.5f;
        }

        Transform t = cachedTransform;

        for (int i = 0, imax = mDrawCalls.size; i < imax; ++i)
        {
            UIDrawCall dc = mDrawCalls.buffer[i];
            dc.clipping     = mClipping;
            dc.clipRange    = range;
            dc.clipSoftness = mClipSoftness;
            dc.depthPass    = depthPass;

            // Set the draw call's transform to match the panel's.
            // Note that parenting directly to the panel causes unity to crash as soon as you hit Play.
            Transform dt = dc.transform;
            dt.position   = t.position;
            dt.rotation   = t.rotation;
            dt.localScale = t.lossyScale;
        }
    }

    /// <summary>
    /// Set the draw call's geometry responsible for the specified material.
    /// </summary>

    void Fill(Material mat)
    {
        // Cleanup deleted widgets
        for (int i = mWidgets.size; i > 0;)
        {
            if (mWidgets[--i] == null)
            {
                mWidgets.RemoveAt(i);
            }
        }

        // Fill the buffers for the specified material
        for (int i = 0, imax = mWidgets.size; i < imax; ++i)
        {
            UIWidget w = mWidgets.buffer[i];

            if (w.visibleFlag == 1 && w.material == mat)
            {
                UINode node = GetNode(w.cachedTransform);

                if (node != null)
                {
                    if (generateNormals)
                    {
                        w.WriteToBuffers(mVerts, mUvs, mCols, mNorms, mTans);
                    }
                    else
                    {
                        w.WriteToBuffers(mVerts, mUvs, mCols, null, null);
                    }
                }
                else
                {
                    Debug.LogError("No transform found for " + NGUITools.GetHierarchy(w.gameObject), this);
                }
            }
        }

        if (mVerts.size > 0)
        {
            // Rebuild the draw call's mesh
            UIDrawCall dc = GetDrawCall(mat, true);
            dc.depthPass = depthPass;
            dc.Set(mVerts, generateNormals ? mNorms : null, generateNormals ? mTans : null, mUvs, mCols);
        }
        else
        {
            // There is nothing to draw for this material -- eliminate the draw call
            UIDrawCall dc = GetDrawCall(mat, false);

            if (dc != null)
            {
                mDrawCalls.Remove(dc);
                NGUITools.DestroyImmediate(dc.gameObject);
            }
        }

        // Cleanup
        mVerts.Clear();
        mNorms.Clear();
        mTans.Clear();
        mUvs.Clear();
        mCols.Clear();
    }

    /// <summary>
    /// Main update function
    /// </summary>

    void LateUpdate()
    {
        UpdateTransformMatrix();
        UpdateTransforms();

        // Always move widgets to the panel's layer
        if (mLayer != gameObject.layer)
        {
            mLayer = gameObject.layer;
            UICamera uic = UICamera.FindCameraForLayer(mLayer);
            mCam = (uic != null) ? uic.cachedCamera : NGUITools.FindCameraForLayer(mLayer);
            SetChildLayer(cachedTransform, mLayer);
            for (int i = 0, imax = drawCalls.size; i < imax; ++i)
            {
                mDrawCalls.buffer[i].gameObject.layer = mLayer;
            }
        }

        UpdateWidgets();

        // If the depth has changed, we need to re-sort the widgets
        if (mDepthChanged)
        {
            mDepthChanged = false;
            mWidgets.Sort(UIWidget.CompareFunc);
        }

        // Fill the draw calls for all of the changed materials
        for (int i = 0, imax = mChanged.size; i < imax; ++i)
        {
            Fill(mChanged.buffer[i]);
        }

        // Update the clipping rects
        UpdateDrawcalls();
        mChanged.Clear();
        mRebuildAll = false;

#if UNITY_EDITOR
        mScreenSize = new Vector2(Screen.width, Screen.height);

        UIRoot root = NGUITools.FindInParents <UIRoot>(gameObject);
        if (root != null)
        {
            mScreenSize *= root.GetPixelSizeAdjustment(Screen.height);
        }
#endif
    }
示例#21
0
文件: UIPanel.cs 项目: fengqk/Art
	/// <summary>
	/// Make the following widget be managed by the panel.
	/// </summary>

	public void AddWidget (UIWidget w)
	{
		w.UpdateGeometry(Time.frameCount);

		mUpdateScroll = true;

		if (widgets.size == 0)
		{
			widgets.Add(w);
		}
		else if (mSortWidgets)
		{
			widgets.Add(w);
			SortWidgets();
		}
		else if (UIWidget.PanelCompareFunc(w, widgets[0]) == -1)
		{
			widgets.Insert(0, w);
		}
		else
		{
			for (int i = widgets.size; i > 0; )
			{
				if (UIWidget.PanelCompareFunc(w, widgets[--i]) == -1) continue;
				widgets.Insert(i+1, w);
				break;
			}
		}
		FindDrawCall(w);
	}