介绍

项目内容选自Unity官方Creator Kit - Beginner Code:https://assetstore.unity.com/packages/templates/tutorials/creator-kit-beginner-code-urp-151986

Unity官方API文档:Unity - Scripting API: MonoBehaviour

使用VS2022作为代码编辑器

Edit-Preferences中的External Tools 中设置编辑器,先用VS,以后再试着用用Rider

image

变量

在unity中,创建脚本scrpit实际上就是自定义新的component类型,可以附加到游戏对象GameObject上;C#脚本中的变量也可以看作这个Component类型中的属性;设置为Public就会暴露在 inspector 中

创建并使用变量:

  1. 新建一个 scene,命名为 MyScene,放入 Assets/Scenes 下
  2. 在新的 scene 中,新建一个 empty gameobject(空游戏对象)命名为 Player
  3. 为 Player 对象新增脚本组件,放入 Assets/Scripts 下,命名为 MainPlayer;在其中打印日志
  4. 将上一步生成的脚本作为Component应用到 Player 中;运行测试即可发现在控制台中打印的日志

image

在 Assets/Creator Kit - Beginner Code/Scripts/Tutorial/SpawnerSample.cs 脚本文件中,添加新的变量 radius,用来设置存放距离,该脚本用来生成指定对象

using UnityEngine;
using CreatorKitCode;

public class SpawnerSample : MonoBehaviour
{
    public GameObject ObjectToSpawn;

    // 距离
    public short radius;
    void Start()
    {
        int angle = 15;
        // 生成物体的位置,初始值为当前物体的位置
        Vector3 spawnPosition = transform.position;
        // Quaternion.Euler方法是进行旋转,这里代表单位向量(1,0,0)绕y轴转15°
        Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
        
        spawnPosition = transform.position + direction * radius;
        // 生成物体
        Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);

        angle = 55;
        direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
        spawnPosition = transform.position + direction * radius;
        Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);

        angle = 95;
        direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
        spawnPosition = transform.position + direction * radius;
        Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
    }
}

方法

public class SpawnerSample : MonoBehaviour
{
    public GameObject ObjectToSpawn;

    // 距离
    public short radius;
    void Start()
    {
        int angle = 15;
        for (; angle < 360; angle +=30) {
            GenNewObject(ref angle);
        }
    }

    private void GenNewObject(ref int angle)
    {
        Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
        Vector3 spawnPosition = transform.position + direction * radius;
        Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
    }
}

C#中,继承自 MonoBehaviour 的组件类,都会被自动实例化

C#中自定义的类作为成员,需要在类上添加[System.Serializable];类的属性要自动暴露在Unity的Inspector中,必须要Public权限;其它权限的属性需要添加[UnityEngine.SerializeField]

public class MainPlayer : MonoBehaviour
{
    public string myName;
    // cat的属性会出现在Inspector中
    public Cat cat;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("My name is " + myName);
    }

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

[System.Serializable]
public class Cat
{
    public string name;
    public string age; 
}

封装与继承

权限修饰符:

默认权限:

在unity中,public 成员可以在 Inspector 中直接操作;其它需要加[UnityEngine.SerializeField];子类中可以使用base关键字代表基类

//父类 Monster.cs
using System;
[Serializable]
public class Monster
{
    // 名字
    public string name;
    // 年龄
    public int age;
    // 毛色
    public string color;
    // 生命
    public int hp;

    public Monster() { }
    public Monster(string name, int age, string color, int hp)
    {
        this.name = name;
        this.age = age;
        this.color = color;
        this.hp = hp;
    }
}

// 派生类 Phoenix.cs
using System;
[Serializable]
public class Phoenix:Monster
{
    private bool isReborn=false;

    // 全参构造函数
    public Phoenix(string name, int age, string color, int hp,bool isReborn) : base(name, age, color, hp)
    {
        this.isReborn = isReborn;
    }
    //无参空构造方法
    public Phoenix()
    {
    }

    void Reborn() {
        this.hp = 1000;
        this.isReborn = true;
    }
}