C#Unity基础方法归纳(1)

方法的间歇调用

    void Start()
    {
        //调用一次的时间
        //Invoke("CreateBox", 2);
        //重复调用方法(方法名,开始调用时间,调用间隔时间)
        InvokeRepeating("CreateBox", 5f, 2f);
    }

    // Update is called once per frame
    void Update()
    {
        //判断方法是否在调用队列
        bool res = IsInvoking("CreateBox");
        print(res);
        if (Input.GetKey(KeyCode.T))
        {
            CancelInvoke("CreateBox");
        }
    }
    void CreateBox()
    {
        print("CreateBoxCreateBox!");
    }

物块的启用停用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class globalVisual : MonoBehaviour
{
    private float timer = 3.0f;
    // Update is called once per frame
    void Update()
    {
        
        timer -= Time.deltaTime;
        if (timer<=0)
        {
            timer = 3.0f;
            gameObject.GetComponent<MeshRenderer>().enabled = !GetComponent<MeshRenderer>().enabled;
        }

    }
    private void OnMouseEnter()
    {
        gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
    }
    private void OnMouseExit()
    {
        gameObject.GetComponent<MeshRenderer>().material.color = Color.white;
    }
}

方块的旋转

// Update is called once per frame
    void Update()
    {
        //rorate方法欧拉角,每秒转30°
        transform.Rotate(Vector3.forward * Time.deltaTime * 30);

        //使用四元数,实现当按下r键时让物体在当前角度的基础上沿x轴旋转5°
        Quaternion q1 = transform.rotation;
        Vector3 vd = new Vector3(5, 0, 0);
        Quaternion q2 = Quaternion.Euler(vd);
        if (Input.GetKey(KeyCode.R))
        {
            transform.rotation = q1 * q2;
        }
    }

时间倍速/停滞

 void Update()
    {
        transform.Rotate(Vector3.up * Time.deltaTime*rotateSpeed);
        
        if (Input.GetKey(KeyCode.Q))
        {
            Time.timeScale = 10;

        }
        if (Input.GetKey(KeyCode.E))
        {
            Time.timeScale = 0;
        }
    }

物体的查找

    // Start is called before the first frame update
//先挂载gameobj再查找
//未激活方法一四无法使用
void Start()
{
    //方法一:全局搜索,Gameobject类中Find方法查询
  GameObject go =  GameObject.Find("G11");
    print(go.name);
    //方法二:transform中find查找,在当前物体的子对象中查找;
    Transform tran = transform.Find("G1/G11");
    print(tran.name);
    //方法三:父物体找子物体
    Transform ttr = GameObject.Find("G1").transform.GetChild(0);
    //方法四:tag标签查找1/多个物体
    GameObject go2 = GameObject.FindGameObjectWithTag("Player");
    print(go2.name);
    GameObject[] goArray = GameObject.FindGameObjectsWithTag("Player");
    print(goArray.Length);
    print(goArray[0].name);
    print(goArray[1].name);
}

**

物体的启用和禁用

**

// Start is called before the first frame update
//物体的启用和禁用
//脚本要挂在父类上,再在父类的这个script属性上把所要实现的物体cylinder拖拽到属性cylind上
public GameObject Cylind;//记得把东西挂上去
// Update is called once per frame
void Update()
{
    if (Input.GetKey(KeyCode.Z))
    {
        Cylind.SetActive(false);
        print(Cylind.activeSelf);
        print(Cylind.activeInHierarchy);
        //cylind.active只看自己激活为true
        //cylind.inhierarchy不仅看自己还要看父物体,只有同时为true才true
    }
    if (Input.GetKey(KeyCode.X))
    {
       Cylind.SetActive(true);
        print(Cylind.activeSelf);
        print(Cylind.activeInHierarchy);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值