Unity中的C#学习(二)

承接之前的学习,从大纲出发,这章主要是理解和掌握一些基础的知识


1.脚本概念:在unity当中,脚本可被看为行为组件。

1.1可以在检视器中看到,公开的属性值可以在面板中动态赋值和通过拖拽的方式添加,通过挂在游戏对象上面进行执行.


2.变量与函数

using UnityEngine;
using System.Collections;

public class VariablesAndFunction{

	int myInt=5;

	void Start(){

		myInt = MultipleByTwo (myInt);
	}

	private int MultipleByTwo(int number){

		int ret;

		ret = myInt * 2;

		return ret;
	}
}
3.脚本规范与语法:

<pre name="code" class="csharp">using UnityEngine;
using System.Collections;
//修饰符 类   类名  		  父类	类作用区域起始位置
public class BasicSyntax: MonoBehaviour{
// 这个是单行注释
/*这个是多行注释
 *
 *
*/
//	返回类型 方法名 
	void Start(){
//		类.静态方法 方法参数 tranform 来自于父类
		Debug.Log (transform.position.x);

		if (transform.position.y <= 5f) 
		{
			Debug.Log ("I`m about to hit the Ground!");
		}
	}
//类作用区域结束位置
}

 4.c#与js的语法区别 

c#的脚本如下:

using UnityEngine;
using System.Collections;
public class ExampleCSharpSyntax: MonoBehaviour{
	int myInt=3;
	int MyFuntion(int number){
		int ret=myInt*number
			return ret;
	}
}

js的脚本如下:

#pragma strict
var myInt: int=3;

function MyFunction(number:int):int
{
	var ret=myInt*number;
	return ret;
}

5.If语句

using UnityEngine;
using System.Collections;

public class IfStatements : MonoBehaviour {
	public float coffeeTempreature = 85.0f;
	public float hotLimitTempreature=70.0f;
	public float coldLimitTempreature=40.0f;

	void Update(){
		if (Input.GetKeyDown (KeyCode.Space))
			TempreatureTest ();

		coffeeTempreature -= Time.deltaTime * 5f;
	}

	void TempreatureTest(){
		if (coffeeTempreature > hotLimitTempreature) {
			Debug.Log ("Coffee is too hot");
		}else if(coffeeTempreature<coldLimitTempreature){
			Debug.Log ("Coffee is too cold.");
		}else{
			Debug.Log("Coffee is just right.");
		}

	}
}
6.循环
<img src="https://img-blog.csdn.net/20160702112205043?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
代码如下:
<pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class Loop : MonoBehaviour {
	int cupsInTheSink=4;

	void Start(){
		//while loop
		while(cupsInTheSink>0){
			Debug.Log ("I`ve washed a cup!");
			cupsInTheSink--;
		}

		//do while loop
		bool shouldContinue = false;
		do {
			print ("Hello World");
		} while(shouldContinue == true);

		//for loop
		int numEnemies=3;
		for (int i = 0; i < numEnemies; i++) {
			Debug.Log ("Creating enemy number: " + numEnemies);
		}

		//foreach loop
		string [] strings=new string[3];
		strings[0]="First String";
		strings[1]="Second String";
		strings[2]="Thrid String";

		foreach (string item in strings) {
			print (item);
		}
	}
}


 7.作用域与修饰符 

代码如下:

using UnityEngine;
using System.Collections;

public class ScopeAndAccessModifiers : MonoBehaviour {
	//共有变量可以在类的外部访问,并且在脚本被挂载的时候,被访问到
	public int alpha;
	//私有变量只可以在类的内部访问和使用
	private int beta=0;
	private int gamma=5;

	void Example(int pens,int crayons){
		int answer;
		answer = pens * crayons * alpha;
		Debug.Log (answer);
	}

	void Update(){

		Debug.Log ("Alpha is set to: " + alpha);
	}
}

第二章的学习先到此结束,都比较基础,但是也很重要.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值