Esempio n. 1
0
    //Below this line the script is basically loading in the chosen theme based on the selection from the inspector
    //Selection is based on 'Theme' enum
    //-------------------------------------------------------------------------------------------------------------


    void BuildHierarchy()
    {
        //Choose anchor position based on positioning enum selection
        Vector2 anchors;

        if (positioning.Equals(Positioning.Left))
        {
            anchors = new Vector2(0, 1);
        }
        else
        {
            anchors = new Vector2(1, 1);
        }
        //Create a canvas
        //---------------------------------------------------------------------------------------
        GameObject canvasObject = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler));
        Canvas     c            = canvasObject.GetComponent <Canvas>();

        c.pixelPerfect = true;
        c.renderMode   = RenderMode.ScreenSpaceOverlay;
        //---------------------------------------------------------------------------------------
        //Create textpanel
        //---------------------------------------------------------------------------------------
        GameObject    textPanelObject = new GameObject("textPanel", typeof(CanvasRenderer), typeof(Image));
        RectTransform textPanelRect   = textPanelObject.GetComponent <RectTransform>();

        SetAnchors(textPanelRect, anchors);

        textPanelObject.transform.SetParent(canvasObject.transform);

        Vector2 size = new Vector2(270, 40);

        textPanelRect.sizeDelta = size;

        HideImage(textPanelObject);

        //---------------------------------------------------------------------------------------
        //Create imgpanel
        //---------------------------------------------------------------------------------------
        GameObject    barPanelObject = new GameObject("barPanel", typeof(CanvasRenderer), typeof(Image));
        RectTransform barPanelRect   = barPanelObject.GetComponent <RectTransform>();

        SetAnchors(barPanelRect, anchors);
        barPanelObject.transform.SetParent(canvasObject.transform);

        Vector2 barPanelSize = new Vector2(270, 70);

        barPanelRect.sizeDelta = barPanelSize;

        HideImage(barPanelObject);

        //---------------------------------------------------------------------------------------
        //Create critical panel
        //---------------------------------------------------------------------------------------
        GameObject    criticalPanelObject = new GameObject("criticalPanel", typeof(CanvasRenderer), typeof(Image));
        RectTransform criticalPanelRect   = criticalPanelObject.GetComponent <RectTransform>();

        SetAnchors(criticalPanelRect, anchors);
        criticalPanelObject.transform.SetParent(canvasObject.transform);

        Vector2 sizeee = new Vector2(270, 40);

        criticalPanelRect.sizeDelta = sizeee;

        if (positioning.Equals(Positioning.Right))
        {
            PositionRight(textPanelObject);
            PositionRight(barPanelObject);
            PositionRight(criticalPanelObject);
        }
        else
        {
            PositionLeft(textPanelObject);
            PositionLeft(barPanelObject);
            PositionLeft(criticalPanelObject);
        }

        HideImage(criticalPanelObject);

        //--------------------------------------------------------------------------------------
        //Create healthbar (filler content) itself
        //---------------------------------------------------------------------------------------
        GameObject    barObject = new GameObject("Healthbar", typeof(CanvasRenderer), typeof(Image));
        RectTransform barRect   = barObject.GetComponent <RectTransform>();

        //Position anchors and parent it to canvas
        barObject.transform.SetParent(barPanelObject.transform);
        Centralize(barRect);

        barObject.transform.localPosition = Vector3.zero;

        //Set image type to filled
        bar            = barObject.GetComponent <Image>();
        bar.fillMethod = Image.FillMethod.Horizontal;
        bar.type       = Image.Type.Filled;
        //----------------------------------------------------------------------------------------
        //Create healthbar frame
        //----------------------------------------------------------------------------------------
        GameObject    barFrameObject = new GameObject("Healthbar_frame", typeof(CanvasRenderer), typeof(Image));
        RectTransform barFrameRect   = barFrameObject.GetComponent <RectTransform>();

        frame = barFrameObject.GetComponent <Image>();
        //Anchors and parenting to canvas
        barFrameRect.transform.SetParent(barPanelObject.transform);
        Centralize(barFrameRect);
        barFrameObject.transform.localPosition = Vector3.zero;

        //---------------------------------------------------------------------------------------
        //Create Health message text
        //----------------------------------------------------------------------------------------
        GameObject    healthMessageObject = new GameObject("Healthbar_Message", typeof(Text));
        RectTransform messageRect         = healthMessageObject.GetComponent <RectTransform>();

        healthMessageObject.transform.SetParent(textPanelObject.transform);
        healthMessageObject.transform.localPosition = Vector3.zero;

        Stretch(messageRect);

        Text text = healthMessageObject.GetComponent <Text>();

        text.font = sd.fonts[myFontTheme].fontFile;

        text.fontSize  = fontSize;
        text.alignment = TextAnchor.MiddleCenter;

        Message = text;
        //---------------------------------------------------------------------------------------
        //Create Critical message text
        //----------------------------------------------------------------------------------------

        GameObject    criticalMessageObject = new GameObject("Healthbar_Critical", typeof(Text));
        RectTransform criticalRect          = criticalMessageObject.GetComponent <RectTransform>();

        criticalMessageObject.transform.SetParent(criticalPanelObject.transform);

        Stretch(criticalRect);

        criticalMessageObject.transform.localPosition = Vector3.zero;

        Text criticalText = criticalMessageObject.GetComponent <Text>();

        criticalText.font = sd.fonts[myFontTheme].fontFile;


        criticalText.fontSize  = fontSize;
        criticalText.alignment = TextAnchor.MiddleCenter;

        Critical         = criticalText;
        Critical.text    = criticalMessage;
        Critical.enabled = true;

        //----------------------------------------------------------------------------------------
        //Check sprite dimensions and resize them
        //----------------------------------------------------------------------------------------

        //Assign proper frame sprite
        frame.sprite = sd.sprites[myTheme].HealthBar_Frame;

        //Scale it
        Vector2 frameDimensions = new Vector2(frame.sprite.bounds.size.x, frame.sprite.bounds.size.y);
        Vector3 frameScale      = new Vector3(frameDimensions.x, frameDimensions.y, 0.1F);

        barFrameRect.transform.localScale = frameScale;

        //Assign proper bar sprite
        bar.sprite = sd.sprites[myTheme].GreenBar;

        //Scale it
        Vector2 barDimensions = new Vector2(bar.sprite.bounds.size.x, bar.sprite.bounds.size.y);
        Vector3 barScale      = new Vector3(barDimensions.x, barDimensions.y, 0.1F);

        barRect.transform.localScale = barScale;


        //Set healthNormalized
        healthNormalized = health / 10;
    }