Advertisement
Guest User

Unity code

a guest
Dec 2nd, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.58 KB | None | 0 0
  1.    /// <summary>
  2.     /// The enemy information UI if clicked on enemy
  3.     /// </summary>
  4.     public GameObject hoverGUI;
  5.  
  6.     public static void SpawnHoverGUI_Enemy(Tile tile) {
  7.  
  8.         // Enable canvas
  9.         GamePlay.EnableCanvas(instance.hoverGUI.GetComponent<Canvas>());
  10.  
  11.         // Config
  12.         int width = 400;
  13.         int fontSize = 20;
  14.  
  15.         // Remove appended gameobjects
  16.         foreach (Transform child in instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().transform) {
  17.             if (child.name == "Appended") {
  18.                 GameObject.Destroy(child.gameObject);
  19.             }
  20.         }
  21.  
  22.         // START Description
  23.  
  24.         // Activate before making a copy
  25.         instance.hoverGUI.GetComponent<HoverGUI_Components>().GetTemplateText().SetActive(true);
  26.  
  27.         // Make copy of text object and parent it
  28.         GameObject EnemyTextObject = Instantiate(instance.hoverGUI.GetComponent<HoverGUI_Components>().GetTemplateText());
  29.         EnemyTextObject.transform.SetParent(instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().transform);
  30.         EnemyTextObject.name = "Appended";
  31.  
  32.         // Adjust size of hover element
  33.         EnemyTextObject.GetComponentInChildren<TextMeshProUGUI>().GetComponent<RectTransform>().sizeDelta = new Vector2(width,
  34.             EnemyTextObject.GetComponentInChildren<TextMeshProUGUI>().GetComponent<RectTransform>().sizeDelta.y);
  35.  
  36.         // Adjust font size
  37.         EnemyTextObject.GetComponentInChildren<TextMeshProUGUI>().fontSize = fontSize;
  38.  
  39.         // Set description
  40.         EnemyTextObject.GetComponentInChildren<TextMeshProUGUI>().text = tile.current_enemy.GetDescription();
  41.  
  42.         // END Description
  43.  
  44.         // START Effects
  45.  
  46.         // Activate before making a copy
  47.         instance.hoverGUI.GetComponent<HoverGUI_Components>().GetTemplateGrid().SetActive(true);
  48.  
  49.         // Make copy of grid object and parent it
  50.         GameObject EnemyGridObject_1 = Instantiate(instance.hoverGUI.GetComponent<HoverGUI_Components>().GetTemplateGrid());
  51.         EnemyGridObject_1.transform.SetParent(instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().transform);
  52.         EnemyGridObject_1.name = "Appended";
  53.  
  54.         // Append effects to grid
  55.         for (int i = 0; i < tile.current_enemy.GetNegativeEffects().Count; i++) {
  56.             GameObject button = (GameObject)Instantiate(GamePlay.GetEffectsButtonPrefab());
  57.             button.transform.SetParent(EnemyGridObject_1.GetComponent<GridLayoutGroup>().transform);
  58.  
  59.             button.GetComponent<Image>().sprite = tile.current_enemy.GetNegativeEffects()[i].GetSprite();
  60.             button.GetComponentsInChildren<TextMeshProUGUI>()[1].text = tile.current_enemy.GetNegativeEffects()[i].GetFloorDuration().ToString();
  61.             button.GetComponentsInChildren<TextMeshProUGUI>()[0].text = tile.current_enemy.GetNegativeEffects()[i].GetRoundDuration().ToString();
  62.         }
  63.  
  64.         // TO DO Same with positive effects
  65.  
  66.         // END Effects
  67.  
  68.         Canvas.ForceUpdateCanvases();
  69.  
  70.         // Positioning the hover GUI
  71.         // The offset is used to postion the GUI on the right of the enemy tile.
  72.         float offSet = (instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().rect.width / 2 + (tile.GetComponent<RectTransform>().rect.width / 2)) + 10;
  73.         RectTransform hoverOverRect = tile.GetComponent<RectTransform>();
  74.  
  75.         instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().position = new Vector3(hoverOverRect.position.x + offSet, hoverOverRect.position.y,
  76.             hoverOverRect.position.z);
  77.  
  78.         Canvas.ForceUpdateCanvases();
  79.  
  80.         // Correct positioning if out of bounds
  81.         RectTransform hoverElementRect = instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>();
  82.  
  83.         Debug.Log(hoverElementRect.sizeDelta);
  84.  
  85.         /*float canvasBounds_y = instance.hoverGUI.GetComponent<Canvas>().GetComponent<RectTransform>().localPosition.y / 2;
  86.         float pos_top_bound = instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().localPosition.y +
  87.             (instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().rect.height / 2);
  88.         Debug.Log(canvasBounds_y);
  89.         Debug.Log(pos_top_bound);
  90.  
  91.         LayoutRebuilder.ForceRebuildLayoutImmediate(instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>());
  92.         instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().ForceUpdateRectTransforms();
  93.  
  94.         Debug.Log(instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().localPosition.y);
  95.         Debug.Log(instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().sizeDelta.y);
  96.  
  97.         if (instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().localPosition.y > canvasBounds_y) {
  98.  
  99.             Debug.Log("is bigger");
  100.             float difference_y = instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().localPosition.y - canvasBounds_y;
  101.             Debug.Log(difference_y);
  102.             instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().position = new Vector3(hoverOverRect.position.x + offSet, hoverOverRect.position.y - difference_y - 30,
  103.                 hoverOverRect.position.z);
  104.  
  105.         } else if (instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().localPosition.y < -canvasBounds_y) {
  106.  
  107.             Debug.Log("is smaller");
  108.             float difference_y = instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().localPosition.y + canvasBounds_y;
  109.             Debug.Log(difference_y);
  110.             instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>().position = new Vector3(hoverOverRect.position.x + offSet, hoverOverRect.position.y + difference_y + 30,
  111.                 hoverOverRect.position.z);
  112.         }*/
  113.  
  114.         //Debug.Log(canvasBounds_y);
  115.  
  116.         // Deactivate template objects
  117.         instance.hoverGUI.GetComponent<HoverGUI_Components>().GetTemplateText().SetActive(false);
  118.         instance.hoverGUI.GetComponent<HoverGUI_Components>().GetTemplateGrid().SetActive(false);
  119.  
  120.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement