Unity3d-C#格式化字符串

当利用Write和WriteLine方法输入的时候,可以对字符串进行格式化输出,什么是格式化输出呢?
Console.WriteLine(“两个数相加{0}+{1}={2}”,2,3,5);
Console.WriteLine(格式字符串(含替代标记),替换值0,替换值1,替换值2,······);

多重标记和值


Unity-C#关键字不能用来做变量名

1 2 3 4 5 6 7
abstract const extern int out short typeof
as continue false interface override sizeof uint
base decimal finally internal params stackalloc long
bool default fixed is private static unchecked
break delegate float lock protected string unsafe
byte do for long public struct ushort
case double foreach namespace readonly switch using
catch else goto new ref this virtual
char enum if null retrun throw void
checked event implicit object sbyte true volatile
class explicit in operator sealed try while

关键字不能被用来做变量名或者其他形式的标识符,除非以@字符开始。
所有C#关键字全部由小写字母组成

Unity-C#命名规范

标识符是一种字符串,用来命名如变量方法、参数和程序结构等。

命名组成=首字符+后续字符

首字符 后续字符
允许 a-z, A-Z, _, @ a-z, A-Z, _, 0-9
不允许 0-9 @

1.字母 下划线 可以用在任何位置
2.数字不能放在首位
3.@字符只能放在标识符的首位

Unity-C#编写第一个程序

我的文章

步骤

1:打开Visual Studio
2:文件-新建-项目-Visual C#-控制台应用
3:名称命名为:01学习VS(命名支持中文英文,但不能包含的字符有:;(/?:&*“<>|#%;;)),位置可任意放到其他D盘,然后点确定
4:按照以下代码写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01学习VS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world");
}
}
}

5.按Ctrl+F5弹出命令控制台,你会看到输入的代码Hello world

总结

刚开始学习编程的时候先不要问为什么,先模仿着来做,多练习,等累积到一定的经验之后再回首来看以往的问题即可迎刃而解。

Unity3d连接MySql查询数据,在UI显示数据

前言

Unity3d通过连接MySql可以查询自己想要的数据,并在UI中显示。
需要用到的软件:
(1)Unity3d 2019.3.0a8
(2)Visual Studio 2017
(3)MySQL Server 5.7
(4)Navicat Premium

若没有安装软件的小伙伴可以到官网下载:
(1)和(2)https://unity.cn 安装的Unity3d的时候会连着安装Visual Studio 2017
(3)https://dev.mysql.com/downloads/windows/installer/8.0.html 向下滑动
(4)https://www.navicat.com.cn/store/navicat-premium

制作步骤

(1)使用Navicat Premium新建一个MySql文件,新建数据库命名为test,新建jiudian_table表格

(2)打开Unity3d,导入MySql包

(3)右击创建Canvas-创建空物体-在空物体下面新建Text文本,将Text文本做成预制体
(4)打开Visual Studio 2017输入代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MySql.Data.MySqlClient;
using System.Data;

public GameObject plan;//生成地方
public GameObject obj;//生成对象
void Start()
{
Jiud();
}

void Jiud()
{
MySqlDataReader reader = DBtest.ins.Query($"select * from jiudian_table");
while (reader.Read())
{
string name= reader.GetString(1);
GameObject go = Instantiate(obj, plan.transform);//实例化=对象,生成位置
go.transform.Find("Text").GetComponent<Text>().text = name;//获取Plan下的Text文本

}
reader.Close();
}

效果如下图所示

总结:若有改进的地方或不对的地方,请多多海涵和指教。若想深入学习和指教请加我扣扣:1446846475,一起来学习哈~

第二篇文章

我的文章

1
2
3
hello world
print("helloworld");
工工工工工工工工工工工工工工工工工工工工工工

表格插入
ABCD | EFGH | IGKL
—–|——|—-
a | b | c
d | e | f
g | h | i