如何在 Unity 中执行 Visual Studio .cs 文件?

问题描述 投票:0回答:1

首先,如果这是一个措辞不好的问题,我深表歉意,但我想不出任何其他方式来解释它。

我有一个程序(应该)手动添加形状并将其转换到 Unity 环境。我的实际“构造”代码如下:

Bus.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bus
{
    private MyVector _Position { get; set; }
    private MyVector _Rotation { get; set; }
    private MyVector _Scale { get; set; }

    public Bus(MyVector pPosition, MyVector pRotation, MyVector pScale)
    {
        _Position = pPosition;
        _Rotation = pRotation;
        _Scale = pScale;

        InitialiseBus();
    }

    public void InitialiseBus()
    {
        MyMatrix parentScale = MyMatrix.CreateScale(_Scale);
        MyMatrix parentRotationX = MyMatrix.CreateRotationX(_Rotation.X);
        MyMatrix parentRotationY = MyMatrix.CreateRotationY(_Rotation.Y);
        MyMatrix parentRotationZ = MyMatrix.CreateRotationZ(_Rotation.Z);
        MyMatrix parentPosition = MyMatrix.CreateTranslation(_Position);
        MyMatrix parentTransform = parentPosition.Multiply(parentRotationX.Multiply(parentRotationY.Multiply(parentRotationZ.Multiply(parentScale))));

        CreateBus(parentTransform);
    }

    public void CreateBus(MyMatrix pParentTransform)
    {
        CreateBody(pParentTransform);
        CreateWheels(pParentTransform);
    }

    private void CreateBody(MyMatrix pParentTransform)
    {
        GameObject body = GameObject.CreatePrimitive(PrimitiveType.Cube);
        body.GetComponent<Renderer>().material.color = Color.red;

        MyMatrix bodyScale = MyMatrix.CreateScale(new MyVector(4f, 1.5f, 1.5f));

        MyMatrix bodyTransform = pParentTransform.Multiply(bodyScale);
        bodyTransform.SetTransform(body);
    }

    private void CreateWheels(MyMatrix pParentTransform)
    {
        GameObject frontleftwheel = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        GameObject frontrightwheel = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        GameObject backleftwheel = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        GameObject backrightwheel = GameObject.CreatePrimitive(PrimitiveType.Cylinder);

        frontleftwheel.GetComponent<Renderer>().material.color = Color.black;
        frontrightwheel.GetComponent<Renderer>().material.color = Color.black;
        backleftwheel.GetComponent<Renderer>().material.color = Color.black;
        backrightwheel.GetComponent<Renderer>().material.color = Color.black;

        MyMatrix wheelScale = MyMatrix.CreateScale(new MyVector(0.8f, 0.2f, 0.8f));

        MyMatrix wheelRotation = MyMatrix.CreateRotationX((float)Math.PI / 2);

        MyMatrix frontleftwheelTranslation = MyMatrix.CreateTranslation(new MyVector(-1.5f, -0.9f, -0.8f));
        MyMatrix frontrightwheelTranslation = MyMatrix.CreateTranslation(new MyVector(-1.5f, -0.9f, 0.8f));
        MyMatrix backleftwheelTranslation = MyMatrix.CreateTranslation(new MyVector(1.5f, -0.9f, -0.8f));
        MyMatrix backrightwheelTranslation = MyMatrix.CreateTranslation(new MyVector(1.5f, -0.9f, 0.8f));

        MyMatrix wheelRotationScale = wheelRotation.Multiply(wheelScale);

        MyMatrix frontleftwheelMatrix = frontleftwheelTranslation.Multiply(wheelRotationScale);
        MyMatrix frontrightwheelMatrix = frontrightwheelTranslation.Multiply(wheelRotationScale);
        MyMatrix backleftwheelMatrix = backleftwheelTranslation.Multiply(wheelRotationScale);
        MyMatrix backrightwheelMatrix = backrightwheelTranslation.Multiply(wheelRotationScale);

        MyMatrix frontleftwheelTransform = pParentTransform.Multiply(frontleftwheelMatrix);
        MyMatrix frontrightwheelTransform = pParentTransform.Multiply(frontrightwheelMatrix);
        MyMatrix backleftwheelTransform = pParentTransform.Multiply(backleftwheelMatrix);
        MyMatrix backrightwheelTransform = pParentTransform.Multiply(backrightwheelMatrix);

        frontleftwheelTransform.SetTransform(frontleftwheel);
        frontrightwheelTransform.SetTransform(frontrightwheel);
        backleftwheelTransform.SetTransform(backleftwheel);
        backrightwheelTransform.SetTransform(backrightwheel);

    }
}

然后调用“构造”,我有

CreateEnvironment.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateEnvironment : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Bus bus = new Bus(new MyVector(0, 0, 0), new MyVector(0, 0, 0), new MyVector(1, 1, 1));
    }

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

    }
}

需要澄清的是,所有向量、矩阵和变换方法都可以完美工作。

我尝试从 Visual Studio 中使用“启动而不调试”来运行

CreateEnvironment.cs
,就像我通常在 VS 中所做的那样,但没有成功。我也尝试过在 Unity 上按“Play”,但这也不会在环境中生成形状。

任何有关问题的建议或未来问题的措辞将不胜感激。

c# unity-game-engine graphics visual-studio-2022 transformation
1个回答
0
投票

欢迎来到 SO,当然也欢迎来到 Unity。让我们澄清一些事情。首先什么是Unity。显然,它是一个游戏引擎。但这只是“流程”部分。什么是“技术流程”部分? Unity 部分(但不是“真正”)类似于框架。这意味着,您在 .NET 中所做的操作在 Unity 中不起作用,反之亦然。是的,有大量的解决方法和技巧,但这不是 Unity 或 .NET 应该工作的方式。它们的共同点就像核物理学与量子物理学一样多。当然,有些概念是匹配的,但它们要么协同工作,要么产生“爆炸”。从字面上看。

继续,因为它是一个游戏引擎,所以环境通常意味着平台。嗯,是。在大多数游戏引擎中,但在 Unity 中则不然。在 Unity 中,环境通常是运行时(意思是 - 当你玩时)、编辑器(在开发期间)和一种用于测试的混合体(通常来自编辑器),当你将代码作为传递进行测试或者可以模拟设备时(即在 Windows 设备(例如您的桌面)上模拟 iOS 设备。

接下来,Unity 允许您做什么?好吧,当我们谈论运行时时,几乎任何事情。这意味着大多数时候您不会进行任何运行时创建,例如形状创建。尽管如此,您还是从 C# 类构造函数来实现这一点,因此最多只完成一次。但我们只能说,您应该谨慎对待这个问题。我的意思是,在 Unity 中,通常不会鼓励您使用 C# 类范例或模式,除非需要(是的,这意味着您想要的任何含义,因为它取决于常识和正确的项目设计)。 您最常遇到的模式是ECS(实体组件系统模式)。最接近 C# 模式(但不是真的)是当您使用运行时创建的 SO(可编写脚本的对象)时,但大多数时候,它们将是在构建时创建并使用的“被动”资产在运行时通过引擎在使用时动态加载(意味着当它们在范围内并且需要时)。

备份:一般来说,您有 4 个“类别”(?因为没有更好的词)对象类型:

组件(在您的例子中,MonoBehaviours - 如果您想讨论“简单”组件和 MB 之间的区别,请随意发表评论,我们可以更深入) - 它们主要是基于 ECS 使用情况在运行时创建的类的实例(可选择使用动态添加或删除的组件)
  • SO(可编写脚本的对象) - 大多数资产“存在”在项目中,但在堆或堆栈上“不可见”(尽管它们是由引擎加载的),并且在进入范围时“初始化”(同样,我们可以更深入地讨论这个问题)。 -> 让事情变得更加复杂的是,MB 和 SO 的本机表示在内部使用相同的本机类,即使您在 C# 中具有不同的层次结构并且两者都有不同的代码模式或事件
  • GO(游戏对象)- 这是让你的游戏充满活力的任何手段(从武器到玩家头像或菜单或其他任何东西)
  • 通过组件加载的一些内部组件(音频组件、视频组件、渲染组件、物理组件 - 基本上表示 C#“脚本”运行时内的引擎)
  • 现在,解决你的问题。要实际“使用”或“运行”或“加载”MB,您需要将其附加到场景中存在的 GO。 (如果您想讨论场景……好吧,您知道该怎么做)。因此,只需添加一个新的
GameObject

,向其中添加

CreateEnvironment
组件,按
Play
即可。 请注意:除了

非常少

统一类(例如GameObject)之外,永远不要、永远、永远、

EVER
EVER使用new()实例化MB或SO。是的,它可能有效,但应用程序将默认使用 C 和 C++ 中的“未定义行为”。我之所以提到 C 和 C++ 是因为 Unity 是 C++ 引擎,而不是 C# 引擎:)
另外,请阅读 Unity 手册和 API 文档,如果您还有其他问题,请告诉我们!

祝你好运!

附注Unity 中没有

Main()

。如果您想讨论这个问题,请告诉我:)

    

© www.soinside.com 2019 - 2024. All rights reserved.