(1) UI-Text (TMP),TitleText
(2) 给Text (TMP)添加Recipients.cs组件
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
-
- public class Recipients : MonoBehaviour
- {
- public TextMeshProUGUI TitleText;//在Unity赋值
-
- //接收文本数据
- public void initData(string titleText)
- {
- //在TitleText.text文本中显示接收到的文本内容
- TitleText.text = titleText;
- }
- }
(3) 将UI-Text (TMP)制成预制体,并从Hierarchy中删除该预制体
(1) Sender.cs
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor.Experimental.GraphView;
- using UnityEngine;
-
- public class Sender : MonoBehaviour
- {
- public Recipients titleText;
- // Start is called before the first frame update
- void Start()
- {
- CreateTitleText();
- }
-
- public void CreateTitleText()
- {
- Recipients t = Instantiate(titleText, this.transform);//调用Recipients.cs实例上的initData方法,并传递一个字符串参数
- t.initData("传递方直接传递文本");
- }
- }
(2) 赋值
(1) 声明一个字符串
string strs; //string:类型; strs:变量名
(2) 传递
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Sender : MonoBehaviour
- {
- public Recipients titleText;//接收方的接收文本
- void Start()
- {
- string str = "直接传输";
- CreateTitleText(str);
- }
-
- public void CreateTitleText(string transmission)//transmission是自取的名字
- {
- Recipients t = titleText;
- t.initData(transmission);
- }
- }
(3) 接收
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
-
- public class Recipients : MonoBehaviour
- {
- public TextMeshProUGUI TitleText;//在Unity中赋值一个文本(用于接收文本信息)
- public void initData(string titleText)//接收的文本命名为titleText
- {
- TitleText.text = titleText;//在TitleText.text文本中显示接收到的文本内容
- }
- }
注意:两个脚本中都要在Unity给文本赋值
string[] strs = new string[3]; //声明一个字符串数组,且初始化它的长度为3
(1) string:类型; []:数组; strs:变量名
(2) 数组需要初始化之后 才能使用。
(3) 初始化的方法:类型+[] + 变量名 = new string[3];
(4) 调用字符串的数据的实例
发送方:
- public Nodes node;
-
-
- void Start()
- {
-
- string[] strs = new string[3]; //声明一个包含三个字符串元素的数组
- //string:类型; []:数组; strs:变量名
-
- strs[0] = "strs这个数组中的第一个数据序号为0";//将数组的第一个元素(索引为0)设置为指定的字符串内容
- CreateNode(strs[0]);//调用方法,传递整个字符串数组
- }
-
- public void CreateNode(string strs)
- {
- Nodes n = Instantiate(node, this.transform);//创建一个node预制体的实例,并将这个实例赋值给变量n
- this.transform:新实例将作为当前对象(脚本所挂载的物体)的子对象
-
- //调用Nodes.cs实例上的initData方法,并传递一个字符串参数
- n.initData(strs); //n:node预制体实例;
- //n.initData();:调用预制体本身携带的公开的方法(在Nodes脚本中创建的方法)
- //括号内要写类型与接收方要求的相同的数据
- }
-
接收方:
- public class Nodes : MonoBehaviour
-
- public TextMeshProUGUI nodeTitle;
-
- public void initData(string titleText)//初始化节点的数据(类型是String;名字是titleText)
- //把接收到的文本命名为titleText
- //初始化节点的数据,接收一个字符串类型的参数titleText作为标题文本
- {
- //使用接收到的文本titleText更新nodeTitle文本组件的内容
- nodeTitle.text = titleText;//将nodeTitle文本组件的内容设置为接收到的titleText(在nodeTitle.text文本中显示接收到的文本内容)
- }
(5) 优化后的方法
- public GameObject nodePrefab; // 在Unity编辑器中设置的预制体
-
- // Start is called before the first frame update
- void Start()
- {
- string[] strs = new string[3]; // 声明一个包含三个字符串元素的数组
- strs[0] = "strs这个数组中的第一个数据序号为0"; // 设置数组的第一个元素
- // ... 可以设置strs[1]和strs[2]
- CreateNode(strs); // 调用方法,传递整个字符串数组
- }
- public void CreateNode(string[] strs) // 字符串数组
- {
- GameObject newNode = Instantiate(nodePrefab, this.transform); // 创建预制体的实例
- Nodes n = newNode.GetComponent<Nodes>(); // 获取Nodes组件
- if (n != null) // 检查是否成功获取到了组件
- {
- n.initData(strs); // initData方法接受一个字符串数组作为参数
- }
- else
- {
- Debug.LogError("No Nodes component found on the instantiated prefab!");
- }
- }
(1) 声明并 初始化变量
List<string> strs_02 = new List<string>();//声明并初始化一个名为strs_02,类型为List<string> 的变量
(2) 调用变量:
类型一:调用数组中的变量
strs_02.Add(strs[0]);
strs_02:变量名
Add:调用
(strs[0]):strs数组中的索引号为0的值
类型2:直接调用字符串
strs_02.Add("strs[0]");
strs_02:变量名
Add:调用
("strs[0]"):输出的是双引号内的文字
- // 将strs数组的第一个元素添加到strs_02列表中
- strs_02.Add(strs[0]);//第一个Add,后面跟的就是strs_02中的序号为0的数据。在这里调用的是strs数组中序号为0的字符串
-
- // 添加字符串"strs[0]"到strs_02列表中,而不是数组strs的某个元素
- strs_02.Add("strs[0]");//注意:这里添加的是字面上的字符串"strs[0]",而不是strs数组中的某个值
(3) 实例:
繁琐但超小白注释:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Test : MonoBehaviour
- {
- public Nodes node;
- // Start is called before the first frame update
- void Start()
- {
-
- string[] strs = new string[3]; //声明一个字符串数组,并分配了3个元素的空间
- //string:类型; []:数组; strs:变量名
- //数组需要初始化之后 才能使用。初始化的方法:……= new string[3];
- //这一步已经完成了初始化
- //将数组的第一个元素(索引为0)设置为指定的字符串内容
- strs[0] = "strs这个数组中的第一个数据序号为0";//给数组的第一个位置(索引0)赋值
- strs[1] = "测试序号1";
- strs[2] = "测试序号2";
-
- // 声明并初始化一个空的List<string>类型的变量,名字为strs_02
- List<string> strs_02 = new List<string>();
-
- // 将strs数组的第一个元素添加到strs_02列表中
- strs_02.Add(strs[0]);//第一个Add,后面跟的就是strs_02中的序号为0的数据。在这里调用的是strs数组中序号为0的字符串
-
- // 添加字符串"strs[0]"到strs_02列表中,而不是数组strs的某个元素
- strs_02.Add("strs[0]");//注意:这里添加的是字面上的字符串"strs[0]",而不是strs数组中的某个值
-
- // 调用CreateNode方法,传递strs_02列表中的第一个元素作为参数
- CreateNode(strs_02[0]);//传递的是"strs这个数组中的第一个数据序号为0"这段文字
-
- // 调用CreateNode方法,传递strs_02列表中的第二个元素作为参数
- CreateNode(strs_02[1]);//传递的是strs_02中的序号为1的数据,即:"strs[0]"这段文字
-
- }
-
- public void CreateNode(string strs)
- {
- Nodes n = Instantiate(node, this.transform);//创建一个node预制体的实例,并将这个实例赋值给变量n
- //node:要实例化的预制体;this.transform:新实例将作为当前对象(脚本所挂载的物体)的子对象
- //调用Nodes.cs实例上的initData方法,并传递一个字符串数组的参数
- n.initData(strs); //n:node预制体实例;
- //n.initData();:调用预制体本身携带的公开的方法(在Nodes脚本中创建的方法)
- //括号内要写类型与接收方要求的相同的数据
- }
- }
普通小白注释
- public Nodes nodePrefab;//Nodes.cs中的node预制体
-
- void Start()
- {
- string[] strs = new string[3]; // 声明并初始化字符串数组
- strs[0] = "strs这个数组中的第一个数据序号为0";//赋值
- strs[1] = "测试序号1";
- strs[2] = "测试序号2";
-
- List<string> strs_02 = new List<string>(); // 声明并初始化List<string>
- strs_02.Add(strs[0]); // 添加数组的第一个元素到列表
- strs_02.Add("strs[0]"); // 添加字符串"strs[0]"到列表
-
- // 调用CreateNode方法,传递strs_02列表中的元素作为参数
- CreateNode(strs_02[0]); // 传递列表中的第一个元素
- CreateNode(strs_02[1]); // 传递列表中的第二个元素
- }
-
- public void CreateNode(string data)
- {
- Nodes n = Instantiate(nodePrefab, this.transform); // 实例化Nodes预制体
- n.initData(data); // 调用initData方法,传递数据给节点进行初始化
- }
直接上手试版本:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Test : MonoBehaviour
- {
- public Nodes node;
- void Start()
- {
- string[] strs = new string[3];
- strs[0] = "输出的是数组中的文字";
- strs[1] = "测试序号1";
- strs[2] = "测试序号2";
-
- List<string> strs_02 = new List<string>();
- strs_02.Add(strs[0]);
- strs_02.Add("输出的是Add.后面括号里面双引号里面的文字");
-
- CreateNode(strs_02[0]);
- CreateNode(strs_02[1]);
- }
- public void CreateNode(string data)
- {
- Nodes n = Instantiate(node, this.transform);
- n.initData(data);
- }
- }
Dictionary<int,string> strs3= new Dictionary<int,string>();//声明并初始化字典,名字是strs3
Dictionary
作用:加了索引的列表。给每个数据都取了个名字,方法查找、修改、调用数据
解释:
Dictionary:字典
添加数据:
strs3.Add(1001, "文本内容");
调用
TransferTitleText(strs3[1001]);