您的位置:首页 > 移动开发 > Unity3D

Unity3d gameObject

2016-07-19 22:32 363 查看
using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {
//print只能在MonoBehavior的子类中使用,否则只能使用Debug.log()
public int age;
public string name;

//每当脚本被加载时调用;“有添加脚本即调用,哪怕该脚本没有激活”
void Awake(){
//通常在awake中 初始或public成员
print("awake");
}

//每次激活脚本时调用;
void OnEnable() {
//通常处理重置操作
print("onEnable");
}

//在第一次调用update之前调用;“脚本的生命周期中只调用一次”
public void Start () {
print("start");
//1.游戏对象的名字
gameObject.name = "cube_";

//2.游戏对象的tag
gameObject.tag = "Player";

//3.表示当前对象是否激活
if (gameObject.activeSelf)
{
}

//4.设置游戏对象的激活状态
gameObject.SetActive(true);

//5.获取游戏对象身上的组件
ss s = gameObject.GetComponent<ss>();

//6.给游戏对象添加指定类型的组件
Light l = gameObject.AddComponent<Light>();

//7.通过Tag值查找游戏对象
GameObject g = GameObject.FindGameObjectWithTag("Player");
g.name = "老王";

GameObject gg = GameObject.FindWithTag("Player");
GameObject[] gs = GameObject.FindGameObjectsWithTag("Player");

//8.通过游戏对象名字查找游戏对象
GameObject ggg = GameObject.Find("Plane");
ggg.name = "平面";

//9.销毁游戏对象
GameObject.Destroy(ggg, 2f);
}

// Update is called once per frame;“每帧调用一次”
void Update () {
bool b = Input.GetKeyDown(KeyCode.W);
print("update is input key 'W'"+b.ToString());
if (b)
{
var position = transform.position;
position.x = position.x + 1;
gameObject.transform.position.Set(position.x, position.y, position.z);
print("Update");
}

//参数表示接收鼠标的动作type;0表示左键,1表示右键,2表示鼠标中键
if (Input.GetMouseButtonDown(0))
{

}

}

//在update方法调用完之后调用
void LateUpdate() {
print("lateUpdate");
}

//取现激活状态后调用
void OnDisable() {
print("onDisable");
}

//被销毁时调用一次
void OnDestroy() {
print("onDestroy");
}

//持续调用;IMGUI代码需要写在OnGUI中
void OnGUI() {
print("onGUI");
}

//以固定频率调用
void FixedUpdate() {
//一般我们会把处理物理的代码放在这里
print("FixedUpdate");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: