简体   繁体   English

如何在C#项目中使用Unity

[英]How to use Unity in a C# project

I just started to use unity3d. 我刚刚开始使用unity3d。 I create a small scene with a first view controller, a script etc.etc. 我用第一个视图控制器,脚本等创建一个小场景。 But I want to know how I can load and play a scene in a C# code project (Visual Studio). 但是我想知道如何在C#代码项目(Visual Studio)中加载和播放场景。 Let me explain. 让我解释。

I've already made a C# project, with many variable, interfaces etc. How can I use an Unity libraries, and some methods in c# code to load and play an unity scene. 我已经制作了一个C#项目,其中包含许多变量,接口等。如何使用Unity库以及c#代码中的某些方法来加载和播放统一场景。

Is this kind of code exist ? 这种代码是否存在? like : 喜欢 :

using System.Unity.Scene;

......
{
 loadScene(myscene);
playScene(myscene);
...
}

....

?

While you can load Scene's from code in the Unity API (See Application.LoadLevel ), You can't exactly use the Unity API outside of unity. 虽然您可以从Unity API中的代码加载Scene的场景(请参阅Application.LoadLevel ),但您无法在unity之外完全使用Unity API。 While you may have some luck importing the library's, the actually functionality won't be there because of the way the Unity IDE/Editor works. 虽然您可能会导入库的运气,但是由于Unity IDE / Editor的工作方式,实际的功能将不存在。

Might I suggest building your non-unity project into a library (Make sure you set the Target framework to 3.5 or below as Unity uses mono 2.0 libraries instead of .NET), and then using that in your unity project. 可能我建议将您的非统一项目构建到一个库中(确保将Target框架设置为3.5或更低版本,因为Unity使用mono 2.0库而不是.NET),然后在您的统一项目中使用它。

You may also wish to look at UnityVS , It's a hands down the best Extension for unity: It gives a great deal of extension and usability for those who prefer VisualStudios over MonoDev. 您可能还希望看看UnityVS ,这是统一性最好的扩展:它为那些喜欢VisualStudios而不是MonoDev的人提供了很多扩展和可用性。

To load a Unity scene using C#: 要使用C#加载Unity场景:

using UnityEngine;
using System;

class YourClass
{
    // ...

    public void LoadScene( string sceneName )
    {
        Application.LoadLevel( sceneName );
    }

    // ...
}

http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html

But as others have mentioned, you should really start Googling some basic Unity C# tutorials. 但是,正如其他人提到的那样,您应该真正开始使用Google基本的一些Unity C#教程。

I think you are looking at this the wrong way, Unity3D is a development environment, and provides much of the tools and structure you need to work within the framework they provide, the meta-data behind the scenes enables much of it's features, to link objects together from the scene to the C# classes. 我认为您看错了方向,Unity3D是一个开发环境,并提供了在它们提供的框架内工作所需的许多工具和结构,幕后的元数据启用了其许多功能,可以进行链接对象从场景到C#类。

My suggestion is to look at integrating other libraries into the Unity3D project, rather than the other way around, which will work fine. 我的建议是着眼于将其他库集成到Unity3D项目中,而不是相反,这会很好地工作。

I have done similar things before, such as integrating some serialization libraries and API's. 我之前做过类似的事情,例如集成一些序列化库和API。

One option you could look at is to hook the Unity3D project up to a web service, and control things that way, allowing you to have some of that logic outside of the Unity3D project 您可能会看到的一个选择是将Unity3D项目挂接到Web服务上,并以此方式进行控制,从而使您可以将某些逻辑包含在Unity3D项目之外

I suggest here as a good start for learning Unity3D: 我建议在这里作为学习Unity3D的一个好的开始:

https://www.assetstore.unity3d.com/#/content/5087 https://www.assetstore.unity3d.com/#/content/5087

Try using monodevelop instead. 尝试改用monodevelop。 Unity integrates really well with Monodevelop and it comes packaged with unity. Unity与Monodevelop集成得很好,并且与Unity打包在一起。

In your editor on the top menu Assets->Create->C# Script 在编辑器的顶部菜单中,资产->创建-> C#脚本

Double Click on your newly created C# script and it should open in monodevelop. 双击新创建的C#脚本,它将在monodevelop中打开。

Then code away! 然后将代码删除!

Every C# script should use 每个C#脚本都应使用

using UnityEngine;

To access the framework, this is usually set up automatically by default. 要访问该框架,通常默认情况下会自动进行设置。

To load a scene: 加载场景:

Application.LoadLevel("Level Name");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM