using System.Collections; using System.Collections.Generic; using UnityEngine; public class cube : MonoBehaviour { // Start is called before the first frame update // Use this for initialization void Start() { //我们将obj1初始化为一个Cube立方体,当然我们也可以初始化为其他的形状 GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Cube); //设置物体的位置Vector3三个参数分别代表x,y,z的坐标数 obj1.transform.position = new Vector3(1, 1, 1); //给这个创建出来的对象起个名字 obj1.name = "dujia"; //设置物体的tag值,在赋值之前要在Inspector面板中注册一个tag值 //注册tag值得方法,用鼠标选中摄像机对象在Inspector面板中找到tag,选addtag obj1.tag = "shui"; //设置物体贴图要图片文件放在(Resources)文件夹下,没有自己创建 obj1.GetComponent().material.mainTexture = (Texture)Resources.Load("ai"); } void OnGUI() { if (GUILayout.Button("Left")) { //通过tag值找到相应的对象 GameObject obj2 = GameObject.FindGameObjectWithTag("shui"); obj2.transform.Rotate(new Vector3(0, 10, 0)); //让该物体转动参数的意义(x,y,z)就是沿着哪个轴转动 } if (GUILayout.Button("UP")) { GameObject obj3 = GameObject.FindGameObjectWithTag("shui"); //参数(x,y,z)y为正向上其他的自己猜猜吧 obj3.transform.Translate(new Vector3(0, 3 * Time.deltaTime, 0)); } } // Update is called once per frame void Update() { } }