[unity菜鸟] 笔记1 —— 函数篇

SendMessage()  

调用其他物体中的指令,先在脚本中编写一个自定义的函数,然后使用SendMessage()命令来调用那个物体上的命令

//①将以下函数附给target对象

void Renamer(string newName)
{
    gameObject.name=newName;     
}

//②在其他脚本上调用这个函数

GameObject.Find("target").SendMessage("Renamer","target_down");

 

移动平台编程注意

//使用这样的代码风格为移动平台编程时,把上述方法分两步很重要
GameObject.Find("target").GetComponent<Setter>().Renamer("target_down");
                                  
                                   ↓
GameObject theTarget = GameObject.Find("target");
theTarget.GetComponent<Setter>().Renamer("target_down");

  

 Update()和FixedUpdate()

FixedUpdate() 与物理引擎保持同步,函数每秒被调用的次数是固定。当我们使用与物理相关的脚本时,它更适合,如刚体的使用和重力效果。

Update()函数取决于帧数,每秒被调用的次数取决于硬件本身的性能。

 

 

 

Input.GetAxis

确定移动方向的,Input.GetAxis("Horizontal")是在X轴上移动,Input.GetAxis("Vertical")代表在z轴上移动

 

Unity中 Move 和 SimpleMove 区别                                                                       (参考)

总结如下:

Move: 不受重力影响, 受到collider的阻挡。 Move(new Vector(0, 0, 1)) --- 向z轴移动动一单位

SimpleMove: 受到重力影响, 也受到collider阻挡, SimpleMove(new Vector(0, 0 ,1)); ----向z轴移动 Time.deltaTime * 1单位的距离。

 

Quaternion.LookRotation

 

InvokeRepeating

void Start()
{
      //0表示现在开始调用,1表示每一秒都调用一次repeat()函数
      InvokeRepeating("repeat",0,1);  
}    

void repeat()
{
      Debug.log(" I am repeating every second");      
}

 

Instantiate  实例                                                                                                 (参考)

static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object

描述:克隆原始物体并返回克隆物体。

posted @ 2014-04-19 15:53  quanzhan  阅读(250)  评论(0编辑  收藏  举报