Unity中常用方法

1.基础

//初始化引入
[RequireComponent(typeof(BoxCollider2D))]
[RequireComponent(typeof(Rigidbody2D))]

//引入TextMeshProUGUI是使用TMP_Text 类型
public TMP_Text textMeshProUGUI; //分数Text
textMeshProUGUI.text = fs.ToString();


//游戏帧率设置  60帧
 Application.targetFrameRate = 60;


//获取物体对象
//获取到当前物体(根据名称,也可以根据路径)
 GameObject go= GameObject.Find("红旗");
 GameObject go = this.transform.GetChild(0).gameObject; //根据下标
 GameObject go= Resources.LoadAll<GameObject>("preform/Enemys");//根据目录结构查找Resources目录下面



//物体位置世界位置/本地位置
 Vector3 vector3 = gameObject.transform.position;
 Vector3 vector3 = gameObject.transform.localPosition;

//颜色处理  【R,G,B,透明度】
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);


//复制物体
  AudioSource music = this.GetComponent<AudioSource>();
  Rigidbody2D rb = this.GetComponent<Rigidbody2D>();
  Translate  tr= this.GetComponent<Translate>();

//挂载脚本
gameObject.AddComponent<BulletCol>();



//当前鼠标点击
Input.GetMouseButtonDown(0)

2.方法

0.物体赋予初始位置初始欧拉角

guanzi.transform.position = new Vector3(-10f, -2f, -0.15f);         //初始位置
guanzi.transform.localEulerAngles = new Vector3(-10f, -2f, -0.15f); //初始欧拉角

       1.物体移动Translate物体旋转Rotate

//物体移动 Translate(x, y, z, 相对)Self 相对自己,Word相对世界
this.transform.Translate(0, 0, 每秒运行长度, Space.Self);

//物体旋转 Rotate(x, y, z, 相对)Self 相对自己,Word相对世界
this.transform.Rotate(0, 速度 * Time.deltaTime, 0, Space.Self);

//父物体旋转 Rotate(x, y, z, 相对)Self 相对自己,Word相对世界
this.transform.parent.Rotate(0, 速度 * Time.deltaTime, 0, Space.Self);

      2.朝向物体移动LookAt

 //让当前物体看向红旗 把物体z轴转向目标LookAt(transform);
this.transform.LookAt(GameObject.transform);

     3.播放音乐

    public AudioClip[] acp;

    public void playMusic(){
        int index = Random.Range(0, acp.Length);
        AudioSource ast = GetComponent<AudioSource>();
        ast.clip = this.acp[index];
        ast.Play();
        ast.Stop();
  }

4.赋予初始速度(刚体)Rigidbody2D

Rigidbody2D rb = this.GetComponent<Rigidbody2D>(); 
rb.velocity = new Vector2(x,y);//x/y 为多少速度为多少 

5.本地坐标转世界坐标

Vector3 targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

6.动画处理传送门

7.摄像机跟随玩家 

8.事件处理

9.场景处理 

//场景切换
SceneManager.LoadScene("game");

 3.函数

消息函数

//发送消息调用  【方法名字,方法参数,参数3】
//SendMessageOptions.DontRequireReceiver 找不到方法不回报错
//SendMessageOptions.RequireReceiver     找不到方法会报错
gameobject.SendMessage("Damage", Random.Range(5, 21),SendMessageOptions.DontRequireReceiver);

定时函数

InvokeRepeating("射击", 1, 2); // 等待1秒后每2秒吊用一次射击的方法。
IsInvoking("射击")     判断该函数是否被调用 如果没有在调用
CancelInvoke("射击")   取消被调用

预制体

//实例化一个guanzi位置在gzWzzb.transform相对于(true)本地坐标/false世界坐标
//实例化预制体, Instantiate(预制体,位置,true/false) true是本地坐标false世界坐标
 guanzi = Instantiate(guanzi, gzWzzb.transform, true);


guanzi.transform.rotation = gzWzzb.transform.rotation;//设置旋转位置


//物体销毁  物体3秒后销毁
 Destroy(guanzi, 3f);

图像轮动

//_MainTex写死,Vector(x,y);  Time.time/5沿着X轴5秒轮动一次
 this.GetComponent<Renderer>().material.SetTextureOffset("_MainTex",new Vector2(Time.time/5,0));

 物体移动

 Vector3 ui;
//物体的移动【移动的transform,移动的位置,移动的角度,移动后输出的位置】
RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.enterEventCamera, out ui);

 携程开辟一个线程

 //StartCoroutine(方法)
 StartCoroutine(aTime());

IEnumerator aTime()
    {
        while (djs >= 0)
         {
            djsGUI.text = djs.ToString();
             yield return new WaitForSeconds(1);
            djs--;
         }
        yield return new WaitForSeconds(1);
 }

//IEnumerator  对应yield类型
yield return 0; //等0帧 销毁这个对象
yield return 1; //等1帧 销毁这个对象
yield return WaitForSeconds(3); //等待3秒 销毁这个对象

异步加载一个场景

时间函数

Time.time 从游戏开始后所运行的时间
Time.deltaTime 表示从上一帧到当前帧时间
Time.frameCount 时间的缩放,Time.timeScale为1是默认值,为0可用于游戏的暂停,为2可用于游戏的快进

4.生命周期

MonoBehaviour生命周期

void Awake(){} //最早时调用,一般可以在此实现单例模式(未激活的组件也会被调用)
void OnEnable(){} //组件激活后调用,在awake()后调用一次
void Start(){}  //在update()之前调用一次,在onEnable之后调用,可以再次设置一些初始值。(未激活的组件不会被调用)
void FixedUpdate(){}//固定频率调用方法,每次调用与上次调用的时间间隔相同
void Update(){}     //帧率调用法,每帧调用一次,每次调用与上一次调用的时间间隔不相同
void LateUpdate(){} //在udate()每调用完一次后,紧跟着调用一次
void OnDisable(){}  //与onEnable相反,组件未激活时调用
void OnDestroy(){}  //被销毁后调用一次

 面板的生命周期

//面板开启时候执行
void OnEnter(){}

//UI暂停时执行的操作
void  onPause(){}

//UI继续时执行的操作
void  onResume(){}

//面板结束时候执行
void OnExit(){}

5.鼠标键盘事件Input方法

6.碰撞事件

猜你喜欢

转载自blog.csdn.net/qq_38092788/article/details/132020512
今日推荐