Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.81 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.UI;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10.  
  11. public class Players : MonoBehaviour
  12. {
  13.  
  14.     public Camera CameraPrincipal;
  15.     public InputField InputBot;
  16.     public Button SetButton;
  17.     public Button DelButton;
  18.     public int BotID;
  19.     public List<string> BotTokenList;
  20.     public List<string> BotNameList;
  21.     public List<InputField> BotTokenInputList;
  22.     public List<InputField> BotNameInputList;
  23.     public List<Button> SetButtonList;
  24.     public List<Button> DelButtonList;
  25.     public string[] Lines;
  26.     public bool SetNotLoad;
  27.     public bool FirstFile;
  28.     public bool LoadNotSet;
  29.     public Image Scroll;
  30.    
  31.     //public GameObject Areia;
  32.    
  33.     // Use this for initialization
  34.     void Start()
  35.     {
  36.         BotID = 1;
  37.         FirstFile = false;
  38.        
  39.     }
  40.  
  41.     // Update is called once per frame
  42.     void Update()
  43.     {
  44.  
  45.         if (Input.GetKeyDown(KeyCode.Escape))
  46.         {
  47.             Application.Quit();
  48.         }
  49.        
  50.         if (Input.GetKeyDown(KeyCode.F12))
  51.         {
  52.             Screen.SetResolution(1280,720,false);
  53.         }
  54.  
  55.         /*if (CameraPrincipal.GetComponent<TwitchChatExample>().msgString == "!up")
  56.         {
  57.             transform.position = new Vector3(transform.position.x,transform.position.y+1,0);
  58.             CameraPrincipal.GetComponent<TwitchChatExample>().msgString = "";
  59.         }
  60.        
  61.         if (CameraPrincipal.GetComponent<TwitchChatExample>().msgString == "!down")
  62.         {
  63.             transform.position = new Vector3(transform.position.x,transform.position.y-1,0);
  64.             CameraPrincipal.GetComponent<TwitchChatExample>().msgString = "";
  65.         }
  66.        
  67.         if (CameraPrincipal.GetComponent<TwitchChatExample>().msgString == "!left")
  68.         {
  69.             transform.position = new Vector3(transform.position.x-1,transform.position.y,0);
  70.             CameraPrincipal.GetComponent<TwitchChatExample>().msgString = "";
  71.         }
  72.        
  73.         if (CameraPrincipal.GetComponent<TwitchChatExample>().msgString == "!right")
  74.         {
  75.             transform.position = new Vector3(transform.position.x+1,transform.position.y,0);
  76.             CameraPrincipal.GetComponent<TwitchChatExample>().msgString = "";
  77.         }
  78.  
  79.         if (Input.GetMouseButton(0))
  80.         {
  81.             Instantiate(Areia, Camera.main.ScreenToWorldPoint(Input.mousePosition - transform.position),
  82.                 Quaternion.identity);
  83.         }
  84.  
  85.         //Ray Raio = Camera.main.ScreenPointToRay(Input.mousePosition);
  86.         RaycastHit2D Hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  87.  
  88.         if (Input.GetKey("w"))
  89.         {
  90.             transform.GetComponent<Rigidbody>().velocity = new Vector3(0,0,10); //(new Vector3(0, 0, 5));
  91.             //transform.Translate(0, 0, 1 * Time.deltaTime);
  92.         }
  93.  
  94.         if (Input.GetMouseButton(1))
  95.         {
  96.             if (Hit.transform.tag == "Areia")
  97.             {
  98.                 Destroy(Hit.transform.gameObject);
  99.             }
  100.             else
  101.             {
  102.                 print("Nada");
  103.             }
  104.         }*/
  105.     }
  106.  
  107.     public void AdicionarBot()
  108.     {
  109.         Scroll.rectTransform.sizeDelta = new Vector2(460,Scroll.rectTransform.sizeDelta.y+30);
  110.        
  111.         InputField InputToken = Instantiate(InputBot, new Vector3(640, 360, 0), Quaternion.identity);
  112.         InputToken.transform.SetParent(GameObject.Find("BotPanel").transform);
  113.         InputToken.transform.name = "BotToken "+BotID;
  114.         InputToken.GetComponentInChildren<Text>().text = "Bot code.";
  115.         //BotTokenList.Add("");
  116.         BotTokenInputList.Add(InputToken);
  117.        
  118.         InputField InputName = Instantiate(InputBot, new Vector3(640, 360, 0), Quaternion.identity);
  119.         InputName.transform.SetParent(GameObject.Find("BotPanel").transform);
  120.         InputName.transform.name = "BotName "+BotID;
  121.         InputName.GetComponentInChildren<Text>().text = "Bot name.";
  122.         //BotNameList.Add("");
  123.         BotNameInputList.Add(InputName);
  124.        
  125.         Button SetB = Instantiate(SetButton,new Vector3(0,0,0),Quaternion.identity);
  126.         SetB.gameObject.SetActive(true);
  127.         SetB.transform.SetParent(GameObject.Find("ButtonPanel").transform);
  128.         SetB.transform.name = "SetButton " + BotID;
  129.         SetButtonList.Add(SetB);
  130.        
  131.         Button DelB = Instantiate(DelButton,new Vector3(0,0,0),Quaternion.identity);
  132.         DelB.gameObject.SetActive(true);
  133.         DelB.transform.SetParent(GameObject.Find("ButtonPanel").transform);
  134.         DelB.transform.name = "DelButton " + BotID;
  135.         DelButtonList.Add(DelB);
  136.  
  137.         BotID++;
  138.     }
  139.  
  140.     public void SetBotInfo()
  141.     {
  142.  
  143.         SetNotLoad = true;
  144.         print(BotID); //Here i choose to create 3 bots, and the ID = 1,2,3 and (4) but it's not a problem.
  145.         LoadConfig(); // Here it loads the txt file which has the configurations.
  146.         print(BotID);
  147.         // But after the LoadConfig() finish, the ID is = 7, but there is no BotID++ or BotID += 1 in the LoadConfig(),
  148.         // and I get a error for having more than it should.
  149.  
  150.         if (SetNotLoad == true)
  151.         {
  152.             if (Lines.Length == 0)
  153.             {
  154.                 if (BotTokenList.Count == 0)
  155.                 {
  156.                     int i2 = 1;
  157.                     while (i2 != BotID)
  158.                     {
  159.                         BotTokenList.Add("");
  160.                         i2++;
  161.                     }
  162.                 }
  163.  
  164.                 if (BotNameList.Count == 0)
  165.                 {
  166.                     int i2 = 1;
  167.                     while (i2 != BotID)
  168.                     {
  169.                         BotNameList.Add("");
  170.                         i2++;
  171.                     }
  172.                 }
  173.             }
  174.  
  175.             if (LoadNotSet == true)
  176.             {
  177.  
  178.                 while (BotTokenList.Count != BotID - 1)
  179.                 {
  180.                     BotTokenList.Add("");
  181.                     print("Erro");
  182.                 }
  183.  
  184.                 while (BotNameList.Count != BotID - 1)
  185.                 {
  186.                     BotNameList.Add("");
  187.                 }
  188.  
  189.             }
  190.  
  191.             string[] BT = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name.Split(new string[] {" "}, StringSplitOptions.None);
  192.             print(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name);
  193.             GameObject CurrentBotCode = GameObject.Find("BotToken " + BT[1]);
  194.             GameObject CurrentBotName = GameObject.Find("BotName " + BT[1]);
  195.             InputField CodeText = CurrentBotCode.GetComponent<InputField>();
  196.             InputField NameText = CurrentBotName.GetComponent<InputField>();
  197.             print(CurrentBotCode.transform.name);
  198.             print(CurrentBotName.transform.name);
  199.             print(CodeText.text);
  200.  
  201.             //BotTokenList[int.Parse(BT[1])] = CodeText.text;
  202.             //BotNameList[int.Parse(BT[1])] = NameText.text;
  203.  
  204.             StreamWriter BotFile = new StreamWriter("BotFile.txt");
  205.  
  206.  
  207.             if (Lines.Length != 0)
  208.             {
  209.                 FirstFile = false;
  210.                 LoadNotSet = true;
  211.             }
  212.  
  213.             int i = 1;
  214.             while (i != BotID)
  215.             {
  216.                 if (Lines.Length != 0)
  217.                 {
  218.                     if (i == int.Parse(BT[1]))
  219.                     {
  220.                         BotFile.WriteLine(i + " " + CodeText.text + " " + NameText.text);
  221.                         BotTokenList[int.Parse(BT[1]) - 1] = CodeText.text;
  222.                         print(BotTokenList[int.Parse(BT[1]) - 1]);
  223.                         BotNameList[int.Parse(BT[1]) - 1] = NameText.text;
  224.                         print("Aqui");
  225.                     }
  226.                     else
  227.                     {
  228.                         if (i != int.Parse(BT[1]))
  229.                         {
  230.                             print(i);
  231.                             if (BotTokenList[i - 1] != "")
  232.                             {
  233.                                 BotFile.WriteLine(i + " " + BotTokenList[i - 1] + " " + BotNameList[i - 1]);
  234.                             }
  235.                             else
  236.                             {
  237.                                 BotFile.WriteLine(i);
  238.                             }
  239.                         }
  240.                     }
  241.                 }
  242.                 else
  243.                 {
  244.  
  245.                     if (FirstFile == false)
  246.                     {
  247.                         BotFile.WriteLine(i);
  248.                     }
  249.  
  250.                     if (i == BotID - 1)
  251.                     {
  252.                         FirstFile = true;
  253.                     }
  254.                 }
  255.                 i++;
  256.             }
  257.             SetNotLoad = false;
  258.             BotFile.Close();
  259.             if (FirstFile == true)
  260.             {
  261.                 FirstFile = false;
  262.                 LoadNotSet = true;
  263.                 SetBotInfo();
  264.             }
  265.         }
  266.     }
  267.  
  268.     public void LoadConfig()
  269.     {
  270.         Lines = File.ReadAllLines("BotFile.txt");
  271.        
  272.         if (SetNotLoad == false)
  273.         {
  274.             LoadNotSet = true;
  275.         }
  276.        
  277.         if (Lines.Length != 0)
  278.         {
  279.             if (BotTokenList.Count == 0)
  280.             {
  281.                 int i2 = 0;
  282.                 while (i2 != Lines.Length)
  283.                 {
  284.                     BotTokenList.Add("");
  285.                     AdicionarBot();
  286.                     i2++;
  287.                 }
  288.             }
  289.        
  290.             if (BotNameList.Count == 0)
  291.             {
  292.                 int i2 = 0;
  293.                 while (i2 != Lines.Length)
  294.                 {
  295.                     BotNameList.Add("");
  296.                     i2++;
  297.                 }
  298.             }
  299.            
  300.             //BotTokenList = new List<string>(Lines.Length);
  301.             //BotNameList = new List<string>(Lines.Length);
  302.             if (SetNotLoad == false)
  303.             {
  304.                 int i = 0;
  305.                 while (i != Lines.Length)
  306.                 {
  307.                     //Calcular o numero da linha para a lista.
  308.                     if (Lines[i] != (i + 1).ToString())
  309.                     {
  310.                         //Adicionar Input fields carregados com a informação abaixo e usar a bool (SetNotLoad) para usar no Set.
  311.                         print(Lines[i]);
  312.                         BotTokenList[i] = Lines[i].Split(' ')[1];
  313.                         BotTokenInputList[i].text = BotTokenList[i];
  314.                         BotNameList[i] = Lines[i].Split(' ')[2];
  315.                         BotNameInputList[i].text = BotNameList[i];
  316.  
  317.                     }
  318.                     print(Lines[i]);
  319.                     i++;
  320.                 }
  321.             }
  322.         }
  323.     }
  324.  
  325.     public void ResetInputs()
  326.     {
  327.         if (BotTokenInputList.Count != 0 && BotNameInputList.Count != 0)
  328.         {
  329.             int i = 0;
  330.             while (i != BotTokenInputList.Count)
  331.             {
  332.                 BotTokenInputList[i].text = "";
  333.                 BotNameInputList[i].text = "";
  334.                 i++;
  335.             }
  336.         }
  337.     }
  338.  
  339.     public void RemoverInput()
  340.     {
  341.         string[] BT = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name.Split(new string[] {" "}, StringSplitOptions.None);
  342.         Lines = File.ReadAllLines("BotFile.txt");
  343.        
  344.         if (BotTokenInputList.Count != 0 && BotNameInputList.Count != 0)
  345.         {
  346.  
  347.             Scroll.rectTransform.sizeDelta = new Vector2(460, Scroll.rectTransform.sizeDelta.y - 30);
  348.             Destroy(BotTokenInputList[int.Parse(BT[1]) - 1].gameObject);
  349.             Destroy(BotNameInputList[int.Parse(BT[1]) - 1].gameObject);
  350.             Destroy(SetButtonList[int.Parse(BT[1]) - 1].gameObject);
  351.             Destroy(DelButtonList[int.Parse(BT[1]) - 1].gameObject);
  352.             BotTokenInputList.RemoveAt(int.Parse(BT[1]) - 1);
  353.             BotNameInputList.RemoveAt(int.Parse(BT[1]) - 1);
  354.             if (BotTokenList.Count != 0 && BotNameList.Count != 0)
  355.             {
  356.                 BotTokenList.RemoveAt(int.Parse(BT[1]) - 1);
  357.                 BotNameList.RemoveAt(int.Parse(BT[1]) - 1);
  358.             }
  359.             SetButtonList.RemoveAt(int.Parse(BT[1]) - 1);
  360.             DelButtonList.RemoveAt(int.Parse(BT[1]) - 1);
  361.             BotID--;
  362.  
  363.             int i = 0;
  364.             while (i != BotTokenInputList.Count)
  365.             {
  366.                 BotTokenInputList[i].transform.name = "BotToken " + (i + 1);
  367.                 BotNameInputList[i].transform.name = "BotName " + (i + 1);
  368.                 SetButtonList[i].transform.name = "SetButton " + (i + 1);
  369.                 DelButtonList[i].transform.name = "DelButton " + (i + 1);
  370.                 i++;
  371.             }
  372.             if (LoadNotSet == true)
  373.             {
  374.                 StreamWriter BotFile = new StreamWriter("BotFile.txt");
  375.                 if (Lines.Length != 0)
  376.                 {
  377.                     i = 1;
  378.                     while (i != Lines.Length)
  379.                     {
  380.                         if (i < int.Parse(BT[1]) - 1)
  381.                         {
  382.                             BotFile.WriteLine(i + " " + BotTokenList[i - 1] + " " + BotNameList[i - 1]);
  383.                         }
  384.                         else
  385.                         {
  386.                             if (i >= int.Parse(BT[1]) - 1)
  387.                             {
  388.                                 BotFile.WriteLine(i + " " + BotTokenList[i - 1] + " " + BotNameList[i - 1]);
  389.                             }
  390.                         }
  391.                         i++;
  392.                     }
  393.                 }
  394.                 BotFile.Close();
  395.             }
  396.         }
  397.     }
  398.  
  399.     public void ApagarData()
  400.     {
  401.         StreamWriter BotFile = new StreamWriter("BotFile.txt");
  402.     }
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement