跳到主要内容

基础1

安装net 环境 支持语法提示

https://dotnet.microsoft.com/zh-cn/download/dotnet/10.0

新建 场景 cmd + N

保存场景 cmd + S

加载当前主 场景 双击存在的场景

将存在的 脚本 拖拽到 场景的 main camera 上 点击运行 就会 运行当前脚本


using UnityEngine;

public class Animal{
public string name;

public Animal( string name){
this.name = name;
}

public virtual void speak(){

}

public void run()
{
Debug.LogFormat("{0} 在跑",this.name);
}
}

public class Dog:Animal{
public Dog( string name):base(name){
this.name = name;
}
public override void speak(){
Debug.Log("wang wang");
}
}

public class test : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Animal a1 = new Dog("milu");
a1.speak();
a1.run();
}

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

}
}


脚本绑定到控件

选中 相关 控件

将对应的 脚本 拖拽到 GameObject 的 inspect 上

脚本中获取 GameObject

在脚本中 定义 相关 public 变量 并控制

选中 挂载 脚本的 控件

在 inspect 面板中找到相关脚本 找到对应的 变量名 与 GameObject 绑定

public class test : MonoBehaviour
{

public GameObject cube;

void Update()
{
// 旧版 输入系统
// if(Input.GetKeyDown(KeyCode.A))
// {
// Debug.Log("a");
// cube.SetActive(true);
// }

// 替换 GetKeyDown(KeyCode.A)
if (Keyboard.current.aKey.wasPressedThisFrame)
{
// 处理 A 键按下
Debug.Log("a");
cube.SetActive(true);
}

if (Keyboard.current.bKey.wasPressedThisFrame)
{
// 处理 A 键按下
Debug.Log("a");
cube.SetActive(false);
}

}
}

添加场景到 Build Settings

  1. 打开 Build Settings
    • 菜单栏:File → Build Settings (或按 Ctrl+Shift+B / Cmd+Shift+B)
  2. 添加场景
    • 从 Project 窗口将你的场景文件拖到 "Scenes In Build" 区域
    • 或者 点击 "Add Open Scenes" 添加当前打开的场景