/// <summary>
    /// Find all the required components.
    /// </summary>

    void Start()
    {
        mTrans  = transform;
        mMain   = Camera.main;
        mWidget = GetComponent <SGWidget>();
        mAlpha  = mWidget.color.a;

        // Find the camera automatically
        if (hudCamera == null)
        {
            hudCamera = HUDCamera.cam;
        }
        if (hudCamera == null)
        {
            hudCamera = Tools.FindInParents <Camera>(mTrans);
        }

        if (hudCamera == null)
        {
            Debug.LogError(Tools.GetHierarchy(gameObject) + " needs a camera to work with");
            Destroy(this);
        }
        else if (mMain == hudCamera)
        {
            Debug.LogWarning("HUD camera and main cameras are the same. This script will do nothing.");
            Destroy(this);
        }
        else
        {
            // Remember the initial distance
            mDist = hudCamera.WorldToViewportPoint(mTrans.position).z;
        }
    }
示例#2
0
    protected override void OnCopyFrom(SGWidget widget)
    {
        SGSprite copy = widget as SGSprite;

        if (copy != null)
        {
            textureRect = copy.textureRect;
            centered = copy.centered;
        }
    }
    /// <summary>
    /// Remove the specified widget from the managed list.
    /// </summary>

    public void RemoveWidget(SGWidget widget)
    {
        //Debug.Log("Removing " + widget.name + " from " + name);

        if (mWidgets != null)
        {
            mWidgets.Remove(widget);
            mRebuild = true;
        }
    }
    protected override void OnCopyFrom(SGWidget widget)
    {
        SGSprite copy = widget as SGSprite;

        if (copy != null)
        {
            textureRect = copy.textureRect;
            centered    = copy.centered;
        }
    }
示例#5
0
    /// <summary>
    /// Add the specified widget to the managed list.
    /// </summary>
    public void AddWidget(SGWidget widget)
    {
        //Debug.Log("Adding " + widget.name + " to " + name);

        if (widget != null && !mWidgets.Contains(widget))
        {
            mWidgets.Add(widget);
            mRebuild = true;
            if (!Application.isPlaying) LateUpdate();
        }
    }
    /// <summary>
    /// Static widget comparison function used for Z-sorting.
    /// </summary>

    static public int CompareFunc(SGWidget left, SGWidget right)
    {
        if (left.mDepth > right.mDepth)
        {
            return(1);
        }
        if (left.mDepth < right.mDepth)
        {
            return(-1);
        }
        return(0);
    }
    protected override void OnCopyFrom(SGWidget widget)
    {
        base.OnCopyFrom(widget);

        SGBorderedSprite copy = widget as SGBorderedSprite;

        if (copy != null)
        {
            outerRect = copy.outerRect;
            innerRect = copy.innerRect;
        }
    }
    /// <summary>
    /// Add the specified widget to the managed list.
    /// </summary>

    public void AddWidget(SGWidget widget)
    {
        //Debug.Log("Adding " + widget.name + " to " + name);

        if (widget != null && !mWidgets.Contains(widget))
        {
            mWidgets.Add(widget);
            mRebuild = true;
            if (!Application.isPlaying)
            {
                LateUpdate();
            }
        }
    }
    /// <summary>
    /// Work-around for Unity not calling OnEnable in some cases.
    /// </summary>

    void Update()
    {
        if (mScreen == null)
        {
            Start();
        }

        if (sizeMatchesScale)
        {
            OnMatchScale(transform.localScale);
            sizeMatchesScale = false;
        }

        if (copyFrom != null)
        {
            OnCopyFrom(copyFrom);
            copyFrom = null;
        }
    }
示例#10
0
 void Start()
 {
     mWidget = GetComponent <SGWidget>();
     mNormal = mWidget.color;
 }
示例#11
0
    /// <summary>
    /// Called when a "copy from" widget gets assigned
    /// </summary>

    virtual protected void OnCopyFrom(SGWidget widget)
    {
        color = widget.color;
    }
 void Start()
 {
     mWidget = GetComponent <SGWidget>();
 }
示例#13
0
    /// <summary>
    /// Remove the specified widget from the managed list.
    /// </summary>
    public void RemoveWidget(SGWidget widget)
    {
        //Debug.Log("Removing " + widget.name + " from " + name);

        if (mWidgets != null)
        {
            mWidgets.Remove(widget);
            mRebuild = true;
        }
    }
示例#14
0
 void Start()
 {
     mWidget = GetComponent<SGWidget>();
 }
示例#15
0
    /// <summary>
    /// Rebuild the UI.
    /// </summary>

    void LateUpdate()
    {
        if (mWidgets == null)
        {
            return;
        }

        // Update all widgets
        for (int i = mWidgets.Count; i > 0;)
        {
            SGWidget w = mWidgets[--i];
            if (w == null)
            {
                mWidgets.RemoveAt(i);
            }
            else
            {
                mRebuild |= w.ScreenUpdate();
            }
        }

        // No need to keep this screen if we don't have any widgets left
        if (mWidgets.Count == 0)
        {
            if (Application.isPlaying)
            {
                Destroy(gameObject);
                return;
            }

            if (mMesh != null)
            {
                DestroyImmediate(mMesh);
                mMesh = null;
            }
            if (this != null)
            {
                DestroyImmediate(gameObject);
            }
            return;
        }

        // Only continue if we need to rebuild
        if (!mRebuild)
        {
            return;
        }

        // Sort all widgets back-to-front
        mWidgets.Sort(SGWidget.CompareFunc);

        // Cache all components
        if (mFilter == null)
        {
            mFilter = gameObject.GetComponent <MeshFilter>();
        }
        if (mFilter == null)
        {
            mFilter = gameObject.AddComponent <MeshFilter>();
        }
        if (mRen == null)
        {
            mRen = gameObject.GetComponent <MeshRenderer>();
        }

        if (mRen == null)
        {
            mRen = gameObject.AddComponent <MeshRenderer>();
            mRen.sharedMaterial = mMat;
        }

        // Fill the vertices and UVs
        foreach (SGWidget w in mWidgets)
        {
            int offset = mVerts.Count;
            w.OnFill(mVerts, mUvs, mCols);

            // Transform all vertices into world space
            Transform t = w.mTrans;

            for (int i = offset, imax = mVerts.Count; i < imax; ++i)
            {
                mVerts[i] = t.TransformPoint(mVerts[i]);
            }
        }
        int count = mVerts.Count;

        // Safety check to ensure we get valid values
        if (count > 0 && (count == mUvs.Count && count == mCols.Count) && (count % 4) == 0)
        {
            int index = 0;

            // It takes 6 indices to draw a quad of 4 vertices
            int[] indices = new int[(count >> 1) * 3];

            // Populate the index buffer
            for (int i = 0; i < count; i += 4)
            {
                indices[index++] = i;
                indices[index++] = i + 1;
                indices[index++] = i + 2;

                indices[index++] = i + 2;
                indices[index++] = i + 3;
                indices[index++] = i;
            }

            if (mMesh == null)
            {
                mMesh      = new Mesh();
                mMesh.name = "UIScreen for " + mMat.name;
            }
            else
            {
                mMesh.Clear();
            }

            // Set the mesh values
            mMesh.vertices  = mVerts.ToArray();
            mMesh.uv        = mUvs.ToArray();
            mMesh.colors    = mCols.ToArray();
            mMesh.triangles = indices;
            mMesh.RecalculateBounds();
            mFilter.mesh = mMesh;
        }
        else
        {
            Debug.LogError("UIWidgets must fill the buffer with 4 vertices per quad. Found " + count);
        }

        // Cleanup
        mVerts.Clear();
        mUvs.Clear();
        mCols.Clear();

        // Don't rebuild the screen next frame
        mRebuild = false;
    }
示例#16
0
 void Start()
 {
     mWidget = GetComponent <SGWidget>();
     StartCoroutine(Flicker());
 }