用户名  找回密码
 立即注册
查看: 316|回复: 0

Unity教程:C# 基础

[复制链接]
发表于 2022-3-25 18:47 | 显示全部楼层 |阅读模式
1、Unity脚本基础框架
  1. //脚本中需要使用的Unity库usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;//定义脚本类,继承自MonoBehaviour类publicclassSimpleControl:MonoBehaviour{publicvoidStart(){
  2.                 Debug.Log("Start函数,在游戏开始时运行一次");}voidUpdate(){
  3.                 Debug.Log("Update函数,游戏运行时每一帧运行一次");}}
复制代码
2、C#基础——数据类型
  1. int num1 =1;int[] array ={0,1,2,3,4};float num2 =1.0f;bool isOpen =true;string str ="This is a String!"Vector3 direction =newVector3(1,1,1);
复制代码
3、C#基础——条件
  1. //(1)if条件语句if(a==1){}elseif(a !=2){}else{}//(2)switch条件语句switch(a){case1:case2://执行语句break;case3://执行语句break;default://执行语句break;}
复制代码
4、C#基础——循环
  1. int[] array ={1,2,3,4,5};int[] temp=newint[5];//(1)for循环语句for(int i =0; i <=5; i++){
  2.     temp[i]= array[i];}//(2)while循环语句int j =0;while(j <=5){
  3.         temp[j]= array[j];
  4.     j++;}
复制代码
5、C#基础——函数/方法
  1. //方法定义:访问类型;返回类型;函数名;参数publicintMyFunction(int num1,int num2){return num1 + num2;}//方法调用int a =1, b =2;int c =MyFunction(a, b);
复制代码
6、C#基础——类
  1. usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;//类:一种自己定义的复杂数据类型的模板//对象:用类创建出来的实例publicclassPerson{//类的成员变量publicstring name ="Zhao";privateint age =18;//类的成员函数publicvoidsetName(string str){
  2.                 name = str;}publicintgetAge(){return age;}//类的构造函数publicPerson(){
  3.                 name ="";
  4.                 age =10;}//构造函数重载publicPerson(string str,int num){
  5.                 name = str;
  6.                 age = num;}}publicclassSimpleControl:MonoBehaviour{publicPerson person1;publicPerson person2;publicvoidStart(){//创建对象
  7.                 person1 =newPerson();
  8.                 person2 =newPerson("Zhao",18);
  9.                 person1.name ="Sun";
  10.                 person1.setName("Sun");//Debug.Log(person1.age);
  11.                 Debug.Log(person1.getAge());}}
复制代码
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-18 04:36 , Processed in 0.137590 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表