Unity3D基础

鼠标事件:

OnMouseEnter():鼠标进入

OnMouseExit():鼠标移出

OnMouseDown():鼠标点击

OnMouseUp():鼠标抬起

static GameObject Instantiate() 克隆

static void Destroy()

位置 、 材料 、碰撞 、渲染

地形、预制、照相机、碰撞

using UnityEngine;
using System.Collections;

public class shubiao : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
    //:鼠标进入
    void   OnMouseEnter(){

        gameObject.renderer.material.color = Color.black;
    }
        
    void    OnMouseExit(){

        gameObject.renderer.material.color = Color.blue;
    }//:鼠标移出
            
    void    OnMouseDown(){
        gameObject.renderer.material.color = Color.yellow;
    }//:鼠标点击
            
        void    OnMouseUp()
        {
        gameObject.renderer.material.color = Color.red;
        GameObject.Destroy (gameObject);
        }//:鼠标抬起
}

GUI布局

using UnityEngine;
using System.Collections;

public class GIU : MonoBehaviour {
    private string str="张三";
    private string str2="";
    private bool  sex=false;
    private string _userName;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
    void OnGUI()
    {
        /*if (GUI.Button (new Rect (10, 10, 70, 20), "按钮")) {
                    print ("A")    ;
                }
        if (GUI.RepeatButton (new Rect (10, 100, 70, 20), "按钮")) {
                print ("B")        ;
                }
        str=GUI.TextArea (new Rect (10, 20, 100, 20), str);
        str2=GUI.PasswordField (new Rect (10, 150, 200, 20), str2, '*');
        sex=GUI.Toggle(new Rect(10,100,50,50),sex,"男" );//check*/

        GuI.Label(new Rect(10,10,70,20),"用户名");
        _userName = GUI.TextArea (new Rect (100, 10, 200, 20), _userName);

        _sex = GUI.Toggle (new Rect (10, 70, 50, 20), _sex, "");
        _sex=GUI.Toggle(new Rect(10,140,50,20),!_sex,"");
    }
}

GUILayOut 布局

例一:      private string str3="测试用的字符串";  //全局变量
GUILayout.Button ("GUILayout按钮"); if (GUILayout.Button (str3)) { str3+=str3 ; } if (GUI.Button (new Rect (50, 100, 100, 100), str)) { str3+=str3; }
 例二:  GUILayout.BeginHorizontal ();  //水平排列
        GUILayout.Button ("我是GUILayoutButton");
        GUILayout.Button ("我是GUILayoutButton");
        GUILayout.Button ("我是GUILayoutButton");
        GUILayout.Button ("我是GUILayoutButton");
        GUILayout.EndHorizontal ();
        GUILayout.BeginVertical ();  //纵向排列
        GUILayout.Button ("我是GUILayoutButton");
        GUILayout.Button ("我是GUILayoutButton");
        GUILayout.Button ("我是GUILayoutButton");
        GUILayout.Button ("我是GUILayoutButton");
        GUILayout.EndVertical();         

移动w 键 A键 S键 D键 :

using UnityEngine;
using System.Collections;

public class fouth : MonoBehaviour {//右边是 Z 轴,上边是y轴,X轴看不见的状态;
    GameObject go;
    // Use this for initialization
    void Start () {
        go=GameObject.Find("Cube3");
        go.renderer.material.color = Color.red;
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetKey (KeyCode.W)) {
            go.transform.Translate(-5*Time.deltaTime,0,0,Space.Self);
                }
        if (Input.GetKey (KeyCode.S)) {
            go.transform.Translate(5*Time.deltaTime,0,0,Space.Self);
        }
        if (Input.GetKey (KeyCode.A)) {
            go.transform.Translate(0,0,-5*Time.deltaTime,Space.Self);
        }
        if (Input.GetKey (KeyCode.D)) {
            go.transform.Translate(0,0,5*Time.deltaTime,Space.Self);
        }
    }
}

旋转:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {//旋转
        gameObject.transform.Rotate (0, 15 * Time.deltaTime, 0, Space.Self);
    }
}

碰撞:

void OnCollisionEnter(Collision co)
    {
        co.gameObject.renderer.material.color = Color.blue;
    }

预制(克隆):

void Update () {
        if (Input.GetKeyUp (KeyCode.Space)) {
            GameObject.Instantiate(go,gameObject.transform.position,gameObject.transform.rotation);
                }
    }
void Update () {

        if (Input.GetKey (KeyCode.LeftShift)) {
                        if (Input.GetKey (KeyCode.LeftShift)) {
                                gameObject.animation.Play ("run");
                gameObject.transform.transform(Vector3.forward*Time.deltaTime*6); //朝前移动 距离 速度
                        } else {
                                gameObject.animation.Play ("walk");
                gameObject.transform.transform(Vector3.forward*Time.deltaTime*0); //朝前移动 距离 速度
                        }
        
                }
        else {

            gameObject.animation.Play("stand");
            gameObject.transform.transform(Vector3.forward*Time.deltaTime*3); //朝前移动 距离 速度
                }     
    }



阴影烘焙:对灯设置 有阴影,对要烘焙的设置静止,打开 windows 点 light烘焙 back ;

动画剪辑:暂无

c#控制动画:

Application

切换场景

  if (Input.GetKeyDown (KeyCode.A)) {
            Application.LoadLevel(0);
                }

截屏

if (Input.GetKeyDown (KeyCode.Space)) {
            Application.CaptureScreenshot(@"c:1.png")
                }

打开一个网址

Application.OpenURL("H");//打开一个网址

退出:

Application.Quit();//退出

左击 游戏对象消失

        if(Input.GetMouseButtonDown(0)) //如果 按到 左击
        {
            Ray ray=camera.main.ScreenPointToRay(Input.mousePosition);//拿到点击的位置
            RaycastHit hitInfo;
            if(Physics.Raycast(ray,out hitInfo))//点击到
            {
                Destroy (hitInfo.collider.gameObject);   //销毁游戏对象
            }
        }
原文地址:https://www.cnblogs.com/woloveprogram/p/4668190.html