要想快速入门还是只能看文档,看视频太慢了,看不全。
在w3cschool有文档的相关教程https://www.w3cschool.cn/unity3d_jc/unity3d_jc-zyo1383m.html
新手选择3d空项目,无法创建项目可能是权限管理,网络vpn等问题。
绑定脚本
在assets里面随便新建一个cs类,默认就有一个模板,
要做的是绑定到控件里面,
我这里选择绑定摄像机,
脚本完整代码
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class MyMove : MonoBehaviour
- {
- public GameObject myCube;
- public GameObject myCube2;
- public int transSpeed = 100;
- public float rotaSpeed = 10.5f;
- public float scale = 1.3f;
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
- // this.
- //this.transform.
- this.transform.Translate(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- }
- void OnGUI()
- {
-
- if (GUILayout.Button("创建Cute", GUILayout.Height(30)))
- {
- myCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
- myCube.AddComponent
(); - myCube.GetComponent
().material.color = Color.blue; - myCube.transform.position = new Vector3(0, 10, 0);
- }
- if (GUILayout.Button("创建Sphere", GUILayout.Height(30)))
- {
- GameObject m_cube = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- m_cube.AddComponent
(); - m_cube.GetComponent
().material.color = Color.red; - m_cube.transform.position = new Vector3(0, 10, 0);
- }
-
- if (GUILayout.Button("移动立方体", GUILayout.Height(30)))
- {
- if (myCube == null)
- {
- showTipDialog("立方体为空");
- return;
- }
- myCube.transform.Translate(Vector3.forward * transSpeed * Time.deltaTime, Space.World);
- }
- if (GUILayout.Button("旋转立方体", GUILayout.Height(30)))
- {
- if (myCube == null)
- {
- showTipDialog("立方体为空");
- return;
- }
- myCube.transform.Rotate(Vector3.up * rotaSpeed, Space.World);
- }
- if (GUILayout.Button("缩放立方体"))
- {
- if (myCube == null)
- {
- showTipDialog("立方体为空");
- return;
- }
- if (myCube.transform.localScale.y == scale)
- {
- myCube.transform.localScale = new Vector3(1, 1, 1);
-
- }
- else
- {
- myCube.transform.localScale = new Vector3(scale, scale, scale);
-
- }
-
- }
- if (GUILayout.Button("隐藏立方体"))
- {
- if (myCube == null)
- {
- showTipDialog("立方体为空");
- return;
- }
-
-
- if (myCube.activeSelf)
- {
- myCube.SetActive(false);
-
- }
- else
- {
- myCube.SetActive(true);
-
- }
- }
- if (GUILayout.Button("销毁立方体"))
- {
- if (myCube == null)
- {
- showTipDialog("立方体为空");
- return;
- }
- // this.Remove
- myCube.transform.localScale = new Vector3(scale, scale, scale);
- Destroy(myCube);
- }
-
- }
- public void showTipDialog(string tip)
- {
- UnityEditor.EditorUtility.DisplayDialog("提示", tip, "确认");
- // return;
- }
- }
运行效果
当然上图里面部分东西是我手动添加的,其他是通过脚本触发自动执行的。
脚本提示问题
在unity3d中,选择edit-首选项->
如下图所示选择识别到的visual studio
如果没有配置的情况下用vs打开是没有语法提示的,配置好之后的效果
不难看出,实际上是载入了assembly-csharp.dll,玩游戏逆向的朋友应该对这个很熟悉, https://docs.microsoft.com/zh-cn/visualstudio/gamedev/unity/get-started/getting-started-with-visual-studio-tools-for-unity?view=vs-2022&pivots=windows