2019-01-23
由于此前C和C++都有學習過,感覺到C#的基本代碼大部分是見過,使用過的,而且代碼各種類的使用也非常像java,在此列舉一些平時比較少使用的代碼,以及Unity代碼
課程鏈接http://www.sikiedu.com/my/course/83
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;//使用命名空間
enum RoleType//枚舉類型增強代碼可讀性
{
Mag,
Soldier,
Wizard
}
public class NewBehaviourScript : MonoBehaviour
{
public GameObject player;
// Start is called before the first frame update
void Start()
{
Transform[] children = transform.GetComponentsInChildren<Transform>();
for (int i = 0;i<children.Length;i++) {
if (children[i] != transform)
GameObject.Destroy(children[i]);
}//for循環銷毀子物體
foreach (Transform t in children) {
if (t != transform) {
Destroy(t.gameObject);
}
}//foreach循環
GameDate gd;
Debug.Log("common");//一般輸出
Debug.LogWarning("Warning");//警告輸出
Debug.LogError("Error");//錯誤輸出
int[] hps = new int[10];//動態初始化
int[] hps2 = new int[4] { 1,2,3,4};//未見過的數組命名方式
int length = hps.Length;//數組是具有長度屬性的
RoleType rt = RoleType.Mag;//初始化
//獲取自身組件
Transform t = GetComponent<Transform>();
Collider[] c = GetComponents<Collider>();
//拖拽獲取組件,先定義一個public 的變量
player.GetComponent<Transform>();
//組件的禁用
c[1].enabled = false;
//獲取游戲物體的四種方式
//1,拖拽的方式
//2,
transform.Find("GameObject (1)/GameObject");
transform.Find("GameObject (2)");
//3,
GameObject.Find("Main Camera");//獲取查找的第一個物體,耗費性能
//4,通過標簽查找
GameObject.FindWithTag("Player");//查找起來快一些
}
}
//定義命名空間
namespace MyGame
{
class GameDate
{
}
}
本來到這就算結束了,不過報名了簡書日更活動還得每天100字!代碼不算字呀= =,還得在末尾水上一筆。明天就是學習Unity的第七天了,希望可以按照所說的七天培養出一種習慣,希望這個寒假可以把大概的操作學習完畢。大概100字了吧……