当前位置: 首页>>代码示例>>C#>>正文


C# Canvas.SetActive方法代码示例

本文整理汇总了C#中Canvas.SetActive方法的典型用法代码示例。如果您正苦于以下问题:C# Canvas.SetActive方法的具体用法?C# Canvas.SetActive怎么用?C# Canvas.SetActive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Canvas的用法示例。


在下文中一共展示了Canvas.SetActive方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateUI

    void CreateUI()
    {
        //---------------Actions---------------------------------------------
        //create canvas and parent it to the agent
        agentCanvas = ((GameObject)Instantiate (agentCanvasPrefab.gameObject, Vector3.zero, Quaternion.identity)).GetComponent<Canvas>();
        agentCanvas.transform.SetParent (this.transform);
        agentCanvas.transform.name = "Action Canvas";
        agent.agentCanvas = agentCanvas;
        //set canvas event camera and position in terms of the agent
        agentCanvas.worldCamera = Camera.main;
        agentCanvas.sortingOrder = 1;
        agentCanvas.transform.localPosition = new Vector3 (0f, 2f, 0f);
        //create buttons
        foreach (AgentActions.ActionData actionData in actionDataList) {
            //bumps up canvas by .3 everytime a new action is added to the list
            agentCanvas.transform.localPosition += new Vector3 (0f, .3f, 0f);
            //create button
            GameObject buttonObject = (GameObject)Instantiate (actionButtonPrefab.gameObject);
            //change name of button
            buttonObject.transform.name = actionData.action.actionName + " Button";
            //set parent to canvas
            buttonObject.transform.SetParent (agentCanvas.transform);
            //zero out button position
            buttonObject.transform.localPosition = Vector3.zero;
            //change text of button
            buttonObject.GetComponentInChildren<Text> ().text = actionData.action.actionName;
            //change action associated with button
            buttonObject.GetComponent<ButtonActionInfo> ().actionData = actionData;
            actionData.button = buttonObject.GetComponent<Button>();
            //change button's onClick
            buttonObject.GetComponent<Button> ().onClick.AddListener (() => {
                buttonObject.GetComponent<ButtonActionInfo> ().SetBattleManagerAction ();});
            //change button colors
            ColorBlock buttonColors = buttonObject.GetComponent<Button> ().colors;
            buttonColors.normalColor = actionData.action.normalColor;
            buttonColors.highlightedColor = actionData.action.highlightedColor;
            buttonColors.pressedColor = actionData.action.pressedColor;
            buttonColors.disabledColor = actionData.action.disabledColor;
            buttonObject.GetComponent<Button> ().colors = buttonColors;
            //change button text color
            if (buttonColors.normalColor != Color.white) {
                //if button color is not white, the text should be white
                buttonObject.GetComponentInChildren<Text> ().color = Color.white;
            } else {
                //else text should be black
                buttonObject.GetComponentInChildren<Text> ().color = Color.black;
            }
            //if action is not usable than disable it
            if (!actionData.usable) {
                buttonObject.GetComponent<Button> ().interactable = false;
            }
            //if action is not known, disable the game object
            if (!actionData.known) {
                buttonObject.SetActive (false);
            }
            //scale button down
            buttonObject.transform.localScale = new Vector3 (1.5f, 1.5f, 1.5f);
            //add button to list
            actionButtonList.Add (buttonObject.GetComponent<Button>());
        }
        //set ui to false
        agentCanvas.SetActive (false);

        //--------------Healthbar-------------------------------------
        //needs a new canvas, set up same way as action button canvas
        agentHUDCanvas = (GameObject)Instantiate (agentHUDCanvasPrefab.gameObject, Vector3.zero, Quaternion.identity);
        agentHUDCanvas.transform.SetParent (this.transform);
        agentHUDCanvas.transform.name = "HUD Canvas";
        //set canvas event camera and position in terms of the agent
        agentHUDCanvas.GetComponent<Canvas> ().worldCamera = Camera.main;
        if(gameObject.GetComponent<TileAgent>().team == BattleManager.CombatTeam.Player)
            agentHUDCanvas.transform.localPosition = new Vector3 (0f,2.3f,0f);
        else
            agentHUDCanvas.transform.localPosition = new Vector3 (0f,1.6f,0f);
        healthbar = (GameObject)Instantiate (healthbarPrefab.gameObject);
        healthbar.transform.SetParent (agentHUDCanvas.transform);
        healthbar.name = "Health Bar";
        //zero out health position
        healthbar.transform.localPosition = Vector3.zero;
        healthbarSlider = healthbar.GetComponent<Slider> ();
        stats = GetComponent<BaseStats>();
        healthbarSlider.maxValue = stats.maxHealth;
        healthbarSlider.minValue = 0;
        healthbarSlider.value = stats.currentHealth;
        //scale healthbar
        healthbar.transform.localScale = new Vector3(1f,1f,0);
    }
开发者ID:DomCristaldi,项目名称:MagesOfBlight,代码行数:87,代码来源:AgentCanvasSpawner.cs


注:本文中的Canvas.SetActive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。