关于Unity的学习二--脚本生命周期

1.用于初始化脚本的代码必须置于Awake或者Start方法中,Awake方法是在加载时运行,Start方法是场景加载后第一次调用Update之前调用,不要在构造函数中初始化任何变量,要用Awake或者Start方法来实现。
2.public类型的变量能在检视视图职工通过拖拽来完成赋值,而private或者protected类型的成员变量不能查看。
3.脚本调试:Unity有一个控制台,Console,可以看到脚本中精确的错误,包括位置、行数。
在Unity中可以用print()和Debug.Log()打印调试信息,但print()只能在Mono类中使用,所以一般情况下最好使用Debug.Log()。
Debug.LogWarning():打印警告 Debug.LogError():打印错误信息 通过Debug.Break()设置断点;
public class TestDebug : MonoBehaviour {
int a;
// Use this for initialization
void Start () {
a = 2;
Debug.Log (a);
Debug.LogError (“Error occoured”);
Debug.LogWarning (“This is a Warning”);
Debug.Break ();

void Update () {

}

}

4.脚本生命周期函数:
分为五个阶段:编辑阶段Reset——开始阶段Awake-OnEnable-Start——迭代更新阶段FixedUpdate-Update-LateUpdate——显示阶段OnGUI——清除阶段OnDisable-OnDestroy

多个脚本的周期函数:A在B之前,先调A的Awake,Start,然后在调B的Awake,Start
5.Time类的使用
常见的属性:
(1)Time.time :从游戏开始后开始计时,表示截止目前共运行的游戏时间
(2)Time.deltaTime:获取Update()方法中完成上一帧所消耗的时间;
(3)Time.fixedTime:FixedUpdate()方法中固定消耗的时间的总和
FixedUpdate()每一帧更新的时间可以通过导航菜单栏“Edit”-“Project Settings”——“Time”菜单项去设置;
(4) Time.fixedDeltaTime:固定更新上一帧所消耗的时间
(5)Time.timeScale:时间比例,等于0表示暂停,=1表示正常,大于1表示加速,小于1表示减速;
例子:
void Start () {
Time.timeScale = 0;

void Update () {
print(“AAAAAAAAAAAA”);
print (“A” + Time.deltaTime);
//print(“update”);
print (Time.time);
print (Time.fixedDeltaTime);
print (Time.fixedTime);
print (Time.deltaTime);//Upadate
}
6.(1)随机数:用Random.Range()方法实现,其中该方法的第一个参数为随机数的起始位置,第二个参数为获取的随机数的结束位置;
void Start () {
int value = Random.Range (1, 100);
print (value);

(2)数学类Mathf
void Start () {
float f = Mathf.Acos (0.5f);
float degree = f * 180 / Mathf.PI;
print (degree);
float calmp = Mathf.Clamp01 (-0.1f);
print (calmp);

7.旋转和平移:
旋转:
void Update () {
this.transform.Rotate (new Vector3 (0, 0, 2));
this.transform.Rotate (0, 0, 120, Space.World);
prefab.GetComponent ().Rotate (120, 0, 0);
prefab.GetComponent ().RotateAround (this.transform.position, Vector3.up, 5);//围绕某一点转
}
平移:
void Update () {
this.transform.Translate (0, 0, 10, Space.World);
//this.transform.Translate (new Vector3 (0, 0, 0.05f));

用于旋转的Rotate方法和用于移动的Translate方法都有四个参数的重载形式,第四个参数为Space枚举类型,一个是Self,另外一个是World,后者表示被应用于相对于世界坐标系统,前者则是自身,默认第四个参数不传的话是Self。
8.访问游戏对象的组件:
(1)在Unity中,附加到游戏对象上的组件可以通过GetComponent方法获得
例子:
void Update(){
this.GetComponent().translate(1,0,0);
}
(2)通过GetComponent方法获取其他脚本
public class LifeTest : MonoBehaviour {
public GameObject sphere;
void Start () {
this.GetComponent ().SayHello ();
sphere.GetComponent ().SayHello ();//需要在Unity中拖拽要加的脚本


(3)脚本对象也可以直接通过代码进行挂载
void Start () {
GameObject sphere = GameObject.Find("Sphere”);//找到挂载的对象,然后把脚本加入到对象中;
HelloScripts sc = sphere.AddComponent ();
sc.enabled = true;

9.(1)通过属性查看器(检视视图)指定参数
代码中声明为public类型的游戏对象的引用,在检视视图中就能看到;只要将该引用指向一个游戏对象,那么脚本中访问这个对象的引用久相当于访问其他游戏对象

(2)通过名字或标签获取游戏对象
FindWithTag方法获取指定标签的游戏对象
void Start (){
//查找Tag为“MyTAg”的对象,若有多个对象的标签都设置为一致时,系统会找到最后添加的对象
GameObject obj = GameObject.FindWithTag(“MyTAg”);
print (obj);
}
Find方法获取指定名字的游戏对象
void Start () {
GameObject sphere = GameObject.Find(“Sphere”);
sphere.GetComponent ().material.color = Color.red;

若查找的对象有多个时,则会找到最后添加进来的对象。

(3)通过层次关系找游戏对象
在游戏中,存在有父子关系,在代码中可以通过获取Transform组件来找到子对象或者父对象;
public class LifeTest : MonoBehaviour {
Transform ChildForm;
void Start () {
ChildForm = this.transform.Find("Capsule”);//transform.Find获取的是名称为Cube的子对象
}
void Update(){
ChildForm.transform.Rotate (0, 0, 10);
}
}

(4)FindObjectOfType方法获取挂载指定类型组件的第一个游戏对象;
FindObjectsOfType方法获取所有挂载指定类型组件的对象,返回的是一个数组;
例子:
HelloScripts[] array = GameObject.FindObjectsOfType ();
foreach (var item in array) {
print(item.gameObject.name+item.gameObject.tag);
}

GameObject[] obj = GameObject.FindGameObjectsWithTag(“MyTag”);
print (obj.Length);
foreach (var item in obj) {
print(item.name+item.tag);
}

(5)在代码中直接创建一个立方体
void Start(){
GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
}

猜你喜欢

转载自blog.csdn.net/qq_36725286/article/details/89961019
今日推荐