Unity3D C#编程(基础)

Unity3D C#编程

一.当前物体

​ 1.this,当前脚本组件

​ 2.this.gameObject,当前物体

​ 3.this.gameObject.name,当前物体名字

​ 4.this.gameObject.transform,当前物体下的Transform组件

也可以写成this.transform

GameObject obj = this.gameObject;//实例化
Vector3 pos =this.gameObject.transform.position;//transform组件下position位置
Debug.Log("**物体位置:" + pos.ToString("F3"));//限制小数点位数

二.物体的坐标

​ position(世界坐标)

​ localPoiton(本地坐标即父与子类坐标)

设置物体的坐标:

this.transform.position = new vector3(1.5f,0,2.0f);

三.帧更新

​ 帧率观察:

-	Time.time,游戏时间
-	Time.deltaTime,距上次更新的 时间差

设定近似帧率

Application.targetFrameRate = 60;(放在start()初始函数里)

四.物体的移动

1.匀速移动

float speed = 3;
float move = speed*deltaTime;//
Vector3 pos = this.transform.localPosition;
        pos.x += move;
        this.transform.localPosition = pos;

​ 上面这个方法是狗屁,用下面这个!

this.transform.Translate(0,0,distance);//位移增量
2.朝向目标运动(地面)
1.获取目标
GameObject flag = GameObject.Find("物体名称");//因为脚本没有挂载到目标上,所以不能使用this关键字
2.转向目向
this.transform.LookAt(flag.transform);
3.向前移动
this.transform.Translate(0,0,move,space.self);
3.向量测距
	Vector3 p1=flag.transform.postion;
	vector3 p2=this.trasform.position;
	vector3 p=p1-p2;
	float distance=p.magnitude;
4.练习
转向物体,碰到物体停止

		GameObject flag = GameObject.Find("目标");//因为脚本没有挂载到目标上,所以不能使用this关键字
        Vector3 p1 = flag.transform.position;
        Vector3 p2 = this.transform.position;
        Vector3 p = p1 - p2;
        float distance = p.magnitude;

        if (distance > 0.3f)
        {
            float speed = 3;
            float move = speed *Time.deltaTime;
                                           
           //转向目向
            this.transform.LookAt(flag.transform);
            //向前移动
            this.transform.Translate(0, 0, move, Space.Self);
        }

五.物体的旋转

​ 1.transform.rotation用的是四元数表示旋转,可以跟SLAM结合?

2.欧拉角

​ transform.eulerAngles = new Vector(0,45,0);

​ transform.localEulerAngles = new Vector(0,45,0);

3.自旋转API

​ float RotSpeed =60;

​ float speed = RotSpeed*Time.deltaTime;

​ this.transform.Rotate(0,speed,0);

4.公转实现

​ 例:地球自转一个速度 卫星围绕着地球公转一个速度。

​ 卫星主要函数:

Transform parent = this.transform.parent;

六.消息函数

-Awake 都是初始化时调用,但先于Start,而且即使脚本组件被禁用也会被调用,而Start在组件被禁用时不会被调用。

-OnEnable 组件启用时调用

-Ondisable 组件禁用时调用

七.脚本添加可调参数

1.参数用法

​ -参数必须为public

​ -名称命名按首字母小写,另一个单词大写。rotateSpeed

​ -参数工具提示,可以用[Tooltip(“旋转角速度”)]来指定

2.变量与引用型

-public string stringValue = “你好你好”;

-public Vector3 rotateSpeed = new Vector3(1f, 1f, 1f);

-public GameObject target;

八.鼠标键盘触摸屏输入

1.鼠标输入API
(返回值为Bool)

​ -Input.GetMouseButtonDown(2)

​ -Input.GetMouseButtonUp(1)

​ -Input.GetMouseButton(0) //一直按着鼠标

参数 0为鼠标左键,1右键,2中键

​ 鼠标点击位置获取:Vector3 mousePos=Input.mousePosition;

2.屏幕坐标
1.转化

-Vector3 worldPos = this.transform.position;

-Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);

2.获取屏幕大小

​ -Screen.width

​ -Screen.higth

九.键盘输入

1.相关API
 //同样返回值都是Bool类型
 -Input.GetKey(KeyCode.W)  //检测按键是否一直按下
 -Input.GetKeyDown(KeyCode.W) //按键事件,按下
 -Input.GetKeyUp(KeyCode.W)

十.组件调用

//音乐点击播放再点击停止播放。
//AudioSource audio = this.GetComponent<AudioSource>();
void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            PlayMusic();
        }
    }
    void PlayMusic()
    {
        AudioSource audio = this.GetComponent<AudioSource>();
        if (audio.isPlaying)
        {
            audio.Stop();
        }
        else
        {
            audio.Play();
        }
   
//主控界面 调用其他脚本组件
public class move : MonoBehaviour
{
    public GameObject node;//也可以 public move target;DoWork()实现里 直接使用脚本组件 target.XXXXX
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        DoWork();
    }
    void DoWork()
    {
        move target = node.GetComponent<move>();//实例化组件时,类型用的是脚本名称
        
    }
}

十一.物体获取

public GameObject node1 = GameObject.Find("父节点/子节点");//如果改名则出错,所以以后都用用这个方法。

十二.资源的使用

1.资源类型
  • AudioClip 音频文件
  • Texture 纹理贴图
  • Material 材质
2.资源调用切换
	public AudioClip successs;
    public AudioClip fault;
    
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            AudioSource audioSource = GetComponent<AudioSource>();
            audioSource.PlayOneShot(successs);
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            AudioSource audioSource = GetComponent<AudioSource>();
            audioSource.PlayOneShot(fault);
        }

    }
3.资源数组
    public AudioClip[] songs;
//音乐盒创建
public AudioClip[] songs;
    
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            NextSong();
        }

    }
    void NextSong()
    {
        int index = Random.Range(0, songs.Length);
        AudioSource ac = GetComponent<AudioSource>();
        ac.clip = this.songs[index];
        ac.Play();
    }
//材质次序切换
public Material[] materials;
    int index = 0;

    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            changcolor();
        }

    }
    void changcolor()
    {
        index += 1;
        if (index>=this.materials.Length)
        {
            index = 0;
        }
        MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
        meshRenderer.material = this.materials[index];
    }

十三.定时调用(invoke)

1.相关API

  • Invoke(func,delay),只调用一次
  • InvokeRepeating(func,delay,interval) 循环调用
  • IsInvoking(func),是否在调度中
  • CancelInvoke(func),取消调用
  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值