赞
踩
承接之前的学习,从大纲出发,这章主要是理解和掌握一些基础的知识
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!");
- }
- }
- //类作用区域结束位置
- }
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;
- }
- 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);
- }
- }
- }
代码如下:
- 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);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。