Unity3D 学习笔记

发布时间:2023-06-16 14:00

Unity3D 学习笔记:

  • Unity版本 2019.1.0f2
  • C#编辑器 Visual Studio
  • 官方学习手册
  • 博客干货:
    1. 新版Unity不再安装MonoDevelopment,可以自己设定脚本编辑器,参考:Unity3D使用VS作为脚本编辑(Unity3d与vs2015关联起来)
    1. 代码中控制音频文件的播放,参考:Unity代码控制音频播放
    2. 简单界面显示倒计时,参考:简单倒计时功能
    3. 其它计时方式实现,参考:多种计时器实现方式
    4. 点击按钮/事件触发场景的切换,参考:点击场景切换
    5. 多个文件共享数据/变量的实现,参考:PlayerPrefs存储与读取数据
    6. 场景间切换传递保存数据的方法
    7. RigidBody与Collision :unity 2d碰撞/ui组件碰撞
    8. 弹出界面,参考:unity点击按钮弹出操作提示界面
  • Unity2D控制游戏对象移动与碰撞:

  (教学视频:哔哩哔哩)

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

public class PlayerController : MonoBehaviour {

    //水平方向
    [Header("水平方向")]
    public float moveX;

    //垂直方向
    [Header("垂直方向")]
    public float moveY;

    [Header("推力")]
    public float push;

    Rigidbody2D rb2D; //刚体

    [Header("分数文字UI")]
    public Text countText;

    [Header("胜利消息")]
    public Text winText;


    public int countTime;

    int score;

    // Use this for initialization
    void Start () {
        
        rb2D = GetComponent ();
        score = 0;

        winText.text = "";
      
        setScoreText (); //提示:目前分数:0
        end = false;

    }
    
    // Update is called once per frame
    void FixedUpdate () {
        moveX = Input.GetAxis ("Horizontal");

        moveY = Input.GetAxis ("Vertical");

        //移动方向 = (水平移动,垂直移动)
        Vector2 movement = new Vector2 (moveX,moveY);

        //施加力量
        rb2D.AddForce (push * movement);
    }

    void OnTriggerEnter2D(Collider2D other){
    
        Debug.Log (name + "触发了" + other.name);

        if (other.CompareTag ("PickUp")) {
        
            //碰撞的物体状态不活跃
            other.gameObject.SetActive (false);

            score += 1;

            setScoreText ();
        
        }
    }
}
  • 红绿灯切换效果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class light : MonoBehaviour {
    public GameObject obj1,obj2,obj3;
    public int state;
    public float time = 0.5f;
    // Use this for initialization

    void Start () {
        obj1 = GameObject.Find ("Red");
        obj2 = GameObject.Find ("Yellow");
        obj3 = GameObject.Find ("Green");

        //最初red亮
        obj1.GetComponent().material.color = Color.red;
        obj2.GetComponent().material.color = Color.gray;
        obj3.GetComponent().material.color = Color.gray;

        obj1.AddComponent  ();
        obj2.AddComponent  ();
        obj3.AddComponent  ();

        obj1.AddComponent ().clip = (AudioClip)Resources.Load ("3");
        obj2.AddComponent ().clip = (AudioClip)Resources.Load ("2");
        obj3.AddComponent ().clip = (AudioClip)Resources.Load ("1");

        obj1.GetComponent ().Play();

        state = 0;

        InvokeRepeating ("changeColor",1f,1f);
    }

    // Update is called once per frame
    void changeColor () {

        switch (state) {
        case 0://now is red
            changeYellow();
            //Invoke ("changeYellow", 0.5f);
            break;
        case 1://yellow
            changeGreen();
            //Invoke ("changeGreen", 0.5f);
            break;
        case 2://green
            changeRed();
            //Invoke ("changeRed", 0.5f);
            break;
        }

    }
    void Update (){

        //changeColor ();
    }
    void changeRed(){
        obj1.AddComponent ();
        obj1.AddComponent ().clip = (AudioClip)Resources.Load ("3");
        obj1.GetComponent ().Play();
        obj3.GetComponent().material.color = Color.gray;
        Debug.Log ("111");
        obj1.GetComponent ().material.color = Color.red;
        state = 0;
    }

    void changeYellow(){
        
        obj2.GetComponent ().Play ();
        //GetComponent ().Play ();
        obj1.GetComponent().material.color = Color.gray;
        obj2.GetComponent ().material.color = Color.yellow;
        state = 1;
    }
    void changeGreen(){

        obj3.GetComponent ().Play ();
        obj2.GetComponent().material.color = Color.gray;
        obj3.GetComponent ().material.color = Color.green;
        state = 2;

    }
    /*void changeColor(){
        switch (state) {
        case 0:    
            obj1.GetComponent ().material.color = Color.red;
            //StartCoroutine (load ());
            obj1.GetComponent ().material.color = Color.gray;
            state = 1;//ye;llow
            //}
            break;
        case 1:    
            //    if (state==1) {
            obj2.GetComponent ().material.color = Color.yellow;
            //StartCoroutine (load ());
            obj2.GetComponent ().material.color = Color.gray;
            state = 2;//green
            //}
            break;
        case 2:    
            //if (state==2) {
            obj3.GetComponent ().material.color = Color.green;
            //StartCoroutine (load ());
            obj3.GetComponent ().material.color = Color.gray;
            state = 0;//red
            //}
            break;
        }
    }*/
}
  • 计时效果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class countdown : MonoBehaviour {
    public GameObject text;
    public int totaltime = 60;
    public GameObject obj1;
    // Use this for initialization
    void Start () {
        StartCoroutine (CountDown ());
        obj1 = GameObject.Find ("Red");
        obj1.AddComponent ();
        obj1.AddComponent ().clip = (AudioClip)Resources.Load ("3");
        obj1.GetComponent ().Play();
    }

    IEnumerator CountDown(){
        while (totaltime >= 0) {
            text.GetComponent().text= totaltime.ToString ();
            yield return new WaitForSeconds (1);
            totaltime--;
        }
    }
    // Update is called once per frame
    void Update () {
        
    }
}
  • 实现小汽车的运动,前进后退,向左向右,旋转,加速:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class car : MonoBehaviour {
    public Rigidbody CarRgd;
    public Transform enemy;    //终点
    public float force;    //加速度
    public AudioSource start;
    public AudioSource onaudio;
    public AudioSource accelerate;
    private bool on;
    private bool ac;

    // Use this for initialization
    void Start () {
        on = false;
        ac = false;
    }
    
    // Update is called once per frame
    void Update () {

        driving ();
    }


    void driving (){



        if (Input.GetKey (KeyCode.Space)) 
        {
            Vector3 dir = enemy.position - CarRgd.position;
            dir.y = 0;
            Quaternion target = Quaternion.LookRotation (dir);
            CarRgd.rotation=Quaternion.Lerp (CarRgd.rotation, target, Time.deltaTime);
            CarRgd.MoveRotation(Quaternion.Lerp (CarRgd.rotation, target, Time.deltaTime*5));
            //CarRgd.MovePosition (enemy.position);
            if (!start.isPlaying) {
                start.Play ();
            }
        }
        //前进
        if (Input.GetKey (KeyCode.UpArrow)) {
            CarRgd.MovePosition (CarRgd.transform.position + Vector3.forward * Time.deltaTime * 2);
            on = true;
        } else {
            on = false;
        }

        if (on) {
            start.Pause ();
            if (!onaudio.isPlaying) {
                onaudio.Play ();
            }

        }

        //后退
        if (Input.GetKey (KeyCode.DownArrow)) 
        {
            CarRgd.MovePosition (CarRgd.transform.position + Vector3.back * Time.deltaTime*2);
        }

        //左移
        if (Input.GetKey (KeyCode.LeftArrow) )
        {
                CarRgd.MovePosition (CarRgd.transform.position + Vector3.left * Time.deltaTime*2);
        }

        //右移
        if (Input.GetKey (KeyCode.RightArrow) )
        {
            CarRgd.MovePosition (CarRgd.transform.position + Vector3.right * Time.deltaTime*2);
        }

        //向右旋转
        if (Input.GetKey (KeyCode.C) )
        {
            Quaternion targetRotation = Quaternion.Euler (new Vector3(0,45,0));
            CarRgd.MoveRotation (Quaternion.Lerp (CarRgd.rotation, targetRotation, Time.deltaTime*5));

        }
        //向左旋转
        if (Input.GetKey (KeyCode.Z) )
        {
            Quaternion targetRotation = Quaternion.Euler (new Vector3(0,-45,0));
            CarRgd.MoveRotation (Quaternion.Lerp (CarRgd.rotation, targetRotation, Time.deltaTime*5));

        }

        //加速
        if (Input.GetKey (KeyCode.X)) {    
            
            CarRgd.MovePosition (CarRgd.transform.position + Vector3.forward * Time.deltaTime * 10);
            //CarRgd.AddForce(Vector3.forward*force);
            //CarRgd.position=CarRgd.transform.position+Vector3.forward*Time.deltaTime;
            //accelerate.Play();
            ac = true;
        
        } else {
            ac = false;
        }

        if (ac) {
            onaudio.Pause ();
            if (!accelerate.isPlaying) {
                accelerate.Play ();
            }

        }
    }
}

 

转载于:https://www.cnblogs.com/codecheng/p/10645111.html

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号