Advertisement
aidenm125

UI Issues

Jan 18th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class QuestsManager1 : MonoBehaviour
  7. {
  8.     public Font font;
  9.     public Color fontColor;
  10.     public List<Text> questText;
  11.     public Canvas c;
  12.     public int numberOfQuests;
  13.     public List<string> questsString;// = new string[numberOfTexts];
  14.     public Text parentText;
  15.  
  16.     public List<int> activeQuests;
  17.  
  18.     public RectTransform initialTextPos;
  19.     float textPosY;
  20.     float textPosX;
  21.     float textWidth = 300;
  22.     float textHeight;
  23.  
  24.     void Update ()
  25.     {
  26.         //still buggy, fix.  Probs put in the addquests function in the for loop which looks identical.  Save on cpu or whatevs, and looks better in code
  27.         float f = 0;
  28.         for(int i = 0; i < numberOfQuests; i++)
  29.         {          
  30.             int j = questText.Count;
  31.             f += questText[i].rectTransform.rect.height;
  32.             if (f == 0)
  33.             {
  34.                 f = 35.8f;
  35.             }
  36.             Vector2 parentSizeDelta = new Vector2(parentText.GetComponent<RectTransform>().sizeDelta.x, f);
  37.             parentText.GetComponent<RectTransform>().sizeDelta = parentSizeDelta;
  38.         }
  39.  
  40.         if(Input.GetKeyDown(KeyCode.A))
  41.         {
  42.             AddQuest("Find mister Chocolate Face");
  43.         }
  44.         if(Input.GetKeyDown(KeyCode.D))
  45.         {
  46.             AddQuest("Eat 10 clocks");
  47.         }
  48.         if(Input.GetKeyDown(KeyCode.L))
  49.         {
  50.             AddQuest("gvcdsahbqjkdfghvsjvfgh sbhjhbfbhsde kweydfgeskufygdfufydsfuydvfkbs f7i6g3i734grwfuwei8fgwefisey fiugudsfygdsvfbdzmfgsdyfsdfgd m,ukfhdskfg dsulngdfkjfhdkfg dffkdfjgf");
  51.         }
  52.         //Read the comment above the functions
  53.         AssignQuestTexts();
  54.     }
  55.  
  56.     //Change this to only be called once every time a quest is added/removed
  57.     public void AssignQuestTexts()
  58.     {
  59.         for(int i = 0; i < numberOfQuests; i++)
  60.         {
  61.             questText[i].text = questsString[i];
  62.         }
  63.     }
  64.  
  65.     public void AddQuest(string quest)
  66.     {
  67.         numberOfQuests++;
  68.         questsString.Add(quest);
  69.  
  70.         GameObject g = GameObject.CreatePrimitive(PrimitiveType.Capsule);
  71.  
  72.         g.AddComponent<RectTransform>();
  73.         g.AddComponent<CanvasRenderer>();
  74.         g.AddComponent<ContentSizeFitter>();
  75.         g.AddComponent<Text>();
  76.         g.GetComponent<Text>().font = font;
  77.         g.GetComponent<Text>().color = fontColor;
  78.         g.GetComponent<Text>().fontSize = 16;
  79.         g.GetComponent<Text>().alignment = TextAnchor.MiddleLeft;
  80.         g.name = "Quest Text";
  81.         g.tag = "QuestText";
  82.         g.transform.SetParent(parentText.transform);
  83.  
  84.         Destroy(g.GetComponent<CapsuleCollider>());
  85.         Destroy(g.GetComponent<MeshRenderer>());
  86.  
  87.         ContentSizeFitter cst = g.GetComponent<ContentSizeFitter> ();
  88.         cst.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
  89.         RectTransform rt = g.GetComponent<RectTransform>();
  90.  
  91.         Vector2 size = new Vector2 (textWidth, 0);
  92.         Vector3 scale = new Vector3(1,1,1);
  93.         Vector2 pos;
  94.  
  95.         rt.sizeDelta = size;
  96.         rt.localScale = scale;
  97.  
  98.         Vector2 max = new Vector2(0.5f, 0.5f);
  99.         Vector2 min = new Vector2(0.5f, 0.5f);
  100.         rt.anchorMax = max;
  101.         rt.anchorMin = min;
  102.  
  103.         questText.Add(g.GetComponent<Text>());
  104.  
  105.         textPosX = initialTextPos.localPosition.x;
  106.         textPosY = initialTextPos.localPosition.y;
  107.  
  108.         float finalPosY;// = textPosY;
  109.         for (int i = 0; i < numberOfQuests; i++)
  110.         {
  111.             textPosY -= questText [i].rectTransform.rect.height; //  this line changes the y postion of the textwe
  112.  
  113.             if (numberOfQuests == 1)
  114.             {
  115.                 //float finalPosY = textPosY;
  116.                 finalPosY = (numberOfQuests + textPosY);
  117.                 pos = new Vector2(textPosX, finalPosY);
  118.                 rt.localPosition = pos;
  119.             }
  120.             else //if(numberOfQuests > 1)
  121.             {
  122.                 Debug.Log(g.GetComponent<Text>().rectTransform.rect.height);
  123.                 finalPosY = ((-10 * (numberOfQuests - 1)) + textPosY);
  124.                 pos = new Vector2(textPosX, finalPosY);
  125.                 rt.localPosition = pos;
  126.             }
  127.         }
  128.     }
  129.  
  130.     public void RemoveQuest(Text quest)
  131.     {
  132.         numberOfQuests--;
  133.         //questID system should be here, don't use Text ffs
  134.         questText.Remove(quest);
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement