using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//要求该脚本绑定的游戏对象必须存在某个组件类型,如果游戏对象没有,会自动为其添加
[RequireComponent(typeof(Animator))]
//[ExecuteInEditMode]//Edit Mode下就会运行代码,和Play Mode不同,此种模式只在必要的时候重绘
public class SmoothFollow : MonoBehaviour
{
[SerializeField]//私有变量也会显示在Inspector。比如当想让某个字段显示在Inspector中,但不想其他脚本能够访问时
private GameObject myPrefab;
[HideInInspector]//公有不显示在Inspector,但其他脚步可以访问
public float speed = 1.0f;
[Header("Move Info")]//在Inspector中在该字段的上方绘制一个标签
public float height = 5.0f;
public int missileCount = 0;
[Space]//在Inspector中在字段上面添加一个空行
public Sprite myAwesomeSprite;
//场景中实例化某个对象以后,会立即运行Awake
void Awake() {
}
//每当激活一个对象的时候
void OnEnable() {
}
//第一次调用对象的Update方法之前调用
//运行Start方法之前,所有对象(不仅仅是本对象)的Awake方法都已经完成运行
void Start()
{
//创建一个myPrefab的一个新副本
var newObject = (GameObject)Instantiate(myPrefab);
// 创建一个新的游戏对象显示为MyNewGameobject
var newObject_1 = new GameObject("MyNewGameobject");
//向newObject_1添加一个新的SpriteRenderer
var newRenderer = newObject_1.AddComponent<SpriteRenderer>();
newRenderer.sprite = myAwesomeSprite;
}
//每帧都会被调用
//所以尽量不要执行耗时工作,执行耗时工作可能得话应该使用协成。
void Update()
{
//启动协成
StartCoroutine(MoveObject());
}
//每一秒钟调用固定次数,比如当用到物理时,需要每个一定时间段施加外力
void FixedUpdate() {
}
IEnumerator MoveObject() {
while(true) {
//每秒移动1
transform.Translate(0, speed * Time.deltaTime, 0);
}
//遇到yield return时,将暂时停止执行该函数
yield return null;//等到下一帧恢复执行
yield return new WaitForSeconds(3);//等待三秒后恢复执行
yield return new WaitUntil(() => this.height < 5.0f);//等待满足某个条件时执行
yield break;//立即停止此协成
}
void HandleFunc() {
}
void DesTroyFunction() {
//销毁此脚本关联的对象
Destroy(gameObject);
//销毁该脚本
//Destroy(this);
}
}