Unity 3D第二更

Unity 3D改變旋轉角度

float value_x=0;
    float value_y=0;
    float value_z=0;
    GameObject cube ;
    GameObject cylinder;
    // Use this for initialization
    void Start () {
        cube = GameObject.Find ("Cube");
        cylinder = GameObject.Find ("Cylinder");
    }
    void OnGUI()
    {

        GUILayout.Box ("改動X軸角度");
        value_x = GUILayout.HorizontalSlider (value_x,-180,180,GUILayout.Width (200));
        GUILayout.Box ("改動Y軸角度");
        value_y = GUILayout.HorizontalSlider (value_y,-180,180,GUILayout.Width (200));
        GUILayout.Box ("改動Z軸角度");
        value_z = GUILayout.HorizontalSlider (value_z,-180,180,GUILayout.Width (200));
        cube.transform.rotation =Quaternion.Euler (  value_x,value_y,value_z);
        GUILayout.Box ("角度爲"+cube.transform.rotation);

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

    }

Unity 3D工具欄選項菜單

 //記錄選擇了哪個按鈕的ID
    int select;

    string[] barsources;

    bool selectToggle1;

    bool selectToggle2;

    void OnGUI()
    {
        int oldSelect = select;
        select = GUI.Toolbar(new Rect(10,10,barsources.Length*100,30),select,barsources);
        if(select != oldSelect)
        {
            selectToggle1 = false;
            selectToggle2 = false;
        }

        switch(select)
        {
            case 0:
                selectToggle1 = GUI.Toggle(new Rect(10,50,200,30),selectToggle1,"工具欄1的單項選擇按鈕-1");
                selectToggle2 = GUI.Toggle(new Rect(10, 80, 200, 30), selectToggle2, "工具欄1的單項選擇按鈕-2");
                break;
            case 1:
                selectToggle1 = GUI.Toggle(new Rect(10, 50, 200, 30), selectToggle1, "工具欄2的單項選擇按鈕-1");
                selectToggle2 = GUI.Toggle(new Rect(10, 80, 200, 30), selectToggle2, "工具欄2的單項選擇按鈕-2");
                break;
            case 2:
                selectToggle1 = GUI.Toggle(new Rect(10, 50, 200, 30), selectToggle1, "工具欄3的單項選擇按鈕-1");
                selectToggle2 = GUI.Toggle(new Rect(10, 80, 200, 30), selectToggle2, "工具欄3的單項選擇按鈕-2");
                break;
            case 3:
                selectToggle1 = GUI.Toggle(new Rect(10, 50, 200, 30), selectToggle1, "工具欄4的單項選擇按鈕-1");
                selectToggle2 = GUI.Toggle(new Rect(10, 80, 200, 30), selectToggle2, "工具欄4的單項選擇按鈕-2");
                break;

        }
    }

    // Use this for initialization
    void Start()
    {
        select = 0;
        barsources = new string[] { "第一個工具欄", "第二個工具欄", "第三個工具欄", "第四個工具欄" };
        selectToggle1 = false;
        selectToggle2 = false;
    }

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

    }

Unity 3D 公轉Around 的用法

void Update () {

      //  transform.Rotate(Vector3.up * Time.deltaTime * -Gongzhuang);
       /// transform.RotateAround(new Vector3(0, 0, 4.031555f), Vector3.up, 20 * Time.deltaTime);
       /// 
        transform.RotateAround(new Vector3(0, 0, 0), Vector3.up, 20 * Time.deltaTime);//三個參數,旋轉中心座標  旋轉速度

    }
    int Gongzhuang = 360;
    void OnGUI()
    {

        if (GUI.RepeatButton(new Rect(10, 40, 150, 30), "月球公轉"))
        {


           // 
        }
    }

Unity 3D滾動條

 Vector2 scrollviewposition;
    // Use this for initialization
    void Start () {
        scrollviewposition[0] = 0;
        scrollviewposition[1] = 0;
    }

    void OnGUI() 
    {
        scrollviewposition = GUI.BeginScrollView(new Rect(0, 0, 300, 300), scrollviewposition, new Rect(0, 0, 300,250), true, false);
        GUI.Label(new Rect(10, 10,50,50), "測");
        GUI.EndScrollView();
    }

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

    }

Unity 3D 接受任務繪製窗口實例

 // Use this for initialization
    void Start()
    {

    }
    // int sudu = 20;

    // private Transform transform;
    // Update is called once per frame

    bool isWin = false;//添加一個標示,表示窗口的狀態
    string Mee = "親愛的勇士們";
    string Mess = "愚蠢的人類呀!";
    string M = "我可以實現你的願望!";
    string t1 = "實現";
    string t2 = "放棄";
    int click = 0;//按鈕的點擊次數

    string[] Huoba;

    void OnGUI()
    {
        GUI.Label(new Rect(20, Screen.height/2, 200, 250), Mee+click);//左邊的說明文字

        if (isWin)
        {
            GUI.Window(0, new Rect(Screen.width / 2, Screen.height / 4, 300, 100), Mouse,Mess );


                if (GUI.Button(new Rect(Screen.width / 2 + 25, Screen.height / 4 + 60, 50, 30),t1))
                {
                    click++;//每點擊一次按鈕,click++
                  //  isWin = false;
                    Mee = "幫我搜集神器,我幫你完場夢想";
                    Mess = "尋找十方神器!";
                    M = "如果你找到了十方神器,我給你榮華富貴!";
                    t1 = "開始尋找";
                    t2 = "放棄尋找,回家睡覺";

                    if (click == 2)//如果點擊次數是2的話,就關閉窗口
                    {


                        isWin = false;



                       // GetComponent<Light>().enabled = false;//關閉組件
                       // gameObject.SetActive(false);//設置活動
                   //     GetComponent<Light>().intensity = 0.0f;//減弱燈光強度
                       // GetComponent<Light>().range = 0.0f;//點光源範圍

                    }




                }
                if (GUI.Button(new Rect(Screen.width / 2 + 220, Screen.height / 4 + 60, 50, 30), t2))//點擊取消按鈕
                {
                    isWin = false;

                    Mee = "呵呵噠!";

                }


        }

    }

    void Mouse(int Winid)
    {

        GUI.Label(new Rect(80, 30, 150, 50), M);//窗體的標籤文字
    }


    void Update()
    {

        //if (click == 0)
        //{
        //    isWin = false;

        //if (Input.GetMouseButtonDown(1))
        //{
        //    RaycastHit hit;
        //    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //    if (Physics.Raycast(ray, out hit, 100))
        //    {
        //        Debug.Log(hit.collider.gameObject.name);

        //    }
        //}
        //}

     //   play = GameObject.Find("Culb");
        //0表示鼠標左鍵1 表示鼠標右鍵
        if (Input.GetMouseButtonDown(0))
        {



           RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 100))
                {
                    if (hit.collider.gameObject.name == "she")//如果遊戲對象是蛇
                    {
                        isWin = true;

                        if (click == 2)//滿足點擊次數
                        {
                            isWin = false;
                            if (transform.Find("Point light").GetComponent<Light>().tag == "GameBoss")
                            {
                                transform.Find("Point light").GetComponent<Light>().range = 0.0f;
                            }
                            //transform.Find("Point light").GetComponent<Light>().enabled = false;
                        }

                    }
                }




        }

        if (click == 2)
        {
            if (Input.GetMouseButtonDown(1))
            {

                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 100))
                {
                    //Debug.Log(hit.collider.gameObject.name);
                    if (hit.collider.gameObject.name == "Cubeone" || hit.collider.gameObject.name == "Cubeone02")//拾取物品
                    {
                        Destroy(hit.collider.gameObject);
                    }

                }
            }
        }


    }

Unity 3D判斷遊戲執行的順序

// Use this for initialization
    void Start () {
        print ("開始執行StartCoroutine,,   "+Time.time);
        StartCoroutine ("aaa");
        print ("結束執行StartCoroutine,,   "+Time.time);
    }
    IEnumerator aaa()
    {
        print ("開始執行aaa,,   "+Time.time);
        yield return new WaitForSeconds (3);
        //

        print ("結束執行aaa,,   "+Time.time);

    }

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

    }   

Unity 3D前後左右移動

  // Use this for initialization
    void Start()
    {
        all = Resources.LoadAll("knight");
        nowFrame = 0;
        countFrame = all.Length;

        fps = 10;
    }
    int x = 100;
    int y = 100;
    object[] all;
    void OnGUI()
    {


        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), (Texture)Resources.Load("back"), ScaleMode.StretchToFill, false, 0);
        DrawAnimatiion(new Rect(x, y, 64, 64));
        if (GUILayout.RepeatButton("向前"))
        {
            x += 2;
        }
        if (GUILayout.RepeatButton("向後"))
        {
            x -= 2;
        }
        if (GUILayout.RepeatButton("向下"))
        {
            y += 2;
        }
        if (GUILayout.RepeatButton("向上"))
        {
            y -= 2;
        }
    }
    void FixedUpdate() 
    {

    }
    int nowFrame = 0;
    int countFrame = 0;
    float time;
    int fps = 0;

    void DrawAnimatiion(Rect r)
    {


        GUI.DrawTexture(r, (Texture)all[nowFrame], ScaleMode.StretchToFill, true, 0);
        time += Time.deltaTime;
        if(time >= 1.0/fps)
        {
            nowFrame++;
            time = 0;
        }
        if(nowFrame >=countFrame)
        {
            nowFrame = 0;
        }


    }

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

    }

Unity 3D 添加腳本移除腳本 添加背景顏色

public Texture myTexture;

    GameObject myObj;

    Renderer render;

    // Use this for initialization
    void Start () {
        myObj = GameObject.Find ("Cube");
        myObj.AddComponent (typeof(UnityScript03));//添加腳本
        render = (Renderer)myObj.GetComponent <Renderer>();
    }
    int a=10;
    int b=10;
    int c=10;
    void OnGUI()
    {
        if (GUILayout.Button ("添加顏色")) {
            myObj.GetComponent<Renderer>().material.color = Color.red;  

        }
        if (GUILayout.Button ("移除顏色")) {
            a++;
            b++;
            c++;
            myObj.transform.position = new Vector3(10,10,10);
            myObj.transform.localScale = new Vector3(a,b,c);

        }

        if (GUILayout.Button ("添加背景圖")) {
            render.material.mainTexture=myTexture;
        }
        if (GUILayout.Button ("移除腳本")) {
            Destroy(myObj.GetComponent<UnityScript03>());       
        }
    }

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

    }

Unity 3D 添加移除窗口


    // Use this for initialization
    void Start () {
        windowList.Add(new Rect(windowList.Count*200,100,200,150));
    }

    ArrayList windowList = new ArrayList();

    void OnGUI() 
    {
        int count = windowList.Count;
        for (int i = 0; i < count;i++ )
        {
            windowList[i] = GUILayout.Window(i, (Rect)windowList[i],myWindow,"這是第"+i+"個窗口");
        }
    }
    void myWindow(int winid) 
    {

        if (GUILayout.Button("添加一個窗口"))
        {

            windowList.Add(new Rect(windowList.Count * 200, 100, 200, 150));
        }
        if (GUILayout.Button("移除該窗口"))
        {
            windowList.Remove(windowList[winid]);
            //windowList.RemoveAt(winid);
        }
        GUI.DragWindow();
    }

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

    }

Unity 3D 添加組件 控制組件旋轉

string AddCub = "添加正方體";
    string AddSphere = "添加球體";
    bool isRotateCub = false;
    bool isRotatreSphere = false;
    string CubRotate="開始選裝方體";
    string SphereRotate = "開始旋轉球體";
    public int Angle=50;
    GameObject Cub;
    GameObject Sphere;
    // Use this for initialization
    void Start () {

    }   
    // Update is called once per frame
    void Update () {
        Cub = GameObject.Find ("Cub1");
        Sphere = GameObject.Find ("Sphere1");
        Diaoluo (isRotateCub, Cub);
        Diaoluo (isRotatreSphere, Sphere);

    }
    void Diaoluo(bool Rotate,GameObject obj)
    {
        if (obj) {
            if(Rotate)
            {
                obj.transform.Rotate(Vector3.up*Time.deltaTime*Angle);
            }
                }
        }
    void OnGUI()
    {
        if(GUILayout.Button(AddCub))
        {
            GameObject objCub = GameObject.CreatePrimitive(PrimitiveType.Cube);
            objCub.name="Cub1";
            objCub.AddComponent(typeof(Rigidbody));//添加剛體
            //objCub.rigidbody.useGravity = false;//移除剛體
            objCub.GetComponent<Renderer>().material.color = Color.red;
            objCub.transform.position = new Vector3(0,10,0);
        }
        if(GUILayout.Button(AddSphere))
        {
            GameObject objSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            objSphere.name="Sphere1";
            objSphere.AddComponent(typeof(Rigidbody));
            objSphere.GetComponent<Renderer>().material.color = Color.green;
            objSphere.transform.position = new Vector3(0,10,0);
        }
        if (GUILayout.Button (CubRotate)) {
                if(!isRotateCub)
            {
                isRotateCub = true;
                CubRotate="停止旋轉方體";
            }else
            {
                isRotateCub = false;
                CubRotate="開始旋轉方體";
            }       
        }
        if (GUILayout.Button (SphereRotate)) {
            if(!isRotatreSphere)
            {
                isRotatreSphere = true;
                SphereRotate="停止旋轉球體";
            }else
            {
                isRotatreSphere = false;
                SphereRotate="開始旋轉球體";
            }

        }
}
發佈了40 篇原創文章 · 獲贊 9 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章