程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Unity3D基礎,unity3d

Unity3D基礎,unity3d

編輯:C#入門知識

Unity3D基礎,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);   //銷毀游戲對象
            }
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved