Unity創建游戲對象_位置,大小,旋轉


實現代碼

using UnityEngine;
using System.Collections;

public class C_3_5_1 : MonoBehaviour {

    void OnGUI()
    {
        if(GUILayout.Button("創建立方體",GUILayout.Height(50)))
        {
            //設置該模型默認為立方體
            GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
            //為對象添加一個剛體,賦予物理屬性
            obj.AddComponent<Rigidbody>();
            //賦予對象的材質紅色
            obj.GetComponent<Renderer>().material.color = Color.red;
            //設置對象的名稱
            obj.name = "Cube";
            //設置此模型材質的位置坐標
            obj.transform.position = new Vector3(0,10f,0);
        }
        if(GUILayout.Button("創建球體",GUILayout.Height(50)))
        {
            //設置該模型默認為球體
            GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            //為對象添加一個剛體,賦予物理屬性
            obj.AddComponent<Rigidbody>();
            //賦予對象的材質紅色
            obj.GetComponent<Renderer>().material.color = Color.green;
            //設置對象的名稱
            obj.name = "Sphere";
            //設置此模型材質的位置坐標
            obj.transform.position = new Vector3(0,15f,0);
        }
        if(GUILayout.Button("創建膠囊", GUILayout.Height(50)))
        {
            //設置該模型為膠囊狀
            GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            //為該對象添加剛體組件
            obj.AddComponent<Rigidbody>();
            //賦予對象的材質黃色
            obj.GetComponent<Renderer>().material.color = Color.yellow;
            //設置對象名稱
            obj.name = "Capsule";
            //設置此模型的位置坐標
            obj.transform.position = new Vector3(0, 5f, 0);
        }
        if (GUILayout.Button("創建圓柱", GUILayout.Height(50)))
        {
            //設置該模型為圓柱
            GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
            //為該對象添加剛體組件
            obj.AddComponent<Rigidbody>();
            //賦予對象的材質黃色
            obj.GetComponent<Renderer>().material.color = Color.yellow;
            //設置對象名稱
            obj.name = "Cylinder";
            //設置此模型的位置坐標
            obj.transform.position = new Vector3(0, 5f, 0);
            //設置此模型伸縮變化
            obj.transform.localScale = new Vector3(1, 0.5f, 1);
            //設置物體旋轉
            obj.transform.rotation = Quaternion.Euler(new Vector3(90, 0, 0));
            obj.GetComponent<Renderer>().material.color = new Color(255,0,0,1);
        }
       
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM